是否有任何类型的before_filter等效项可以与Rails.cache.fetch一起使用?
我在运行多个构建的测试环境中使用memcached,并希望设置一种方法来根据全局级别的环境变量修改我的密钥,而不是每次调用Rails.cache.fetch。< / p>
如果有其他方法可以解决这个我目前尚未发现的问题,我愿意接受建议。
答案 0 :(得分:0)
您可以使用namespace
选项或设置环境变量RAILS_CACHE_ID
RAILS_APP_VERSION
来附加缓存键名称。
来自rails/activesupport/lib/active_support/cache.rb
# 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}/" : ""
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