aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2013-04-28 11:15:36 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2013-04-28 11:15:36 -0700
commit226de24fa2c9c402495287f5d8153e132759c8c8 (patch)
tree08f29cf57654de284d1cd4b233655025c543e569 /activesupport
parent457c79cbf3a856c3955bd1b707c3fbf8ea2ad23e (diff)
parent3182295ce2fa01b02cb9af0b977a9cf83cc5d9aa (diff)
downloadrails-226de24fa2c9c402495287f5d8153e132759c8c8.tar.gz
rails-226de24fa2c9c402495287f5d8153e132759c8c8.tar.bz2
rails-226de24fa2c9c402495287f5d8153e132759c8c8.zip
Merge pull request #10364 from wangjohn/refactoring_activesupport_cache
Making the retrieval of the cache store class a method
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/cache.rb33
1 files changed, 23 insertions, 10 deletions
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 2368e5ebd4..6c220ae625 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -56,16 +56,7 @@ module ActiveSupport
case store
when Symbol
- store_class_name = store.to_s.camelize
- 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)
+ retrieve_store_class(store).new(*parameters)
when nil
ActiveSupport::Cache::MemoryStore.new
else
@@ -73,6 +64,18 @@ module ActiveSupport
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)
expanded_cache_key = namespace ? "#{namespace}/" : ""
@@ -94,6 +97,16 @@ module ActiveSupport
else key.to_param
end.to_s
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
# An abstract cache store class. There are multiple cache store