如何在Rails中使用多个缓存? (真的) [英] How to use multiple caches in rails? (for real)

查看:64
本文介绍了如何在Rails中使用多个缓存? (真的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用2个缓存-内存中的默认缓存1个和内存缓存的1个,尽管抽象地(我认为)这2个无关紧要。

I'd like to use 2 caches -- the in memory default one and a memcache one, though abstractly it shouldn't matter (I think) which two.

内存中的默认设置是我要加载较小且很少更改的数据的位置。到目前为止,我一直在使用记忆。我从那里的数据库中保存了一堆域数据类型的东西,还有一些来自外部资源的小数据,每15分钟-1小时刷新一次。

The in memory default one is where I want to load small and rarely changing data. I've been using the memory one to date. I keep a bunch of 'domain data' type stuff from the database in there, I also have some small data from external sources that I refresh every 15 min - 1 hour.

我最近添加了内存缓存,因为我现在正在使用一些较大的资产。我的处理方式有点复杂,但这些容量较大〜千字节,数量相对较小(数百个),并且具有很高的可缓存性-它们会更改,但是每小时刷新一次可能会太多。这个集合可能会增加,但是会在所有主机之间共享。刷新很昂贵。

I recently added memcache because I'm now serving up some larger assets. Sort of complex how I got into this, but these are larger ~kilobytes, relatively small in quantity (hundreds), and highly cacheable -- they change, but a refresh once per hour is probably too much. This set might grow, but it's shared across all hosts. Refreshes are expensive.

第一组数据现在已经使用默认内存缓存已有一段时间了,并且表现良好。 Memcache非常适合第二组数据。

The first set of data has been using the default memory cache for a while now, and has been well-behaved. Memcache is perfect for the second set of data.

我已经调整了Memcache,它非常适合第二组数据。问题在于,由于我的现有代码已经考虑在本地内存中,因此我对每个请求进行多次内存缓存访问,这增加了我的延迟。

I've tuned memcache, and it's working great for the second set of data. The problem is that because of my existing code that was done 'thinking' it was in local memory, I'm doing several trips to memcache per request, which is increasing my latency.

因此,我想使用2个缓存。有想法吗?

So, I want to use 2 caches. Thoughts?

(注意:memcache在与服务器不同的计算机上运行。即使我在本地运行,我也拥有大量的主机,因此它不会虽然不是所有人都知道的,但我也想避免只用一台更大的机器,即使我可能可以通过增大内存并仅使用内存来解决这个问题(数据确实不是那么大),在我扩展时并不能解决问题,因此只能解决问题。)

(note: memcache is running on different machine(s) than my server. Even if I ran it locally, I have a fleet of hosts so it wouldn't be local to all. Also, I want to avoid needing to just get bigger machines. Even though I probably could solve this problem by making the memory bigger and just using the in memory (the data really isn't that big), this doesn't solve the problem as I scale, so it will just be kicking the can.)

推荐答案

ActiveSupport :: Cache: :MemoryStore是您要使用的。 Rails.cache使用MemoryStore,FileStore或在我的情况下使用DalliStore:-)

ActiveSupport::Cache::MemoryStore is what you want to use. Rails.cache uses either MemoryStore, FileStore or in my case DalliStore :-)

您可以拥有ActiveSupport :: Cache :: MemoryStore的全局实例并使用它或创建一个具有保持此对象(更清洁)的单例模式的类。将Rails.cache设置到其他缓存存储区,并将此单例用于MemoryStore

You can have global instance of ActiveSupport::Cache::MemoryStore and use it or create a class with a singleton pattern that holds this object (cleaner). Set Rails.cache to the other cache store and use this singleton for MemoryStore

以下是此类:

module Caching
  class MemoryCache
      include Singleton

      # create a private instance of MemoryStore
      def initialize
        @memory_store = ActiveSupport::Cache::MemoryStore.new
      end

      # this will allow our MemoryCache to be called just like Rails.cache
      # every method passed to it will be passed to our MemoryStore
      def method_missing(m, *args, &block)
        @memory_store.send(m, *args, &block)
      end
  end
end

使用方法如下:

Caching::MemoryCache.instance.write("foo", "bar")
=> true
Caching::MemoryCache.instance.read("foo")
=> "bar"
Caching::MemoryCache.instance.clear
=> 0
Caching::MemoryCache.instance.read("foo")
=> nil
Caching::MemoryCache.instance.write("foo1", "bar1")
=> true
Caching::MemoryCache.instance.write("foo2", "bar2")
=> true
Caching::MemoryCache.instance.read_multi("foo1", "foo2")
=> {"foo1"=>"bar1", "foo2"=>"bar2"}

这篇关于如何在Rails中使用多个缓存? (真的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆