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.
Some implementations may not support all methods beyond the basic cache methods of fetch, write, read, exist?, and delete.
ActiveSupport::Cache::Store can store any serializable Ruby object.
cache = ActiveSupport::Cache::MemoryStore.new cache.read("city") # => nil cache.write("city", "Duckburgh") cache.read("city") # => "Duckburgh"
Keys are always translated into Strings and are case sensitive. When an object is specified as a key, its cache_key method will be called if it is defined. Otherwise, the to_param method will be called. Hashes and Arrays can be used as keys. The elements will be delimited by slashes and Hashes elements will be sorted by key so they are consistent.
cache.read("city") == cache.read(:city) # => true
Nil values can be cached.
If your cache is on a shared infrastructure, you can define a namespace for your cache entries. If a namespace is defined, it will be prefixed on to every key. The namespace can be either a static value or a Proc. If it is a Proc, it will be invoked when each key is evaluated so that you can use application logic to invalidate keys.
cache.namespace = lambda { @last_mod_time } # Set the namespace to a variable @last_mod_time = Time.now # Invalidate the entire cache by changing namespace
Caches can also store values in a compressed format to save space and reduce time spent sending data. Since there is some overhead, values must be large enough to warrant compression. To turn on compression either pass :compress => true in the initializer or to fetch or write. To specify the threshold at which to compress values, set :compress_threshold. The default threshold is 32K.