class LruRedux::TTL::Cache
Attributes
max_size[R]
ttl[R]
Public Class Methods
new(*args)
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 6 def initialize(*args) max_size, ttl = args ttl ||= :none raise ArgumentError.new(:max_size) if max_size < 1 raise ArgumentError.new(:ttl) unless ttl == :none || ((ttl.is_a? Numeric) && ttl >= 0) @max_size = max_size @ttl = ttl @data_lru = {} @data_ttl = {} end
Public Instance Methods
[](key)
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 78 def [](key) ttl_evict found = true value = @data_lru.delete(key){ found = false } if found @data_lru[key] = value else nil end end
[]=(key, val)
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 90 def []=(key, val) ttl_evict @data_lru.delete(key) @data_ttl.delete(key) @data_lru[key] = val @data_ttl[key] = Time.now.to_f if @data_lru.size > @max_size key, _ = @data_lru.first @data_ttl.delete(key) @data_lru.delete(key) end val end
clear()
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 145 def clear @data_lru.clear @data_ttl.clear end
count()
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 154 def count @data_lru.size end
delete(key)
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 128 def delete(key) ttl_evict @data_lru.delete(key) @data_ttl.delete(key) end
Also aliased as: evict
each() { |pair| ... }
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 109 def each ttl_evict array = @data_lru.to_a array.reverse!.each do |pair| yield pair end end
Also aliased as: each_unsafe
expire()
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 150 def expire ttl_evict end
fetch(key) { || ... }
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 66 def fetch(key) ttl_evict found = true value = @data_lru.delete(key){ found = false } if found @data_lru[key] = value else yield if block_given? end end
getset(key) { || ... }
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 44 def getset(key) ttl_evict found = true value = @data_lru.delete(key){ found = false } if found @data_lru[key] = value else result = @data_lru[key] = yield @data_ttl[key] = Time.now.to_f if @data_lru.size > @max_size key, _ = @data_lru.first @data_ttl.delete(key) @data_lru.delete(key) end result end end
key?(key)
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 137 def key?(key) ttl_evict @data_lru.key?(key) end
Also aliased as: has_key?
max_size=(max_size)
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 22 def max_size=(max_size) max_size ||= @max_size raise ArgumentError.new(:max_size) if max_size < 1 @max_size = max_size resize end
to_a()
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 121 def to_a ttl_evict array = @data_lru.to_a array.reverse! end
ttl=(ttl)
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 33 def ttl=(ttl) ttl ||= @ttl raise ArgumentError.new(:ttl) unless ttl == :none || ((ttl.is_a? Numeric) && ttl >= 0) @ttl = ttl ttl_evict end
Protected Instance Methods
resize()
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 179 def resize ttl_evict while @data_lru.size > @max_size key, _ = @data_lru.first @data_ttl.delete(key) @data_lru.delete(key) end end
ttl_evict()
click to toggle source
# File lib/lru_redux/ttl/cache.rb, line 165 def ttl_evict return if @ttl == :none ttl_horizon = Time.now.to_f - @ttl key, time = @data_ttl.first until time.nil? || time > ttl_horizon @data_ttl.delete(key) @data_lru.delete(key) key, time = @data_ttl.first end end
valid?()
click to toggle source
for cache validation only, ensures all is sound
# File lib/lru_redux/ttl/cache.rb, line 161 def valid? @data_lru.size == @data_ttl.size end