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"}
Initializes a LruHash with a given maxsize.
# File lib/rufus/lru.rb, line 58 def initialize (maxsize) super() @maxsize = maxsize @lru_keys = [] end
# File lib/rufus/lru.rb, line 85 def [] (key) value = super return nil unless value touch(key) value end
# File lib/rufus/lru.rb, line 94 def []= (key, value) remove_lru super touch(key) value end
# File lib/rufus/lru.rb, line 110 def delete (key) value = super @lru_keys.delete(key) value end
# File lib/rufus/lru.rb, line 66 def maxsize= (s) @maxsize = s remove_lru end
Generated with the Darkfish Rdoc Generator 2.