Friday, March 21, 2008

How to define the pages-cache directory onfly

Generally, in Rails, when you have a deal with the page-cache feature, you define the cache output directory (if define at all) in the environment.rb, something like that

config.action_controller.page_cache_directory = RAILS_ROOT + "/public/cache/"

But sometimes you may need to add something special to the path on fly, when the controller is working. Say a language prefix or so. And you can do so in a nice one-line patch. You should overload the 'cache_page' method (not the 'caches_page' one), like that

def cache_page(content=nil, path=nil)
super content || response.body, path || (@language_name + request.path)
end

And you won't need to touch anything else, case all your controller caching settings will eventually call the method. Just notice, that the method should be public. And don't forget to add your hacky prefix to your sweepers.

That's pretty much it.

No comments: