Rails 5、Capistrano 3部署后如何清除缓存

Rails 5, Capistrano 3 how to clear cache after deploy

我想执行相当于

的上限
Rails.cache.clear

部署后,但无法正常工作。这是我在 deploy.rb 文件

中的尝试
namespace :deploy do
    after :restart, :clear_cache do
        on release_roles(fetch(:assets_roles)) do
            within release_path do
                with rails_env: fetch(:rails_env) do
                    Rails.cache.clear
                end
            end
        end
    end
end

但这行不通:

SSHKit::Runner::ExecuteError: Exception while executing as deploy@hostname.com  uninitialized constant Rails

如果不是这个,那是什么?

感谢您的帮助, 凯文

更新:

这是正确的语法:

namespace :deploy do
    task :clear_cache do
        on roles(:app) do |host|
            with rails_env: fetch(:rails_env) do
                within current_path do
                    execute :rake, "cache:clear"
                end
            end
        end
    end
end

我建议您应该创建一个 rake 任务来清除缓存并使用 capistrano 挂钩调用它们。例如:

lib/tasks/cache.rb

namespace :cache do
  desc 'clear rails cache'
  task clear: :environment do
    Rails.cache.clear
  end
end

config/deploy.rb

namespace :cache do
  task :clear do
    on roles(:app) do |host|
      with rails_env: fetch(:rails_env) do
        within current_path do
          execute :bundle, :exec, "rake cache:clear"
        end
      end
    end
  end
end

after 'deploy:update', 'cache:clear'