63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/model-cache.rb', line 63
def cache_method_for_time(sym, time)
alias_method :"__noncached_#{sym}", sym
define_method sym do |*args|
ckey = [self.cache_key, self.instance_variable_get(:@__invalidate_cache), sym, *args]
ckey = ckey.hash.to_s(16)
ModelCache.cache(ckey, time) do
self.send(:"__noncached_#{sym}", *args)
end
end
define_method :"__is_cached_#{sym}?" do |*args|
ckey = [self.cache_key, self.instance_variable_get(:@__invalidate_cache), sym, *args]
ckey = ckey.hash.to_s(16)
if CACHE.class.name == 'Memcached'
begin
result = CACHE.get(ckey.hash.to_s)
cache_hit = true
rescue Memcached::NotFound => e
end
elsif CACHE.class.name == 'MemCache' or CACHE.class.name == 'Dalli::Client'
result = CACHE.get(ckey.hash.to_s)
if result
cache_hit = true
end
if result == NIL_OBJECT
result = nil
end
else
raise "CACHE object not configured #{CACHE.inspect}!"
end
cache_hit
end
define_method :"__flush_#{sym}" do |*args|
ckey = [self.cache_key, sym, *args]
ckey = ckey.hash.to_s(16)
CACHE.delete(ckey)
end
end
|