memcached and cache_fu 8

Posted by Andrew Fri, 15 Jun 2007 04:12:00 GMT

After going live for about a week it became obvious that we should implement caching to help performance. I experimented with Rails’ page caching, but in Rails 1.2 the expire_cache helper does not deal with custom routes very well. Also, our pages have view counts, ratings, etc, that change way too often for us to be able to cache the entire page.

The next thing I looked at was caching the MySQL queries, this seemed easier than implementing fragment caching, coz I found a CachedModel tutorial and it looked very good. Took me about an hour to get everything with CachedModel working, I then looked at the verbose output of memcached and my development.log, it didn’t seem to be caching very much. After some more googling it turned out that CachedModel only caches find() queries, but we use custom finders in our models almost exclusively. To get it to cache more I had to manually cache and evict objects, which was kindda not fun.

So back to more googling, and I found a RailConf 2007 presentation by Chris Wanstrath on cache_fu, it was appropriately titled Kickin’ Ass with cache_fu. It seemed cool, and he talked about creating wrappers for custom finders, which was exactly what I needed. So I spent like an hour on it and moved from CachedModel to cache_fu.

I was all excited that it was so easy, almost too easy right? Yeah, I restarted my development server in Locomotive, loaded the main page, and was greeted with a “NoMethodError: protected method” error. It was pretty weird, I spent quite a while trying to figure it out by going through the cache_fu code, still couldn’t figure out it. I emailed Chris, who was extremely helpful and replied to my mails in lightning fast speed (thanks Chris!), he had never seen an error like that before either. After a while I went ahead and checked out cache_fu on our production server and ran the test suites that comes with it, it worked! Chris suspected that the problem had to do with Locomotive, I went ahead and installed a few gems to my global environment and ran the tests outside of the Locomotive sandbox, it worked! So after comparing the installed gem lists between Locomotive and MacPorts’ Ruby installation, I narrowed it down to the Ruby-MemCache gem. Uninstalled that, everything worked! So I thought I’d blog about this in case someone run into the same problem in the future. I have no idea when I installed that gem, I guess the name looked good so I installed it.

cache_fu is pretty neat, I created finders that check if there are cached version of the data and I’m using these new finders in our controllers, for example, here’s how the custom finder that locates recently updated lists looks like (I’ve removed some irrelevant code):

def List.recently_updated(category, find_conditions, 
  sort_column, sort_direction, limit, offset)

  find_conditions += 
    " AND (lists.is_private=0 OR lists.is_private IS NULL)"
  lists = List.find(:all, :conditions => find_conditions,
    :order => "updated_at DESC", :limit => limit, 
    :offset => offset)  

  lists = List.cached_perform_roster_sort(lists, 
    sort_column, sort_direction)

  lists
end

The cached version of it looks like this:

def List.cached_recently_updated(category, find_conditions, 
  sort_column, sort_direction, limit, offset)

  get_cache('recently_updated:' + category.to_s +
    conditions.to_s + sort_direction.to_s + 
    limit.to_s + offset.to_s) do

    recently_updated(category, find_conditions, 
      sort_column, sort_direction, limit, offset)
  end
end

pretty clean and simple! :) We’re testing the version of our site that uses memcached right now, if all goes well we will deploy it to the live site and hopefully you will enjoy a nice speedup.

Now we just have to tune the caching a bit, probably will add fragment caching to various places. Next big thing for me is to scale, right now we’re considering EC2 and Joyent.