Module: Cachier
- Extended by:
- Cachier
- Included in:
- Cachier
- Defined in:
- lib/cachier.rb,
lib/cachier/version.rb,
lib/cachier/matchers.rb,
lib/cachier/controller_helper.rb
Overview
Hooks into ApplicationController’s write_fragment method. write_fragment is used for action and fragment caching. Create an alias method chain to call our customer method which stores the associated key with the tag in a Redis Set. Then we can expire all those keys from anywhere in the code using Rails.cache.delete
I use alias_method_chain instead of calling ‘super’ because there is a very rare case where someone may have redfined ‘write_fragment’ in their own controllers. Using an alias method chain keeps those methods intact.
Defined Under Namespace
Modules: ControllerHelper, Matchers
Constant Summary
collapse
- CACHE_KEY =
'cachier-tags'
- VERSION =
"0.0.1"
Instance Method Summary
collapse
Instance Method Details
#clear ⇒ Object
59
60
61
62
|
# File 'lib/cachier.rb', line 59
def clear
expire(*tags)
Rails.cache.delete(CACHE_KEY)
end
|
#expire(*tags) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/cachier.rb', line 36
def expire(*tags)
return unless perform_caching?
tags.each do |tag|
if fragment_keys = Rails.cache.fetch(tag)
fragment_keys.each do |fragment_key|
Rails.cache.delete(fragment_key)
end
end
Rails.cache.delete(tag)
end
remove_tags_from_tag_list(tags)
end
|
#fetch(cache_key, cache_params = {}) ⇒ Object
Fetch from cache, this function accepts a block
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/cachier.rb', line 12
def fetch(cache_key, cache_params = {})
tags = cache_params.delete(:tag) || []
store_fragment(cache_key, tags)
if block_given?
returned_object = Rails.cache.fetch(cache_key, cache_params) {
yield
}
else
Rails.cache.read(cache_key)
end
end
|
#keys ⇒ Object
68
69
70
71
72
|
# File 'lib/cachier.rb', line 68
def keys
tags.inject([]) do |arry, tag|
arry += (Rails.cache.fetch(tag) || [])
end.compact
end
|
#keys_for(tag) ⇒ Object
74
75
76
|
# File 'lib/cachier.rb', line 74
def keys_for(tag)
Rails.cache.fetch(tag) || []
end
|
7
8
9
|
# File 'lib/cachier.rb', line 7
def perform_caching?
::ApplicationController.perform_caching
end
|
#store_fragment(fragment, *tags) ⇒ Object
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/cachier.rb', line 25
def store_fragment(fragment, *tags)
return unless perform_caching?
tags.each do |tag|
fragments = Rails.cache.fetch(tag) || []
Rails.cache.write(tag, fragments + [fragment])
end
add_tags_to_tag_list(tags)
end
|
55
56
57
|
# File 'lib/cachier.rb', line 55
def tags
Rails.cache.fetch(CACHE_KEY) || []
end
|
#wipe ⇒ Object
64
65
66
|
# File 'lib/cachier.rb', line 64
def wipe
clear
end
|