【问题标题】:Rails.cache.fetch before hook钩子前的 Rails.cache.fetch
【发布时间】:2014-12-18 15:06:53
【问题描述】:

是否有任何类型的 before_filter 等价物可以与 Rails.cache.fetch 一起使用?

我在运行多个构建的测试环境中使用 memcached,并希望设置一种方法来基于全局级别的环境变量修改我的密钥,而不是触及对 Rails.cache.fetch 的每一次调用。

如果有其他我目前不知道的解决方法,我愿意接受建议。

【问题讨论】:

    标签: ruby-on-rails ruby memcached


    【解决方案1】:

    您可以使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 2021-02-09
      • 1970-01-01
      相关资源
      最近更新 更多