views:

122

answers:

2

Is it possible to configure Rails so caches created with caches_page survive a Capistrano deploy? Ie, can I configure the cache to be saved into a shared directory rather than in the public directory?

+1  A: 

Capistrano isn't really Rails related, it's just commonly used by the Rails community for deployment. So no, you can't "configure Rails" to do what you want. What you can do is add a task to your Capfile that runs shell commands to copy the cache into your new deployment before it is symlinked as 'current'.

namespace :deploy do
   desc "Copy cache to the new release"
   task :cache_copy, :roles => :app, :on_error => :continue do
     on_rollback {
       run "rm -rf #{latest_release}/public/cache"
     }

     run "cp -a #{current_path}/public/cache #{latest_release}/public"  
   end
end

before "deploy:symlink", "deploy:cache_copy"

But I really don't think you'd want to do such a thing for cached pages because the cache will likely be out of sync with the new code's output.

Chris
Thank you that is helpful. PS, as I mentioned in my comment above - I do understand why this is potentially a bad idea, but I do have a good reason :)
aaronrussell
+2  A: 

The accepted answer is OK, but it's generally better not to copy everything upon the deployment, but just symlink the cache folder.

This way, you can create your folder in shared/ directory and symlink it upon deployment like:

namespace :deploy do
   desc "Link cache folder to the new release"
   task :link_cache_folder, :roles => :app, :on_error => :continue do
     run "ln -s #{shared_path}/cache #{latest_release}/public/cache"  
   end
end

before "deploy:symlink", "deploy:link_cache_folder"
Anton