Class: ActiveSupport::Cache::Store
- Defined in:
- lib/active_support/cache.rb
Overview
An abstract cache store class. There are multiple cache store implementations, each having its own additional features. See the classes under the ActiveSupport::Cache module, e.g. ActiveSupport::Cache::MemCacheStore. MemCacheStore is currently the most popular cache store for large production websites.
ActiveSupport::Cache::Store is meant for caching strings. Some cache store implementations, like MemoryStore, are able to cache arbitrary Ruby objects, but don’t count on every cache store to be able to do that.
cache = ActiveSupport::Cache::MemoryStore.new
cache.read("city") # => nil
cache.write("city", "Duckburgh")
cache.read("city") # => "Duckburgh"
Direct Known Subclasses
Instance Attribute Summary collapse
-
#logger_off ⇒ Object
(also: #logger_off?)
readonly
Returns the value of attribute logger_off.
-
#silence ⇒ Object
(also: #silence?)
readonly
Returns the value of attribute silence.
Instance Method Summary collapse
- #decrement(key, amount = 1) ⇒ Object
- #delete(key, options = nil) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #exist?(key, options = nil) ⇒ Boolean
-
#fetch(key, options = {}) ⇒ Object
Fetches data from the cache, using the given key.
- #increment(key, amount = 1) ⇒ Object
- #mute ⇒ Object
-
#read(key, options = nil) ⇒ Object
Fetches data from the cache, using the given key.
- #silence! ⇒ Object
-
#write(key, value, options = nil) ⇒ Object
Writes the given value to the cache, with the given key.
Instance Attribute Details
#logger_off ⇒ Object (readonly) Also known as: logger_off?
Returns the value of attribute logger_off.
94 95 96 |
# File 'lib/active_support/cache.rb', line 94 def logger_off @logger_off end |
#silence ⇒ Object (readonly) Also known as: silence?
Returns the value of attribute silence.
94 95 96 |
# File 'lib/active_support/cache.rb', line 94 def silence @silence end |
Instance Method Details
#decrement(key, amount = 1) ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/active_support/cache.rb', line 225 def decrement(key, amount = 1) log("decrementing", key, amount) if num = read(key) write(key, num - amount) else nil end end |
#delete(key, options = nil) ⇒ Object
204 205 206 |
# File 'lib/active_support/cache.rb', line 204 def delete(key, = nil) log("delete", key, ) end |
#delete_matched(matcher, options = nil) ⇒ Object
208 209 210 |
# File 'lib/active_support/cache.rb', line 208 def delete_matched(matcher, = nil) log("delete matched", matcher.inspect, ) end |
#exist?(key, options = nil) ⇒ Boolean
212 213 214 |
# File 'lib/active_support/cache.rb', line 212 def exist?(key, = nil) log("exist?", key, ) end |
#fetch(key, options = {}) ⇒ Object
Fetches data from the cache, using the given key. If there is data in the cache with the given key, then that data is returned.
If there is no such data in the cache (a cache miss occurred), then then nil will be returned. However, if a block has been passed, then that block will be run in the event of a cache miss. The return value of the block will be written to the cache under the given cache key, and that return value will be returned.
cache.write("today", "Monday")
cache.fetch("today") # => "Monday"
cache.fetch("city") # => nil
cache.fetch("city") do
"Duckburgh"
end
cache.fetch("city") # => "Duckburgh"
You may also specify additional options via the options
argument. Setting :force => true
will force a cache miss:
cache.write("today", "Monday")
cache.fetch("today", :force => true) # => nil
Other options will be handled by the specific cache store implementation. Internally, #fetch calls #read, and calls #write on a cache miss. options
will be passed to the #read and #write calls.
For example, MemCacheStore’s #write method supports the :expires_in
option, which tells the memcached server to automatically expire the cache item after a certain period. We can use this option with #fetch too:
cache = ActiveSupport::Cache::MemCacheStore.new
cache.fetch("foo", :force => true, :expires_in => 5.seconds) do
"bar"
end
cache.fetch("foo") # => "bar"
sleep(6)
cache.fetch("foo") # => nil
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/active_support/cache.rb', line 151 def fetch(key, = {}) @logger_off = true if ![:force] && value = read(key, ) @logger_off = false log("hit", key, ) value elsif block_given? @logger_off = false log("miss", key, ) value = nil ms = Benchmark.ms { value = yield } @logger_off = true write(key, value, ) @logger_off = false log('write (will save %.2fms)' % ms, key, nil) value end end |
#increment(key, amount = 1) ⇒ Object
216 217 218 219 220 221 222 223 |
# File 'lib/active_support/cache.rb', line 216 def increment(key, amount = 1) log("incrementing", key, amount) if num = read(key) write(key, num + amount) else nil end end |
#mute ⇒ Object
104 105 106 107 108 109 |
# File 'lib/active_support/cache.rb', line 104 def mute previous_silence, @silence = defined?(@silence) && @silence, true yield ensure @silence = previous_silence end |
#read(key, options = nil) ⇒ Object
Fetches data from the cache, using the given key. If there is data in the cache with the given key, then that data is returned. Otherwise, nil is returned.
You may also specify additional options via the options
argument. The specific cache store implementation will decide what to do with options
.
181 182 183 |
# File 'lib/active_support/cache.rb', line 181 def read(key, = nil) log("read", key, ) end |
#silence! ⇒ Object
96 97 98 99 |
# File 'lib/active_support/cache.rb', line 96 def silence! @silence = true self end |
#write(key, value, options = nil) ⇒ Object
Writes the given value to the cache, with the given key.
You may also specify additional options via the options
argument. The specific cache store implementation will decide what to do with options
.
For example, MemCacheStore supports the :expires_in
option, which tells the memcached server to automatically expire the cache item after a certain period:
cache = ActiveSupport::Cache::MemCacheStore.new
cache.write("foo", "bar", :expires_in => 5.seconds)
cache.read("foo") # => "bar"
sleep(6)
cache.read("foo") # => nil
200 201 202 |
# File 'lib/active_support/cache.rb', line 200 def write(key, value, = nil) log("write", key, ) end |