Who would have thought that this site would need to use page caching? Well, to be honest, it doesn’t. The one Mongrel cluster was rendering the pages absolutely fine. The site doesn’t get a whole heap of hits. But after watching the Rails Lab screencasts by Gregg Pollack on tuning and optimizing Rails applications, I decided to give it a try.

The Advanced Rails Recipes has two good recipes, Preserve Files Between Deployments and Segregate Page Cache Storage with Nginx, which along with screencasts helped a lot. I was only ever going to venture into Page Caching as the posts and the pages remain static.

All in all it was really easy. I knew about symlinking using Capistrano so it was just a case of setting my Enki fork to use page cache for the index and show actions for posts and pages. The problem I had wasn’t with the caching, which worked fine, it was with expiring the pages if they are changed. Enki has the administration controllers in a sub-folder so for some reason I couldn’t just use expire_page post_path(post) in the post sweeper, to get around that I just had to build up a string matching the post path manually, expire_page "/#{year}/#{month}/#{day}/#{slug}". There is no doubt a way around that using routes, but I haven’t found it. So if I add a post, it will zap the main index page, the archives page, the tags page and also the page of any tags that are associated with the new post. Simple. The only other problem was with the Nginx config settings. I had to change the ones in the book slightly to use $document_root instead.

Before I deployed the caching changes I added the RPM Lite from New Relic to see the response times. Apart from the occasional, self-induced, spike it was all running fine. The /posts/show controller action was taking the most time, essentially the rendering, but looking at the stats post-caching the slowest controller action is the sitemap, and I guess I could cache that.

Now that the site is running completely from static HTML pages it makes me wonder why I bothered setting up the database, Rails app, capistrano, directories etc when I could have just run it on my iMac and rsynced the changes up to the server. In fact that’s really how this site started. I just had an index page with server-side includes dragging in dated post entries.

When Brent Simmons described his publishing system earlier this year I found it really inspiring. No databases, no code (as least running on the server), easy to backup, just templates and scripts run locally and synced to the web-server. That’s all you need. Nothing big, nothing fancy. Of course, I wanted to learn more about Rails by using it on a live site… and it has been lots of fun.

  • Posted on Wednesday, 11 March 2009
  • Tagged with rails