Ever since I started using blurt for my blog, I have wanted to add page caching. Patrick, the author, thinks it is premature optimization -- especially since no one reads our blogs anyway (or at least mine). My view is that performance really stinks on a 256 MB shared slice. By implementing page caching, pages that used to take between a few hundred ms and a few seconds, are now instantaneous. About as digg safe as a single, puny, server can be!

Getting it working wasn't that easy, though. I followed the usual suspects for page caching, but kept running into weird issues with the sweepers. We were originally frozen at Rails 2.1.1, and I think most of the obvious problems were fixed by upgrading to 2.2.2. Once things appeared working, I deployed to production only to be greeted with new issues. Seems I forgot to configure my developer Passenger instance to use a sub URI like I'm doing in production.

The first bit of Google-delivered help was Paul Gross's article on using Mephisto with Phusion Passenger with a custom cache directory. Of course, that's for a website without a sub-URI. Before I could address that issue, I needed to fix a Rails 2.2.2 problem. It pays to read the release notes, and luckily for me someone else had.

First, add the following to your appropriate environment. If you are doing things right, it should be in config/environment.rb and not config/environments/production.rb:

  config.action_controller.relative_url_root = "<sub-uri>"

Next, remove "config.action_view.cache_template_loading = true" from config/environments/production.rb.

Now you are ready to engage in mod_rewrite hacking! A useful trick is to actually log what mod_rewrite is doing. Add the following lines to your apache config (virtual host or other):

        RewriteLog /var/log/apache2/<domain>-rewrite.log
        RewriteLogLevel 3

After playing around for a bit, Paul's rewrite code turned into:

        RailsAllowModRewrite on
        RewriteEngine on
        RewriteRule ^/<sub-uri>/?$ /<sub-uri>/cache/index.html [QSA]
        RewriteRule ^/<sub-uri>/([^.]+?)/?$ /<sub-uri>/cache/$1.html [QSA]
        RewriteCond %{DOCUMENT_ROOT}/<sub-uri>/cache/%{REQUEST_FILENAME} -f
        RewriteRule ^/<sub-uri>/(.*)$ /<sub-uri>/cache/$1 [L]

Hopefully this helps prevent someone from futzing around in circles for a few hours.