Class: VolatileHash
- Inherits:
-
Object
- Object
- VolatileHash
- Defined in:
- lib/volatile_hash.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(options) ⇒ VolatileHash
constructor
A new instance of VolatileHash.
Constructor Details
#initialize(options) ⇒ VolatileHash
Returns a new instance of VolatileHash.
2 3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/volatile_hash.rb', line 2 def initialize() @strategy = [:strategy] || 'ttl' @cache = {} if @strategy == 'ttl' @registry = {} @ttl = [:ttl] || 3600 else #lru @max_items = [:max] || 10 @item_order = [] end @refresh = [:refresh] or false end |
Instance Method Details
#[](key) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/volatile_hash.rb', line 15 def [](key) value = @cache[key] if @strategy == 'ttl' if @registry[key] && expired?(key) @cache.delete key @registry.delete key value = nil end if @refresh and @registry[key] set_ttl(key) end else lru_update key if @cache.has_key?(key) end value #in case of LRU, just return the value that was read end |
#[]=(key, value) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/volatile_hash.rb', line 32 def []=(key, value) if @strategy == 'ttl' set_ttl(key) @cache[key] = value else @item_order.unshift key @cache[key] = value lru_invalidate if @max_items < @item_order.length end end |