Time-based expiry of the Rails Page Cache
Rails page caching is a great way to cope with a large number of hits. However, it’s hard to use in a typical web application cos parts of the page need to be personalized.
Even then, there are quite a few use cases where one could do a page cache for partials! Especially when using AJAX, the rendered partial at times is the same for all users. For example, when showing “related content”, the content displayed is related to the current article and not the viewer. In such a case one could use a page cache.
The only problem now is expiring the page cache. Computing “related content” is usually a time intensive operation when calculated from the other way, i.e. new content is added, so expire all pages to which it might be related to (so that it will appear as their related content the next time the partial is generated). Thus expiring the cache using sweepers isn’t an option. In such cases, a time based expiry works very well.
For this I have written a simple script using which you can specify which directory should be purged and at what time intervals.
Just download the cachedel script and drop it into your RAILS_ROOT/scripts directory
Then configure your cron to invoke it periodically.
Example:
0 * * * * /var/www/apps/c2w_portal/current/script/cachedel "blogs" 120This will invoke the cachedel script every hour and the script will delete all files in the blogs cache that are older than 2 hours







