mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Making the retrieval of the cache store class a method, also wrote
comments for the expand_cache_key method.
This commit is contained in:
parent
607f136d52
commit
3182295ce2
1 changed files with 23 additions and 10 deletions
|
@ -56,16 +56,7 @@ module ActiveSupport
|
||||||
|
|
||||||
case store
|
case store
|
||||||
when Symbol
|
when Symbol
|
||||||
store_class_name = store.to_s.camelize
|
retrieve_store_class(store).new(*parameters)
|
||||||
store_class =
|
|
||||||
begin
|
|
||||||
require "active_support/cache/#{store}"
|
|
||||||
rescue LoadError => e
|
|
||||||
raise "Could not find cache store adapter for #{store} (#{e})"
|
|
||||||
else
|
|
||||||
ActiveSupport::Cache.const_get(store_class_name)
|
|
||||||
end
|
|
||||||
store_class.new(*parameters)
|
|
||||||
when nil
|
when nil
|
||||||
ActiveSupport::Cache::MemoryStore.new
|
ActiveSupport::Cache::MemoryStore.new
|
||||||
else
|
else
|
||||||
|
@ -73,6 +64,18 @@ module ActiveSupport
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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:
|
||||||
|
#
|
||||||
|
# expand_cache_key([:foo, :bar]) # => "foo/bar"
|
||||||
|
# expand_cache_key([:foo, :bar], "namespace") # => "namespace/foo/bar"
|
||||||
|
#
|
||||||
|
# The +key+ argument can also respond to +cache_key+ or +to_param+.
|
||||||
def expand_cache_key(key, namespace = nil)
|
def expand_cache_key(key, namespace = nil)
|
||||||
expanded_cache_key = namespace ? "#{namespace}/" : ""
|
expanded_cache_key = namespace ? "#{namespace}/" : ""
|
||||||
|
|
||||||
|
@ -94,6 +97,16 @@ module ActiveSupport
|
||||||
else key.to_param
|
else key.to_param
|
||||||
end.to_s
|
end.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Obtains the specified cache store class, given the name of the +store+.
|
||||||
|
# Raises an error when the store class cannot be found.
|
||||||
|
def retrieve_store_class(store)
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
# An abstract cache store class. There are multiple cache store
|
# An abstract cache store class. There are multiple cache store
|
||||||
|
|
Loading…
Reference in a new issue