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 |
# 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 end |
Instance Method Details
#[](key) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/volatile_hash.rb', line 14 def [](key) value = @cache[key] if @strategy == 'ttl' if expired?(key) @cache.delete key @registry.delete key value = nil 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
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/volatile_hash.rb', line 28 def []=(key, value) if @strategy == 'ttl' @registry[key] = Time.now + @ttl.to_f @cache[key] = value else @item_order.unshift key @cache[key] = value lru_invalidate if @max_items < @item_order.length end end |