Jay Levitt
Jay Levitt

Reputation: 1720

Caching Rails models between requests - bad idea?

I have a complex query that's executed on every page and whose results rarely change, so I'd like to cache it in memcached and expire it manually when it's time to update it. The simplest way would be to cache the resulting model objects themselves. But I've seen vague warnings that Active Record models shouldn't be persisted between requests, because Bad Things Can Happen.

Is that true? Is there any decent write-up of the behavior of models between requests? And if that's a bad idea, what are some corresponding good ideas?

I know Devise uses ActiveSupport::Dependencies::Reference to cache references to classes, but I can't find any documentation on that anywhere, and I don't know if that's what I want or why.

Upvotes: 2

Views: 906

Answers (1)

apneadiving
apneadiving

Reputation: 115541

Caching queries is completely ok. Just keep in mind what you do.

One example can be found in heroku's documentation.

BTW keep in mind that Rails already do SQL caching.

Upvotes: 1

Related Questions