【问题标题】:is it possible to specify a cache with fragment caching or determine cache hit / miss ratio for a specific call是否可以使用片段缓存指定缓存或确定特定调用的缓存命中/未命中率
【发布时间】:2021-04-20 23:26:24
【问题描述】:

我们有一个 Rails 4.2 应用程序,目前正在多个应用程序中使用共享缓存。我们的 memcached 未命中率非常高(例如 85% 的命中率和 15% 的未命中率),但由于多个应用程序共享同一个 memcached 实例,这一点变得复杂。因此,我们可能会在几个关键缓存进程中获得很高的未命中率(我们的 DataDog 数据会支持这一点)。

是否可以在片段缓存调用中指定缓存存储,例如:

cache(order, OrderCache) do
  # whatever
end 

我认为通过对象缓存可以做到这一点:

OrderCache =  ActiveSupport::Cache::MemCacheStore.new

是否有其他方法可以解开特定缓存操作的命中/未命中率?

【问题讨论】:

    标签: ruby-on-rails caching memcached


    【解决方案1】:

    也许这是一个复杂的想法,但我认为如果我正确理解您的问题,您可以制作自己的缓存存储包装器来决定使用哪个缓存存储。

    当调用缓存方法时,它最终会调用控制器上的read_fragmentwrite_fragment https://apidock.com/rails/AbstractController/Caching/Fragments/read_fragment,而这些方法调用cache_store.readcache_store.write

    然后,您可以拥有一个自定义缓存存储类,其中包含自定义 readwrite 方法,根据选项,将读取和写入委托给真正的缓存存储。

    class MyCacheStore < ...
      def initialize
        @my_catchall_store = ActiveSupport::Cache::MemCacheStore.new
        @my_order_store = ActiveSupport::Cache::MemCacheStore.new
      end
    
      def read(key, options)
        case options[:store]
        when :order_store then @my_order_store.read(key, options)
        else
          @my_catchall_store.read(key, options)
        end
      end
    
      # similar for .write
    

    然后你像...一样使用它

    cache(:order, store: :order_store) do
      # some code
    end
    
    cache(:something_else) do
      # some code
    end
    

    我不确定这是不是你要问的对不起。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2018-10-01
      • 2019-11-21
      • 1970-01-01
      相关资源
      最近更新 更多