Created
September 23, 2012 01:50
-
-
Save antonbaron/3768506 to your computer and use it in GitHub Desktop.
Simulates Rails cache tags invalidation. Allows using cache like Rails.cache.fetch [key, tag] do .... end and Rails.cache.clear_tag tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/cache_tag_invalidation.rb | |
module CacheTagInvalidation | |
puts <<-WARNING | |
Adding cache tag invalidation. | |
NOTICE that this will work only with MemoryStore on single process or with memcached store | |
WARNING | |
def write *args | |
if args.first.is_a?(Array) && args.first.size == 2 # Simulate tags only whan cache key is an array of 2 elements | |
key, tag = args.first | |
taglist_key = ["CacheTagList", tag] | |
keys = read(taglist_key) || [] | |
super taglist_key, keys << key | |
end | |
super | |
end | |
def clear_tag tag | |
taglist_key = ["CacheTagList", tag] | |
(read(taglist_key) || []).each do |k| | |
delete [k, tag] | |
end | |
delete taglist_key | |
end | |
end | |
Rails.cache.extend CacheTagInvalidation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment