Caching data to reduce database hits

Are there any downsides to doing something like this assuming Foo data
rarely changes?

class Foo < ActiveRecord::Base
class << self
def get_all_foo
@foo || Foo.find(:all)
end
end
end

Is it ok to cache this data so you don’t constantly reload it from the
database? How long does @foo last? Just for the length of one request
or until the server is restarted?

Thanks,
Aaron

(sorry if this message is a dupe, I tried posting through the google
interface first)

On 12/20/06, Aaron B. [email protected] wrote:

end
end

Is it ok to cache this data so you don’t constantly reload it from the
database? How long does @foo last? Just for the length of one request
or until the server is restarted?

I believe that this will only last for the duration of a request. If
you
want it to be persistant, you could put it into the environment.rb file
and
it would persist over many calls.

Probably a better way to do this is to use memcache though. There are a
number of good plugins that allow you to use this. A nice one by Chris
Wanstrath http://www.agilewebdevelopment.com/plugins/owner/166 can be
found at

http://www.agilewebdevelopment.com/plugins/acts_as_cached

Hope that helps