class WoolenCommon::Cache
Constants
- CACHE_OBJECT
- CACHE_VERSION
Attributes
expiration[R]
max_num[R]
max_obj_size[R]
max_size[R]
Public Class Methods
new(*args, &hook)
click to toggle source
initialize(max_obj_size
= nil, max_size
= nil, max_num
= nil,
expiration = nil, &hook)
initialize(hash, &hook)
# File lib/woolen_common/cache.rb, line 25 def initialize(*args, &hook) if args.size == 1 and args[0].kind_of?(Hash) @max_obj_size = @max_size = @max_num = @expiration = nil args[0].each do |k, v| k = k.intern if k.respond_to?(:intern) case k when :max_obj_size @max_obj_size = v when :max_size @max_size = v when :max_num @max_num = v when :expiration @expiration = v else warn "cache unknown key :#{k}=>#{v}" end end else @max_obj_size, @max_size, @max_num, @expiration = args end # Sanity checks. if @max_obj_size and @max_size and @max_obj_size > @max_size raise ArgumentError, "max_obj_size exceeds max_size (#{@max_obj_size} > #{@max_size})" end if @max_obj_size and @max_obj_size <= 0 raise ArgumentError, "invalid max_obj_size `#{@max_obj_size}'" end if @max_size and @max_size <= 0 raise ArgumentError, "invalid max_size `#{@max_size}'" end if @max_num and @max_num <= 0 raise ArgumentError, "invalid max_num `#{@max_num}'" end if @expiration and @expiration <= 0 raise ArgumentError, "invalid expiration `#{@expiration}'" end @hook = hook @objs = {} @size = 0 @list = [] @hits = 0 @misses = 0 end
version()
click to toggle source
# File lib/woolen_common/cache.rb, line 18 def self.version CACHE_VERSION end
Public Instance Methods
[](key)
click to toggle source
# File lib/woolen_common/cache.rb, line 169 def [](key) self.expire unless @objs.include?(key) @misses += 1 return nil end obj = @objs[key] obj.atime = Time.now.to_i @list.each_index do |i| if @list[i] == key @list.delete_at(i) break end end @list.push(key) @hits += 1 obj.content end
[]=(key, obj)
click to toggle source
# File lib/woolen_common/cache.rb, line 192 def []=(key, obj) self.expire if self.cached?(key) self.invalidate(key) end size = obj.to_s.size if @max_obj_size and @max_obj_size < size debug("warning: `#{obj.inspect}' isn't cached because its size exceeds #{@max_obj_size}") return obj end if @max_obj_size.nil? and @max_size and @max_size < size debug("warning: `#{obj.inspect}' isn't cached because its size exceeds #{@max_size}") return obj end if @max_num and @max_num == @list.size self.invalidate(@list.first) end @size += size if @max_size while @max_size < @size self.invalidate(@list.first) end end @objs[key] = CACHE_OBJECT.new(obj, size, Time.now.to_i) @list.push(key) obj end
cached?(key)
click to toggle source
# File lib/woolen_common/cache.rb, line 76 def cached?(key) @objs.include?(key) end
cached_value?(val)
click to toggle source
# File lib/woolen_common/cache.rb, line 85 def cached_value?(val) self.each_value do |v| return true if v == val end false end
Also aliased as: has_value?, value?
each_key() { |key| ... }
click to toggle source
# File lib/woolen_common/cache.rb, line 239 def each_key @objs.each_key do |key| yield key end self end
each_pair() { |key, content| ... }
click to toggle source
# File lib/woolen_common/cache.rb, line 230 def each_pair @objs.each do |key, obj| yield key, obj.content end self end
Also aliased as: each
each_value() { |content| ... }
click to toggle source
# File lib/woolen_common/cache.rb, line 246 def each_value @objs.each_value do |obj| yield obj.content end self end
empty?()
click to toggle source
# File lib/woolen_common/cache.rb, line 253 def empty? @objs.empty? end
expire()
click to toggle source
# File lib/woolen_common/cache.rb, line 156 def expire if @expiration now = Time.now.to_i @list.each_index do |i| key = @list[i] break unless @objs[key].atime + @expiration <= now self.invalidate(key) end end # GC.start end
fetch(key, default = nil) { |key| ... }
click to toggle source
# File lib/woolen_common/cache.rb, line 257 def fetch(key, default = nil) val = self[key] if val.nil? if default val = self[key] = default elsif block_given? val = self[key] = yield(key) else raise IndexError, "invalid key `#{key}'" end end val end
index(val)
click to toggle source
# File lib/woolen_common/cache.rb, line 95 def index(val) self.each_pair do |k, v| return k if v == val end nil end
invalidate(key) { |key| ... }
click to toggle source
# File lib/woolen_common/cache.rb, line 120 def invalidate(key) obj = @objs[key] if obj if @hook @hook.call(key, obj.content) end @size -= obj.size @objs.delete(key) @list.each_index do |i| if @list[i] == key @list.delete_at(i) break end end elsif block_given? return yield(key) end obj.content end
Also aliased as: delete
invalidate_all()
click to toggle source
# File lib/woolen_common/cache.rb, line 142 def invalidate_all if @hook @objs.each do |key, obj| @hook.call(key, obj) end end @objs.clear @list.clear @size = 0 end
Also aliased as: clear
keys()
click to toggle source
# File lib/woolen_common/cache.rb, line 102 def keys @objs.keys end
length()
click to toggle source
# File lib/woolen_common/cache.rb, line 106 def length @objs.length end
Also aliased as: size
state_statics()
click to toggle source
The total size of cached objects, the number of cached objects, the number of cache hits, and the number of cache misses.
# File lib/woolen_common/cache.rb, line 273 def state_statics [@size, @list.size, @hits, @misses] end
store(key, value)
click to toggle source
# File lib/woolen_common/cache.rb, line 226 def store(key, value) self[key] = value end
to_hash()
click to toggle source
# File lib/woolen_common/cache.rb, line 112 def to_hash @objs.dup end
values()
click to toggle source
# File lib/woolen_common/cache.rb, line 116 def values @objs.collect { |key, obj| obj.content } end