Module: ModelCache::ClassMethods

Defined in:
lib/model-cache.rb

Instance Method Summary collapse

Instance Method Details

#cache_method(*args) ⇒ Object



56
57
58
59
60
61
# File 'lib/model-cache.rb', line 56

def cache_method(*args)
  opts = args.extract_options!
	args.each do |sym|
	  cache_method_for_time(sym, (opts[:time] || DEFAULT_TIME))
	end
end

#cache_method_for_time(sym, time) ⇒ Object



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