Class: SimpleCache
- Inherits:
-
Object
- Object
- SimpleCache
- Defined in:
- lib/simple_cache.rb
Overview
SimpleCache is a very simple cache store.
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
-
#cache!(key, value) ⇒ Object
Cache an item.
-
#has_key?(key) ⇒ Boolean
Does it have this key.
-
#initialize(options = {:max_size => 10}) ⇒ SimpleCache
constructor
Create a new cache store: SimpleCache.new(:max_size => 100) to set the size of the cache.
-
#value(key) ⇒ Object
Get value of an key.
Constructor Details
#initialize(options = {:max_size => 10}) ⇒ SimpleCache
10 11 12 13 |
# File 'lib/simple_cache.rb', line 10 def initialize( = {:max_size => 10}) @options = @store = {} end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/simple_cache.rb', line 4 def @options end |
#store ⇒ Object
Returns the value of attribute store.
5 6 7 |
# File 'lib/simple_cache.rb', line 5 def store @store end |
Instance Method Details
#cache!(key, value) ⇒ Object
Cache an item
21 22 23 24 25 26 27 |
# File 'lib/simple_cache.rb', line 21 def cache!(key, value) if @store.size >= @options[:max_size] @store.delete(@store.keys.first) end @store[key] = value end |
#has_key?(key) ⇒ Boolean
Does it have this key
16 17 18 |
# File 'lib/simple_cache.rb', line 16 def has_key?(key) !@store[key].nil? end |
#value(key) ⇒ Object
Get value of an key
31 32 33 |
# File 'lib/simple_cache.rb', line 31 def value(key) @store[key] end |