Kevin Bedell
Kevin Bedell

Reputation: 13404

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).

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.

I have some ideas and have experimented with some things but I'd like to get a wider set of recommendations.

Upvotes: 0

Views: 73

Answers (2)

Ahmish
Ahmish

Reputation: 1151

Memcached and memcache-client are the obvious ones but there are also other options like cachetastic. Check out something like ruby toolbox to see what else the community likes...

Upvotes: 1

Mori
Mori

Reputation: 27779

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

Upvotes: 1

Related Questions