/
base.rb
70 lines (61 loc) · 2.25 KB
/
base.rb
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module ActiveRecord
class Base
@@rails_cache = nil
cattr_accessor :rails_cache
protected
def rails_cache
self.class.rails_cache
end
# Expire the cache for the associations which contains the given class.
#
# Example:
# class Blog < ActiveRecord::Base
# has_many :posts, :cached => true
# has_many :recent_posts, :class_name => 'Post',
# :limit => 10, :order => 'id DESC', :cached => true
#
# has_many :readers, :class_name => 'Person'
# end
#
# If one of the most recent posts will be updated, #expire_cache_for
# will be invoked with the "Post" parameter, in order to expire the
# cache for the first to associations.
def expire_cache_for(class_name)
self.class.reflections.each do |name, reflection|
if reflection.options[:cached] and reflection.class_name == class_name
cache_delete(reflection)
end
end
end
def cache_read(reflection)
return unless cached_associations[reflection.name]
rails_cache.read(reflection_cache_key(reflection))
end
def cache_write(reflection, value)
cached_associations[reflection.name] = rails_cache.write(reflection_cache_key(reflection), value)
end
def cache_delete(reflection)
return unless cached_associations[reflection.name]
cached_associations[reflection.name] = !rails_cache.delete(reflection_cache_key(reflection))
end
def cache_fetch(reflection, value)
reflection_name, key = extract_options_for_cache(reflection)
cached_associations[reflection_name] = true
rails_cache.fetch(key) { value }
end
def extract_options_for_cache(reflection)
if reflection.is_a?(AssociationReflection)
[ reflection.name, reflection_cache_key(reflection) ]
else
[ reflection.split('/').last, reflection ]
end
end
def reflection_cache_key(reflection)
"#{cache_key}/#{reflection.name}"
end
def cached_associations
cached_associations = (Thread.current[:cached_associations] ||= {})
cached_associations[cache_key] ||= {}
end
end
end