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 and has a cache_key
method
defined, this method will be called to define the key. Otherwise, the
to_param
method will be called. Hashes and Arrays can also be
used as keys. The elements will be delimited by slashes, and the elements
within a Hash 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 = -> { @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 overhead, values must be
large enough to warrant compression. To turn on compression either pass
compress: true
in the initializer or as an option to
fetch
or write
. To specify the threshold at which
to compress values, set the :compress_threshold
option. The
default threshold is 16K.