Icon_search
Profile

Rails Caching non HTML Files in a Different Cache Location

Posted on 05/08/2007 by Adam
 | 

We use page caching in Rails to speed up sites and improve performance. This is a great feature of Rails but can get cumbersome when trying to keep up with which pages to sweep and when. To make life easier we use a separate directory to house the cache. This solution is great for files that end in a .html extension but for other file types you need a new rewrite rule for mod_rewrite. The example below is for caching dynamic images.

RewriteRule ^([^"cache"].+)\.(png)$ cache/$1.png

I am rewriting any request for a .png to have the cache/ added to the beginning of the url. I am also checking to make sure the url hasn't already been rewritten otherwise Apache gets stuck in a loop rewriting the url. Depending on the application, you may need to add a slash in front of cache like so:

RewriteRule ^([^"cache"].+)\.(png)$ /cache/$1.png

Tagged:  ruby, on, rails, caching, server, performance