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"
Methods
Public Instance methods
decrement(key, amount = 1)
     # File activesupport/lib/active_support/cache.rb, line 213
213:       def decrement(key, amount = 1)
214:         log("decrementing", key, amount)
215:         if num = read(key)
216:           write(key, num - amount)
217:         else
218:           nil
219:         end
220:       end
delete(key, options = nil)
     # File activesupport/lib/active_support/cache.rb, line 192
192:       def delete(key, options = nil)
193:         log("delete", key, options)
194:       end
delete_matched(matcher, options = nil)
     # File activesupport/lib/active_support/cache.rb, line 196
196:       def delete_matched(matcher, options = nil)
197:         log("delete matched", matcher.inspect, options)
198:       end
exist?(key, options = nil)
     # File activesupport/lib/active_support/cache.rb, line 200
200:       def exist?(key, options = nil)
201:         log("exist?", key, options)
202:       end
fetch(key, options = {}) {|| ...}

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
     # File activesupport/lib/active_support/cache.rb, line 139
139:       def fetch(key, options = {})
140:         @logger_off = true
141:         if !options[:force] && value = read(key, options)
142:           @logger_off = false
143:           log("hit", key, options)
144:           value
145:         elsif block_given?
146:           @logger_off = false
147:           log("miss", key, options)
148: 
149:           value = nil
150:           ms = Benchmark.ms { value = yield }
151: 
152:           @logger_off = true
153:           write(key, value, options)
154:           @logger_off = false
155: 
156:           log('write (will save %.2fms)' % ms, key, nil)
157: 
158:           value
159:         end
160:       end
increment(key, amount = 1)
     # File activesupport/lib/active_support/cache.rb, line 204
204:       def increment(key, amount = 1)
205:         log("incrementing", key, amount)
206:         if num = read(key)
207:           write(key, num + amount)
208:         else
209:           nil
210:         end
211:       end
read(key, options = nil)

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.

     # File activesupport/lib/active_support/cache.rb, line 169
169:       def read(key, options = nil)
170:         log("read", key, options)
171:       end
silence!()
    # File activesupport/lib/active_support/cache.rb, line 94
94:       def silence!
95:         @silence = true
96:         self
97:       end
write(key, value, options = nil)

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
     # File activesupport/lib/active_support/cache.rb, line 188
188:       def write(key, value, options = nil)
189:         log("write", key, options)
190:       end