Cache Money - The bling bling painless way to do ActiveRecord object and collections caching

Most caching solutions in the Rails world involve something like Cache-Fu: an alternative API to ActiveRecord that explicitly annotates all call sites with cache rules.

  • User.find(1) becomes User.get_cache(1)
  • User.find(:all, ...) becomes User.get_cache("query_name", :ttl => 5.minutes) { User.find(:all, ... )}

I hate this kind of interface, which places the burden on the caller and meekly surrenders any attempt at encapsulation. Your codebase will be littered with haphazard cache rules in your controllers, views, and models.

This looks pretty awesome. Definitely going to investigate this some more.

Because manual cache expiry is not very fun. =)

views
3 responses
I don't know if cache expiry really has to be that hard. In terms of invalidation, we basically already do what this gem is doing:

We pretty heavy handedly invalidate the cache in after_save and after_destroy calls. So the code is still scattered a bit, but not terrible.

And this gem is only for object caching, which is good, but page or action would be better.

At the end of the day, i guess why not turn this on, and then also page cache on top as necessary. But i'm not sure i'm super impressed here.

I'd like to hear more opinion about a gem I wrote for this purpose: cached_models.

http://lucaguidi.com/projects/cached_models
http://lucaguidi.com/2009/02/17/getting-started-with-cachedmodels (video tutorial)

enjoy!

Much has been said about page and action caching. I like/need simple model caching and cache money looks like the answer. To start, it greatly simplifies code for caching models that rarely change.