简体   繁体   English

在Rails 3.0 / 3.1中,我应该如何缓存应用程序范围内所需的不变数据?

[英]In Rails 3.0/3.1 how should I cache unchanging data that's needed application-wide?

I've got some reasonable complex data I need to use for a lot of requests. 我有一些合理的复杂数据需要用于很多请求。 It consists of a hash of different ActiveRecord model objects (though I could only cache the attributes I need instead of the entire object). 它由不同的ActiveRecord模型对象的哈希组成(虽然我只能缓存我需要的属性而不是整个对象)。

I need to access the data frequently and it seems naturally to cache it. 我需要经常访问数据,它似乎很自然地缓存它。 It may change infrequently, but not often. 它可能不经常改变,但不经常改变。 I would need to be able to reload the data if it did change, but in general the data seems best as cached. 如果数据发生了变化,我需要能够重新加载数据,但一般来说数据看起来最好是缓存的。

I'm not using memcached currently, and it seems like overkill to begin using for just one piece of data. 我目前没有使用memcached,开始使用只有一个数据似乎有些过分。

I have some ideas and have experimented with some things but I'd like to get a wider set of recommendations. 我有一些想法,并尝试了一些事情,但我想得到更广泛的建议。

class MyModel < ActiveRecord::Base
  def self.my_cached_hash
    @my_cached_hash ||= get_my_cached_hash_here
  end

  def self.reset_my_cached_hash
    @my_cached_hash = nil
  end
end

Memcached and memcache-client are the obvious ones but there are also other options like cachetastic . Memcached和memcache-client是显而易见的,但还有其他选项,如cachetastic Check out something like ruby toolbox to see what else the community likes... 看看像ruby工具箱之类的东西,看看社区还有什么喜欢......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM