Class | Rufus::Lru::Hash |
In: |
lib/rufus/lru.rb
|
Parent: | ::Hash |
A Hash that has a max size. After the maxsize has been reached, the least recently used entries (LRU hence), will be discared to make room for the new entries.
require 'rubygems' require 'rufus/lru' h = LruHash.new(3) 5.times { |i| h[i] = "a" * i } puts h.inspect # >> {2=>"aa", 3=>"aaa", 4=>"aaaa"} h[:newer] = "b" puts h.inspect # >> {:newer=>"b", 3=>"aaa", 4=>"aaaa"}
One may want to squeeze hash manually
h = LruHash.new(3, true) # or h.squeeze_on_demand=true after h is created . . h.squeeze!
If a value has destructor method clear it may be called upon the key-value removal
h = LruHash.new(33, does_not_matter, true) # or h.clear_value_on_removal=true after h is created
Nota bene: this class is not thread-safe. If you need something thread-safe, use Rufus::Lru::SynchronizedHash.
lru_keys | -> | ordered_keys |
Returns the keys with the lru in front. |
lru_keys | [R] | |
maxsize | [R] | |
on_removal | [RW] |
Initializes a LruHash with a given maxsize.
Options: