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.rb
100644 51 lines (39 sloc) 1.519 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
46
47
48
49
50
51
require 'acts_as_cached/config'
require 'acts_as_cached/cache_methods'
require 'acts_as_cached/fragment_cache'
require 'acts_as_cached/benchmarking'
require 'acts_as_cached/disabled'
require 'acts_as_cached/local_cache'
 
module ActsAsCached
  @@config = {}
  mattr_reader :config
 
  def self.config=(options)
    @@config = Config.setup options
  end
 
  def self.skip_cache_gets=(boolean)
    ActsAsCached.config[:skip_gets] = boolean
  end
 
  module Mixin
    def acts_as_cached(options = {})
      extend ClassMethods
      include InstanceMethods
 
      extend Extensions::ClassMethods if defined? Extensions::ClassMethods
      include Extensions::InstanceMethods if defined? Extensions::InstanceMethods
 
      options.symbolize_keys!
 
      options[:store] ||= ActsAsCached.config[:store]
      options[:ttl] ||= ActsAsCached.config[:ttl]
 
      # convert the find_by shorthand
      if find_by = options.delete(:find_by)
        options[:finder] = "find_by_#{find_by}".to_sym
        options[:cache_id] = find_by
      end
      
      cache_config.replace options.reject { |key,| not Config.valued_keys.include? key }
      cache_options.replace options.reject { |key,| Config.valued_keys.include? key }
 
      Disabled.add_to self and return if ActsAsCached.config[:disabled]
      Benchmarking.add_to self if ActsAsCached.config[:benchmarking]
    end
  end
 
  class CacheException < StandardError; end
  class NoCacheStore < CacheException; end
  class NoGetMulti < CacheException; end
end