memcached and cache_fu 8
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
endThe 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
endpretty 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.
Trackbacks
Use the following link to trackback from your own site:
http://blog.onmylist.com/articles/trackback/15
-
Buy viagra online. Viagra. Cheap viagra. Viagra without prescription. Compare stay erect and viagra. Viagra soft tabs. Re viagra cello.
-
hottest backseat bangers backseatbangers
-
Excellent post. Keep it up!
-
Excellent post. Keep it up!
-
Lipitor unusual side effect. Leg pain symptoms lipitor. Side effects of lipitor drugs. Lipitor generic. Generic version of lipitor. Lipitor.
-
Free viagra sample. Free sample of viagra.
Comments
-
This site was made to be a facebook application, no? I have no idea how to money-ma-tize that, but facebook applications are so hot right now. Forgive the off topic comment. I didn't really know where else to give feedback.
-
You should consider EngineYard for Rails hosting as well - they're really good - I'm a customer but I don't get anything for saying good things about them.
-
I have quite a high credit rating and was careless enough to get several credit cards for which I was pre-approved by email. Now I realize they are too many and I was late with payments for 2 of them. Fortunately, I got a balance transfer credit card and moved the most part of my debt there, as I found the advice and the application form at http://best-creditcar.cn/mastercard-platinum-payment-center.html >chase platinum mastercard payment center chase unverisal 4a3s2d
-
Caching certainly does help reduce load times.
-
I think this is an important story and would benefit from other NowPublic contributors working on it. I've flagged it as News Wanted and invite others in relevant locations to look for more evidence. http://levitra-for-sale.com " alt="levitra">Cheap levitra. http://buying-cialis.net ">Cialis pill.
-
http://cheapest-viagra.in.
-
used tractors sales on ebay.com
-
Your remove the 'Ruby-MemCache' solved my issue - its been a problem thats bugging me for ages. Thanks!