Class ActiveSupport::Cache::Store
In: vendor/rails/activesupport/lib/active_support/cache.rb
Parent: Object

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

decrement   delete   delete_matched   exist?   fetch   increment   read   silence!   write  

Public Instance methods

[Source]

     # File vendor/rails/activesupport/lib/active_support/cache.rb, line 202
202:       def decrement(key, amount = 1)
203:         log("decrementing", key, amount)
204:         if num = read(key)
205:           write(key, num - amount)
206:         else
207:           nil
208:         end
209:       end

[Source]

     # File vendor/rails/activesupport/lib/active_support/cache.rb, line 181
181:       def delete(key, options = nil)
182:         log("delete", key, options)
183:       end

[Source]

     # File vendor/rails/activesupport/lib/active_support/cache.rb, line 185
185:       def delete_matched(matcher, options = nil)
186:         log("delete matched", matcher.inspect, options)
187:       end

[Source]

     # File vendor/rails/activesupport/lib/active_support/cache.rb, line 189
189:       def exist?(key, options = nil)
190:         log("exist?", key, options)
191:       end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/cache.rb, line 128
128:       def fetch(key, options = {})
129:         @logger_off = true
130:         if !options[:force] && value = read(key, options)
131:           @logger_off = false
132:           log("hit", key, options)
133:           value
134:         elsif block_given?
135:           @logger_off = false
136:           log("miss", key, options)
137: 
138:           value = nil
139:           seconds = Benchmark.realtime { value = yield }
140: 
141:           @logger_off = true
142:           write(key, value, options)
143:           @logger_off = false
144: 
145:           log("write (will save #{'%.2f' % (seconds * 1000)}ms)", key, nil)
146: 
147:           value
148:         end
149:       end

[Source]

     # File vendor/rails/activesupport/lib/active_support/cache.rb, line 193
193:       def increment(key, amount = 1)
194:         log("incrementing", key, amount)
195:         if num = read(key)
196:           write(key, num + amount)
197:         else
198:           nil
199:         end
200:       end

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.

[Source]

     # File vendor/rails/activesupport/lib/active_support/cache.rb, line 158
158:       def read(key, options = nil)
159:         log("read", key, options)
160:       end

[Source]

    # File vendor/rails/activesupport/lib/active_support/cache.rb, line 83
83:       def silence!
84:         @silence = true
85:         self
86:       end

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

[Source]

     # File vendor/rails/activesupport/lib/active_support/cache.rb, line 177
177:       def write(key, value, options = nil)
178:         log("write", key, options)
179:       end

[Validate]