Sweeping strategies in Rails

Monday, January 09

The caching mechanism in Rails is delightfully simple: add a caches_page declaration for a given controller action and a static copy of the rendered view is automatically saved to disk. Subsequent requests for the page are served without invoking Rails. As many have noted, sweeping the cache can be a pain.

Enter two recent articles that discuss some practical sweeping strategies: Lazily sweeping the whole Rails page cache from Thijs van der Vossen, and Rick Olson’s Referenced Page Caching

Both strategies advocate relocating the cache directory to a subdirectory of public (i.e. public/cache), a brilliantly simple solution that makes complete expiry of the cache trivial. I especially like Thijs’ laziness: FileUtils.rm_r(self.page_cache_directory)

Comments

Leave a response