module ActiveSupport::Cache
def expand_cache_key(key, namespace = nil)
ActiveSupport::Cache.expand_cache_key([:foo, :bar], "namespace") # => "namespace/foo/bar"
ActiveSupport::Cache.expand_cache_key([:foo, :bar]) # => "foo/bar"
concatenated into a single key. For example:
each of elements in the array will be turned into parameters/keys and
If the +key+ argument provided is an array, or responds to +to_a+, then
scoped within that namespace.
cache store. Optionally accepts a namespace, and all keys will be
Expands out the +key+ argument into a key that can be used for the
def expand_cache_key(key, namespace = nil) expanded_cache_key = namespace ? +"#{namespace}/" : +"" if prefix = ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] expanded_cache_key << "#{prefix}/" end expanded_cache_key << retrieve_cache_key(key) expanded_cache_key end
def lookup_store(store = nil, *parameters)
ActiveSupport::Cache.lookup_store(MyOwnCacheStore.new)
If the first argument is not a Symbol, then it will simply be returned:
# => same as: ActiveSupport::Cache::FileStore.new('/tmp/cache')
ActiveSupport::Cache.lookup_store(:file_store, '/tmp/cache')
class's constructor:
Any additional arguments will be passed to the corresponding cache store
# => returns a new ActiveSupport::Cache::MemCacheStore object
ActiveSupport::Cache.lookup_store(:mem_cache_store)
# => returns a new ActiveSupport::Cache::MemoryStore object
ActiveSupport::Cache.lookup_store(:memory_store)
For example:
store class under the ActiveSupport::Cache namespace will be created.
If you pass a Symbol as the first argument, then a corresponding cache
ActiveSupport::Cache::MemoryStore object will be returned.
If no arguments are passed to this method, then a new
Creates a new Store object according to the given options.
def lookup_store(store = nil, *parameters) case store when Symbol options = parameters.extract_options! # clean this up once Ruby 2.7 support is dropped # see https://github.com/rails/rails/pull/41522#discussion_r581186602 if options.empty? retrieve_store_class(store).new(*parameters) else retrieve_store_class(store).new(*parameters, **options) end when Array lookup_store(*store) when nil ActiveSupport::Cache::MemoryStore.new else store end end
def retrieve_cache_key(key)
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
def retrieve_store_class(store)
Obtains the specified cache store class, given the name of the +store+.
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