Sha256: ede073bcb24c9548d97cff74d44c67fe3d3e7b50c12f7142ae6f64f716357548
Contents?: true
Size: 1.7 KB
Versions: 3
Compression:
Stored size: 1.7 KB
Contents
# Ruby 1.9 makes our life easier, Hash is already ordered # # This is an ultra efficient 1.9 freindly implementation class LruRedux::Cache def initialize(max_size) @max_size = max_size @data = {} end def max_size=(size) raise ArgumentError.new(:max_size) if @max_size < 1 @max_size = size if @max_size < @data.size @data.keys[0..@max_size-@data.size].each do |k| @data.delete(k) end end end def getset(key) found = true value = @data.delete(key){ found = false } if found @data[key] = value else result = @data[key] = yield # this may seem odd see: http://bugs.ruby-lang.org/issues/8312 @data.delete(@data.first[0]) if @data.length > @max_size result end end def fetch(key) found = true value = @data.delete(key){ found = false } if found @data[key] = value else yield if block_given? end end def [](key) found = true value = @data.delete(key){ found = false } if found @data[key] = value else nil end end def []=(key,val) @data.delete(key) @data[key] = val # this may seem odd see: http://bugs.ruby-lang.org/issues/8312 @data.delete(@data.first[0]) if @data.length > @max_size val end def each array = @data.to_a array.reverse!.each do |pair| yield pair end end # used further up the chain, non thread safe each alias_method :each_unsafe, :each def to_a array = @data.to_a array.reverse! end def delete(k) @data.delete(k) end def clear @data.clear end def count @data.size end # for cache validation only, ensures all is sound def valid? true end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
lru_redux-0.8.4 | lib/lru_redux/cache19.rb |
lru_redux-0.8.3 | lib/lru_redux/cache19.rb |
lru_redux-0.8.2 | lib/lru_redux/cache19.rb |