class LruRedux::Cache
Ruby 1.9 makes our life easier, Hash is already ordered
This is an ultra efficient 1.9 freindly implementation
Ruby 1.9 makes our life easier, Hash is already ordered
This is an ultra efficient 1.9 freindly implementation
Public Class Methods
new(*args)
click to toggle source
# File lib/lru_redux/cache.rb, line 5 def initialize(*args) max_size, _ = args raise ArgumentError.new(:max_size) if max_size < 1 @max_size = max_size @data = {} end
Public Instance Methods
[](key)
click to toggle source
# File lib/lru_redux/cache.rb, line 50 def [](key) found = true value = @data.delete(key){ found = false } if found @data[key] = value else nil end end
[]=(key,val)
click to toggle source
# File lib/lru_redux/cache.rb, line 60 def []=(key,val) @data.delete(key) @data[key] = val @data.shift if @data.length > @max_size val end
clear()
click to toggle source
# File lib/lru_redux/cache.rb, line 94 def clear @data.clear end
count()
click to toggle source
# File lib/lru_redux/cache.rb, line 98 def count @data.size end
delete(key)
click to toggle source
# File lib/lru_redux/cache.rb, line 82 def delete(key) @data.delete(key) end
Also aliased as: evict
each() { |pair| ... }
click to toggle source
# File lib/lru_redux/cache.rb, line 67 def each array = @data.to_a array.reverse!.each do |pair| yield pair end end
Also aliased as: each_unsafe
fetch(key) { || ... }
click to toggle source
# File lib/lru_redux/cache.rb, line 40 def fetch(key) found = true value = @data.delete(key){ found = false } if found @data[key] = value else yield if block_given? end end
getset(key) { || ... }
click to toggle source
# File lib/lru_redux/cache.rb, line 28 def getset(key) found = true value = @data.delete(key){ found = false } if found @data[key] = value else result = @data[key] = yield @data.shift if @data.length > @max_size result end end
key?(key)
click to toggle source
# File lib/lru_redux/cache.rb, line 88 def key?(key) @data.key?(key) end
Also aliased as: has_key?
max_size=(max_size)
click to toggle source
# File lib/lru_redux/cache.rb, line 14 def max_size=(max_size) max_size ||= @max_size raise ArgumentError.new(:max_size) if max_size < 1 @max_size = max_size @data.shift while @data.size > @max_size end
to_a()
click to toggle source
# File lib/lru_redux/cache.rb, line 77 def to_a array = @data.to_a array.reverse! end
ttl=(_)
click to toggle source
# File lib/lru_redux/cache.rb, line 24 def ttl=(_) nil end
Protected Instance Methods
valid?()
click to toggle source
for cache validation only, ensures all is sound
# File lib/lru_redux/cache.rb, line 105 def valid? true end