Reputation: 23
So I have this expensive db query whose result won't change very often.
The thing is, the only thing I really care about for 95% of the time with this query is a 50 kb string of raw data.
Is it completely against "the RoR way"TM to use page caching to cache the result of an expensive and oft-used query?
caching the resultset on the db server itself is a little overkill since I really only care about a few bits of data that I've .collect{}ed from the query result. And page caching is wasteful since several different pages make use of this data and would all be caching the same thing with only the view around it changing.
Upvotes: 2
Views: 1479
Reputation: 84134
Sounds like you want the rails cache
Rails.cache.fetch('cache_key') do
# calculations here
end
This either performs the calculations and stuffs the block's return value in the cache or reads the value from the cache.
You can either expire things explicitly (Rails.cache.delete) or set expiry times or use generational cache keys.
There are a variety of cache stores you can use, but one of the more common ones is memcsched. There are details on how you can configure the cache store here
Upvotes: 4