I’ve come across a situation in my little sandbox ruby on rails app that might get some people. I’m trying to add caching to my controllers using the “caches_page” method, which will capture the complete output of the action being cached and store it as an html file in your filesystem. It’s setup like so in your controller:
class UsersController < ApplicationController
<b>caches_page :find</b>
def find
@user = User.find(params[:id)
end
end
Without any monkeying around with the configs or the location of the cached output, it works great. I execute the controller the first time and see the SQL output in the debug log statements, and execute it again and see nothing (indicating it didn’t even hit the controller and pulled the page straight from the cache). Perfect. I can even see the html output in “public/users/find/1.html.
However, I moved the location on my filesystem of the cached output to a different directory by adding this line to “config/environments/development.rb”:
ActionController::Base.page_cache_directory = "/tmp/ror/cache"
Great, now when I hit the same page and then go look in the new cache directory I can see the same “users/find/1.html” file. However, upon closer inspection of my log files I see that it’s never pulling and using the cached file for output – obviously defeating the purpose of such an endeavor.
To understand the problem we have to understand how the caching works. All the “caches_page” call does is take the raw HTML output from the full execution of the action (filters and action) and put it on the filesystem in a place where WEBrick is looking for static files. By default they’re placed in the “public” directory, which is where WEBrick looks for static files first before handing off to RoR. Well, with my changed config that static place has moved, but WEBrick doesn’t know about it – hence the cache not being hit on identical requests.
So it appears there are limited options. I’ve looked in the “scripts/server” file that bootstraps WEBrick and tells where WEBrick should serve files from, and it appears that you can only specify one such directory:
OPTIONS = {
:port => 3000,
:ip => "0.0.0.0",
:environment => "development",
<b>:server_root => File.expand_path(File.dirname(__FILE__) + "/../public/"),</b>
:server_type => WEBrick::SimpleServer
}
So, adding another root to that path won’t work (to have WEBrick serve from both “public” and the new cache directory ”/tmp/ror/cache”). It would appear that our only viable options are to combine the cache and public directories into one physical location – either move the files in “public” to your cache directory or leave the config as it is out of the box and have your cached output sent to “public”. This is the option I’m going with for now until I can figure out a better solution, although it’s quite a pain to not commit the cache files into subversion after the cache has been used…

” ActionController::Base.page_cache_directory = ”/tmp/ror/cache” “
need changed to
” ActionController::Base.page_cache_directory = ”#{RAILS_ROOT}/tmp/cache/” “