83174d3a85e4ab78bc7f170e5a23178a6f96a0c0
4 # See ActiveSupport::Cache::Store for documentation.
6 autoload
:FileStore, 'active_support/cache/file_store'
7 autoload
:MemoryStore, 'active_support/cache/memory_store'
8 autoload
:SynchronizedMemoryStore, 'active_support/cache/synchronized_memory_store'
9 autoload
:DRbStore, 'active_support/cache/drb_store'
10 autoload
:MemCacheStore, 'active_support/cache/mem_cache_store'
11 autoload
:CompressedMemCacheStore, 'active_support/cache/compressed_mem_cache_store'
14 autoload
:LocalCache, 'active_support/cache/strategy/local_cache'
17 # Creates a new CacheStore object according to the given options.
19 # If no arguments are passed to this method, then a new
20 # ActiveSupport::Cache::MemoryStore object will be returned.
22 # If you pass a Symbol as the first argument, then a corresponding cache
23 # store class under the ActiveSupport::Cache namespace will be created.
26 # ActiveSupport::Cache.lookup_store(:memory_store)
27 # # => returns a new ActiveSupport::Cache::MemoryStore object
29 # ActiveSupport::Cache.lookup_store(:drb_store)
30 # # => returns a new ActiveSupport::Cache::DRbStore object
32 # Any additional arguments will be passed to the corresponding cache store
33 # class's constructor:
35 # ActiveSupport::Cache.lookup_store(:file_store, "/tmp/cache")
36 # # => same as: ActiveSupport::Cache::FileStore.new("/tmp/cache")
38 # If the first argument is not a Symbol, then it will simply be returned:
40 # ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
41 # # => returns MyOwnCacheStore.new
42 def self.lookup_store(*store_option
)
43 store
, *parameters
= *([ store_option
].flatten
)
47 store_class_name
= (store
== :drb_store ? "DRbStore" : store
.to_s
.camelize
)
48 store_class
= ActiveSupport
::Cache.const_get(store_class_name
)
49 store_class
.new(*parameters
)
51 ActiveSupport
::Cache::MemoryStore.new
57 def self.expand_cache_key(key
, namespace
= nil)
58 expanded_cache_key
= namespace
? "#{namespace}/" : ""
60 if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]
61 expanded_cache_key
<< "#{ENV["RAILS_CACHE_ID
"] || ENV["RAILS_APP_VERSION
"]}/"
64 expanded_cache_key
<< case
65 when key
.respond_to
?(:cache_key)
68 key
.collect
{ |element
| expand_cache_key(element
) }.to_param
76 # An abstract cache store class. There are multiple cache store
77 # implementations, each having its own additional features. See the classes
78 # under the ActiveSupport::Cache module, e.g.
79 # ActiveSupport::Cache::MemCacheStore. MemCacheStore is currently the most
80 # popular cache store for large production websites.
82 # ActiveSupport::Cache::Store is meant for caching strings. Some cache
83 # store implementations, like MemoryStore, are able to cache arbitrary
84 # Ruby objects, but don't count on every cache store to be able to do that.
86 # cache = ActiveSupport::Cache::MemoryStore.new
88 # cache.read("city") # => nil
89 # cache.write("city", "Duckburgh")
90 # cache.read("city") # => "Duckburgh"
92 cattr_accessor
:logger
99 # Fetches data from the cache, using the given key. If there is data in
100 # the cache with the given key, then that data is returned.
102 # If there is no such data in the cache (a cache miss occurred), then
103 # then nil will be returned. However, if a block has been passed, then
104 # that block will be run in the event of a cache miss. The return value
105 # of the block will be written to the cache under the given cache key,
106 # and that return value will be returned.
108 # cache.write("today", "Monday")
109 # cache.fetch("today") # => "Monday"
111 # cache.fetch("city") # => nil
112 # cache.fetch("city") do
115 # cache.fetch("city") # => "Duckburgh"
117 # You may also specify additional options via the +options+ argument.
118 # Setting <tt>:force => true</tt> will force a cache miss:
120 # cache.write("today", "Monday")
121 # cache.fetch("today", :force => true) # => nil
123 # Other options will be handled by the specific cache store implementation.
124 # Internally, #fetch calls #read, and calls #write on a cache miss.
125 # +options+ will be passed to the #read and #write calls.
127 # For example, MemCacheStore's #write method supports the +:expires_in+
128 # option, which tells the memcached server to automatically expire the
129 # cache item after a certain period. We can use this option with #fetch
132 # cache = ActiveSupport::Cache::MemCacheStore.new
133 # cache.fetch("foo", :force => true, :expires_in => 5.seconds) do
136 # cache.fetch("foo") # => "bar"
138 # cache.fetch("foo") # => nil
139 def fetch(key
, options
= {})
141 if !options
[:force] && value
= read(key
, options
)
143 log("hit", key
, options
)
147 log("miss", key
, options
)
150 ms
= Benchmark
.ms
{ value
= yield }
153 write(key
, value
, options
)
156 log('write (will save %.2fms)' % ms
, key
, nil)
162 # Fetches data from the cache, using the given key. If there is data in
163 # the cache with the given key, then that data is returned. Otherwise,
166 # You may also specify additional options via the +options+ argument.
167 # The specific cache store implementation will decide what to do with
169 def read(key
, options
= nil)
170 log("read", key
, options
)
173 # Writes the given value to the cache, with the given key.
175 # You may also specify additional options via the +options+ argument.
176 # The specific cache store implementation will decide what to do with
179 # For example, MemCacheStore supports the +:expires_in+ option, which
180 # tells the memcached server to automatically expire the cache item after
183 # cache = ActiveSupport::Cache::MemCacheStore.new
184 # cache.write("foo", "bar", :expires_in => 5.seconds)
185 # cache.read("foo") # => "bar"
187 # cache.read("foo") # => nil
188 def write(key
, value
, options
= nil)
189 log("write", key
, options
)
192 def delete(key
, options
= nil)
193 log("delete", key
, options
)
196 def delete_matched(matcher
, options
= nil)
197 log("delete matched", matcher
.inspect
, options
)
200 def exist
?(key
, options
= nil)
201 log("exist?", key
, options
)
204 def increment(key
, amount
= 1)
205 log("incrementing", key
, amount
)
207 write(key
, num
+ amount
)
213 def decrement(key
, amount
= 1)
214 log("decrementing", key
, amount
)
216 write(key
, num
- amount
)
223 def log(operation
, key
, options
)
224 logger
.debug("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}") if logger && !@silence && !@logger_off