Update: Check out my presentation from RailsConf 2007 for even more caching fun.
Tuesday night I gave a presentationish thing at the San Francisco Ruby Meetup on memcached. It was a lot of fun, and Microsoft hosted it. Microsoft hosting a Ruby meetup. Sun hiring the JRuby Crew. Anyone else feel like we’re being tricked?! Get me my tinfoil hat.
The meetup was great. Lots of cool people. Amazing questions and discussion. I wish you could have been there. You couldn’t, so I’m putting my slides up as a PDF. While I’ve included notes, there were so many great ideas and points raised during the presentation by Ruby dudes that you truly are getting the shaft. My apologies.
Much of my talk focused on memcached concepts, tricks, caveats, and experiences. I also used it as a selfish opportunity to promote a new plugin: acts_as_cached. We’re using this monster on Chowhound and Chow and it’s really working well for us. There are many places I would like to take it, many things I would like to improve, but it works. The tests all pass. Just, uh, ignore that one test I commented out. It doesn’t pass.
Some usage idioms can be gleaned from the README. There’s more code in there than in the presentation, which I tried to keep somewhat abstract. Ideas, people.
Get the presentation:
Install the plugin:
$ cd RAILS_ROOT
$ ruby script/plugin install \ svn://errtheblog.com/svn/plugins/acts_as_cached
It’ll try to help you out by telling you if it can’t find any dependencies. Here they are:
- memcached. Follow topfunky’s guide to get the daemon.
- memcache-client: $ sudo gem install memcache-client
When you install the plugin it copies memcached.yml into your config/ directory. Check it out and tweak the settings to your liking. Start your memcached daemon, start your Rails app, then get caching!
I realize memcached is a beast. Here’s the deal: if you have a great idiom or idea, post it in the comments. If you have a bug fix, complaint, or question: join the acts_as_cached mailing list. Thanks.
Cache away.
Here’s a caching idiom I use a lot:
http://evansweb.info/articles/2006/08/23/ruby-memcache-client-enhancement
Basically I’ve modified Cache.get so that it takes a block. The block is evaluated to generate the cache contents if the key lookup fails.
That was a helpful presentation. I realized that I don’t have to worry about caching at this point, but it was great to see that it is not that hard to use memcache when the time arrives.
Oh, we have battled with memcache and rails on a rather large codebase at work. We started out with cached_model and expanded it to also cache relations. It works pretty well, but it is not without problems… your take – to do all the caching explicitly might be a good alternative.. cached_model tries to do everything transparently.. but using memcache and invalidating is not always that transparent :P
The presentation is fabulous! And also the acts_as_cached plugin. Thanks
Really helpful pdf. Thanks!
best rails cache solution possible i’ve seen sofa. btw would be more impressed if the links on your site worked… http://require.errtheblog.com/filedetails.php?repname=plugins&path=%2Facts_as_cached%2FREADME but then I see it’s php so not so surprised! ;) rail on!
I see I’m 7 months out of date to post here, how bout an update – is this alive n’ kickin or supaceded?
Hey reevolver, check this e-mail. acts_as_cached 2.0 is coming real soon—look for it around RailsConf ‘07 in May.
hi thanks for nice work, i got a small confusion, i just started digging in your nice effort and planning to use it on my current project.
my confusion is, it seems like you have applied acts_as_cached on Object, instead of specific AR::Base or other stuffs. it has positive advantage if i wish to cache any object. but seems not necessary when i only care about AR objects. i rather prefer to explicitly mention act_as_cached on my cache able classes.
best wishes,
The svn link to the plugin seems to be broken: svn://errtheblog.com/svn/plugins/acts_as_cached
Found it: http://github.com/defunkt/cache_fu/tree/master
Any word on Rails 2.1/named_scope compatibility?
Chime in.