Determing File Paths for Action Caching in Rails

Posted by science on November 08, 2007

Path

I just spent some “quality time” figuring out how to programatically determine the location of an action cache file in Rails. “cache_action” is a great tool and when applied properly can really save cpu. To find the file locations for this cache you could clearly hack your way in, but it’s harder than with page caches, because the *machine name* is included in path. Hassles!

You can put this little code snippet into your Controller/Action someplace and it ought to make your world right. Note if you want to expire action caches individually, you can use the file name generated by this to manually delete the file too:

ActionCachePath = ::ActionController::Caching::Actions::ActionCachePath
# ":" is converted magically to "." by caching name generator, so we have to do that too.
cached_path = ActionCachePath.new(self).path.gsub(/:/, '.')
File::expand_path(File::join(RAILS_ROOT, 'tmp', 'cache', cached_path))+'.cache'

Of course if you change the location of your cache, you’ll have to adjust the above code appropriately. Enjoy.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments