3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/cacheable/types/attribute_cache.rb', line 3
def with_attribute(*attributes)
self.cached_indices = attributes.inject({}) { |indices, attribute| indices[attribute] = {} }
class_eval do
after_commit :expire_attribute_cache, :on => :update
after_commit :expire_all_attribute_cache, :on => :update
end
attributes.each do |attribute|
define_singleton_method("find_cached_by_#{attribute}") do |value|
self.cached_indices["#{attribute}"] ||= []
self.cached_indices["#{attribute}"] << value
Rails.cache.fetch attribute_cache_key("#{attribute}", value) do
self.send("find_by_#{attribute}", value)
end
end
define_singleton_method("find_cached_all_by_#{attribute}") do |value|
self.cached_indices["#{attribute}"] ||= []
self.cached_indices["#{attribute}"] << value
Rails.cache.fetch all_attribute_cache_key("#{attribute}", value) do
self.send("find_all_by_#{attribute}", value)
end
end
end
end
|