Module: ActiveSupport::Cache
Overview
See Store for documentation.
Constant Summary
-
DEFAULT_COMPRESS_LIMIT =
# File 'activesupport/lib/active_support/cache.rb', line 501.kilobyte
-
DeserializationError =
# File 'activesupport/lib/active_support/cache.rb', line 54
Raised by coders when the cache entry can't be deserialized. This error is treated as a cache miss.
Class.new(StandardError)
-
OPTION_ALIASES =
# File 'activesupport/lib/active_support/cache.rb', line 46
Mapping of canonical option names to aliases that a store will recognize.
{ expires_in: [:expire_in, :expired_in].freeze }.freeze -
UNIVERSAL_OPTIONS =
# File 'activesupport/lib/active_support/cache.rb', line 29
These options mean something to all cache implementations. Individual cache implementations may support additional options.
[ :coder, :compress, :compress_threshold, :compressor, :expire_in, :expired_in, :expires_in, :namespace, :race_condition_ttl, :serializer, :skip_nil, :raw, :max_key_size, ].freeze
Class Attribute Summary
Class Method Summary
-
.expand_cache_key(key, namespace = nil)
Expands out the
keyargument into a key that can be used for the cache store. -
.lookup_store(store = nil, *parameters)
Creates a new
Storeobject according to the given options. - .retrieve_cache_key(key) private
-
.retrieve_store_class(store)
private
Obtains the specified cache store class, given the name of the
store.
Class Attribute Details
.format_version (rw)
[ GitHub ]# File 'activesupport/lib/active_support/cache.rb', line 63
attr_accessor :format_version
Class Method Details
.expand_cache_key(key, namespace = nil)
Expands out the key argument into a key that can be used for the
cache store. Optionally accepts a namespace, and all keys will be
scoped within that namespace.
If the key argument provided is an array, or responds to to_a, then
each of elements in the array will be turned into parameters/keys and
concatenated into a single key. For example:
<code></code>([:foo, :bar]) # => "foo/bar"
<code>expand_cache_key</code>([:foo, :bar], "namespace") # => "namespace/foo/bar"
The key argument can also respond to cache_key or to_param.
# File 'activesupport/lib/active_support/cache.rb', line 124
def (key, namespace = nil) = namespace ? +"#{namespace}/" : +"" if prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] << "#{prefix}/" end << retrieve_cache_key(key) end
.lookup_store(store = nil, *parameters)
Creates a new Cache::Store object according to the given options.
If no arguments are passed to this method, then a new
Cache::MemoryStore object will be returned.
If you pass a ::Symbol as the first argument, then a corresponding cache
store class under the Cache namespace will be created.
For example:
<code>lookup_store</code>(:memory_store)
#### => returns a new {ActiveSupport::Cache::MemoryStore} object
<code>lookup_store</code>(:mem_cache_store)
#### => returns a new {ActiveSupport::Cache::MemCacheStore} object
Any additional arguments will be passed to the corresponding cache store class's constructor:
<code>lookup_store</code>(:file_store, '/tmp/cache')
#### => same as: {ActiveSupport::Cache::FileStore.new}('/tmp/cache')
If the first argument is not a Symbol, then it will simply be returned:
<code>lookup_store</code>(MyOwnCacheStore.new)
#### => returns {MyOwnCacheStore.new}
# File 'activesupport/lib/active_support/cache.rb', line 96
def lookup_store(store = nil, *parameters) case store when Symbol = parameters. retrieve_store_class(store).new(*parameters, **) when Array lookup_store(*store) when nil ActiveSupport::Cache::MemoryStore.new else store end end
.retrieve_cache_key(key) (private)
[ GitHub ]# File 'activesupport/lib/active_support/cache.rb', line 136
def retrieve_cache_key(key) case when key.respond_to?(:cache_key_with_version) then key.cache_key_with_version when key.respond_to?(:cache_key) then key.cache_key when key.is_a?(Array) then key.map { |element| retrieve_cache_key(element) }.to_param when key.respond_to?(:to_a) then retrieve_cache_key(key.to_a) else key.to_param end.to_s end
.retrieve_store_class(store) (private)
Obtains the specified cache store class, given the name of the store.
Raises an error when the store class cannot be found.
# File 'activesupport/lib/active_support/cache.rb', line 148
def retrieve_store_class(store) # require_relative cannot be used here because the class might be # provided by another gem, like redis-activesupport for example. require "active_support/cache/#{store}" rescue LoadError => e raise "Could not find cache store adapter for #{store} (#{e})" else ActiveSupport::Cache.const_get(store.to_s.camelize) end