aaronrussell
aaronrussell

Reputation: 9477

How to make Rails caches_page survive a capistrano deploy?

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?

Upvotes: 4

Views: 972

Answers (3)

Marcel Cary
Marcel Cary

Reputation: 1

I found this was sufficient to have the public/cache directory symlinked under shared:

set :shared_children, shared_children + ["public/cache"]

Upvotes: 0

Anton
Anton

Reputation: 2483

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"

Upvotes: 4

Chris
Chris

Reputation: 1836

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.

Upvotes: 1

Related Questions