2

I am storing database query results in memcache on heroku. I am using memcachier addon on heroku. For example if I have a cache User's tasks in memcache. I do something like this:

 def cached_tasks
   Rails.cache.fetch([:users, id, :tasks], :expires_in => 12.hours) { tasks.to_a }  
 end

This works perfectly fine but I want to use two different memcache instances to store data.

Why?

One that is used very frequently, basically data changes frequently and another for those which are big data objects and those will never change or very rarely.

How can I use two different instances and specify that cached_tasks should be stored in memcache_instance_1 and other like cached_images should be stored in memcache_instance_2

Why not to use the same one:

Because sometimes I need to flush the whole cache and that will flush the big data too which I don't want to.

Any suggestions?

4

2 回答 2

3

env为您的 heroku 应用程序声明三个新变量

BIG_MEMCACHIER_SERVERS
BIG_MEMCACHIER_USERNAME
BIG_MEMCACHIER_PASSWORD

big_cache.rb在目录中添加一个名为的config\initializers文件:

module Rails
  def self.big_cache
    @big_cache ||= ActiveSupport::Cache::DalliStore.new(
      (ENV["BIG_MEMCACHIER_SERVERS"] || "").split(","),
      :username => ENV["BIG_MEMCACHIER_USERNAME"],
      :password => ENV["BIG_MEMCACHIER_PASSWORD"])
  end
end

现在您可以按如下方式访问第二个缓存:

Rails.big_cache.fetch(..)
于 2013-07-23T06:51:58.007 回答
0

我要做的是尝试从我正在缓存的东西中定义什么实际上是需要缓存的东西,或者它实际上需要一些寿命有限的持久性。

我会让 Rails 内部缓存系统做它最擅长的事情:页面、动作和片段缓存,并使用另一个持久性引擎(例如 Redis)来保存和维护你谈到的那些大对象(这个建议的实现完全不同的问题)。

请注意,Redis 还允许在键上设置 TTL(生存时间) - 因此它可以提供您想要的这种有限寿命的持久性。

于 2013-07-22T23:38:21.510 回答