action cache, , , , , , , , " />

MULTIMONGREL! Awesome Action Caching with Rails

January 23, 2009
playing pinball in Vegas reminded me of mongrel multi caching funtime

playing pinball in Vegas reminded me of mongrel multi caching funtime

We really like cache with our rails at Spongecell and have been really successful with Tom Fake’s Action Cache plugin when we are unable to page cache highly demanded content.

One little concurrency issue that I encountered running with the Action cache plugin on multiple mongrels was one mongrel would delete (expire) a meta data file while another was simultaneously writing the meta or data file. Anyhoo, the result was an angry corrupt file that was served to other web clients. This made some of customers angry. Then I was angry. To avoid all the hatred, I patched the method cached_entry in action_cache.rb with this little change to remove the cache on exceptions. action_cache.diff

          return nil

With that said, here are my favorite features which you will love too!

  • The timer expiry. So… you can be super lazy and just expire your cache say every 10 minutes without having to write complex sweeping code that may or may not slow down some other actions.
    @response.time_to_live = 10.minutes
  • Easy integration with  Nginx X-Accel-Redirect (and Apache’s X-SendFile) to tell your web server to send the cached file downstream. This allows you to still use before and after filters, but  offloads the work the Nginx and free up your mongrels a bit to bark at someone else.

For more information on rails caching, check out Gregg from Rail Envy’s sweet post.

Leave a Reply