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 / config.rb
100644 94 lines (74 sloc) 2.643 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module ActsAsCached
  module Config
    extend self
 
    @@class_config = {}
    mattr_reader :class_config
 
    def valued_keys
      [ :store, :version, :pages, :per_page, :ttl, :finder, :cache_id, :find_by, :key_size ]
    end
 
    def setup(options)
      config = options['defaults']
 
      case options[RAILS_ENV]
      when Hash then config.update(options[RAILS_ENV])
      when String then config[:disabled] = true
      end
 
      config.symbolize_keys!
 
      setup_benchmarking! if config[:benchmarking] && !config[:disabled]
 
      setup_cache_store! config
      config
    end
 
    def setup_benchmarking!
      Benchmarking.inject_into_logs!
    end
 
    def setup_cache_store!(config)
      config[:store] =
        if config[:store].nil?
          setup_memcache config
        elsif config[:store].respond_to? :constantize
          config[:store].constantize.new
        else
          config[:store]
        end
    end
 
    def setup_memcache(config)
      config[:namespace] << "-#{RAILS_ENV}"
 
      silence_warnings do
        Object.const_set :CACHE, memcache_klass.new(config)
        Object.const_set :SESSION_CACHE, memcache_klass.new(config) if config[:session_servers]
      end
 
      CACHE.servers = Array(config.delete(:servers))
      SESSION_CACHE.servers = Array(config[:session_servers]) if config[:session_servers]
 
      setup_session_store if config[:sessions]
      setup_fragment_store! if config[:fragments]
      setup_fast_hash! if config[:fast_hash]
      setup_fastest_hash! if config[:fastest_hash]
 
      CACHE
    end
 
    def memcache_klass
      Object.const_defined?(:MemCacheWithConsistentHashing) ? MemCacheWithConsistentHashing : MemCache
    end
 
    def setup_session_store
      ActionController::Base.session_store = :mem_cache_store
      ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.update 'cache' => defined?(SESSION_CACHE) ? SESSION_CACHE : CACHE
    end
 
    def setup_fragment_store!
      ActsAsCached::FragmentCache.setup!
    end
 
    # break compatiblity with non-ruby memcache clients in exchange for speedup.
    # consistent across all platforms.
    def setup_fast_hash!
      def CACHE.hash_for(key)
        (0...key.length).inject(0) do |sum, i|
          sum + key[i]
        end
      end
    end
 
    # break compatiblity with non-ruby memcache clients in exchange for speedup.
    # NOT consistent across all platforms. Object#hash gives different results
    # on different architectures. only use if all your apps are running the
    # same arch.
    def setup_fastest_hash!
      def CACHE.hash_for(key) key.hash end
    end
  end
end