public
Fork of defunkt/cache_fu
Description: Everyone's favorite memcached plugin for ActiveRecord.
Homepage: http://errtheblog.com
Clone URL: git://github.com/technoweenie/cache_fu.git
defunkt (author)
Tue Jan 22 16:27:07 -0800 2008
cache_fu / lib / acts_as_cached / local_cache.rb
100644 45 lines (37 sloc) 1.236 kb
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
module ActsAsCached
  module LocalCache
    @@local_cache = {}
    mattr_accessor :local_cache
 
    def fetch_cache_with_local_cache(*args)
      @@local_cache[cache_key(args.first)] ||= fetch_cache_without_local_cache(*args)
    end
 
    def set_cache_with_local_cache(*args)
      @@local_cache[cache_key(args.first)] = set_cache_without_local_cache(*args)
    end
 
    def expire_cache_with_local_cache(*args)
      @@local_cache.delete(cache_key(args.first))
      expire_cache_without_local_cache(*args)
    end
    alias :clear_cache_with_local_cache :expire_cache_with_local_cache
    
    def cached_with_local_cache?(*args)
      !!@@local_cache[cache_key(args.first)] || cached_without_local_cache?(*args)
    end
 
    def self.add_to(klass)
      return if klass.ancestors.include? self
      klass.send :include, self
 
      klass.class_eval do
        %w( fetch_cache set_cache expire_cache clear_cache cached? ).each do |target|
          alias_method_chain target, :local_cache
        end
      end
    end
  end
end
 
module ActionController
  class Base
    def local_cache_for_request
      ActsAsCached::LocalCache.add_to ActsAsCached::ClassMethods
      ActsAsCached::LocalCache.local_cache = {}
    end
  end
end