Ruby-on-rails 部署到heroku后如何清除rails缓存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12028088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I clear rails cache after deploy to heroku?
提问by kikiki.blue
I applied cache to my heroku rails app and it works well. But everytime I deploy to heroku, I also want to clear cache automatically.
我将缓存应用于我的 heroku rails 应用程序,它运行良好。但是每次我部署到 heroku 时,我也想自动清除缓存。
so I googled and I found this.
所以我用谷歌搜索,我找到了这个。
task :after_deploy, :env, :branch do |t, args|
puts "Deployment Complete"
puts "Cleaning all cache...."
FileUtils.cd Rails.root do
sh %{heroku run console}
sh %{Rails.cache.clear}
end
end
but when I raked this script, it just show the heroku console command line but the Rails.cache.clear command does not typed. (I guess that is because heroku console is interactive)
但是当我抓取这个脚本时,它只显示了 heroku 控制台命令行,但没有输入 Rails.cache.clear 命令。(我猜这是因为 heroku 控制台是交互式的)
and
和
system "heroku console Rails.cache.clear"
doesn't work for cedar apps.
不适用于雪松应用程序。
How can I solve this problem?
我怎么解决这个问题?
Thanks.
谢谢。
回答by gabeodess
Rails has a built in rake task:
Rails 有一个内置的 rake 任务:
rake tmp:clear
回答by a.ross.cohen
The following should work on cedar:
以下应该适用于雪松:
heroku run console
..then wait 5 seconds for heroku console to boot
..然后等待 5 秒让 heroku 控制台启动
Rails.cache.clear
Then you should see the cache clear, and you can quit console. Remember you might have to refresh a few times because your local machine will often cache assets and such in your browser until it makes a fresh request.
然后你应该看到缓存清除了,你可以退出控制台。请记住,您可能需要刷新几次,因为您的本地计算机通常会在浏览器中缓存资产等,直到它发出新的请求。
If it happens to be assets that you're caching though, you don't need to go through a manual clear every time you push, you just need to have the asset pipeline set up and make sure all your js/css(less/sass)/static images are being compiled with hashes at the end of their filenames.
如果它恰好是您正在缓存的资产,则您不需要在每次推送时都进行手动清除,您只需要设置资产管道并确保您的所有 js/css(less/ sass)/静态图像在其文件名末尾使用散列进行编译。
回答by Ivar
You should be able to create a cache clearing rake task that looks something like this:
你应该能够创建一个缓存清除任务,看起来像这样:
namespace :cache do
desc "Clears Rails cache"
task :clear => :environment do
Rails.cache.clear
end
end
and invoke it directly in one command that you can use in your post deploy hook - like so:
并在您可以在部署后挂钩中使用的一个命令中直接调用它 - 如下所示:
heroku run rake cache:clear
回答by nthj
Ruby on Rails has a magical ENV variable called 'RAILS_CACHE_ID'. I set this to the git commit id whenever I deploy: heroku config:set RAILS_CACHE_ID=$CIRCLE_SHA1
Ruby on Rails 有一个神奇的 ENV 变量,称为“RAILS_CACHE_ID”。每当我部署时,我都会将其设置为 git commit id:heroku config:set RAILS_CACHE_ID=$CIRCLE_SHA1
回答by rvirani
If you want to simply run a rake task after deploy, I would recommend checking out:
如果您只想在部署后运行 rake 任务,我建议您查看:
https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks
https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks
We've been using it in production for over 6 months and it's been rock solid.
我们已经在生产中使用它超过 6 个月,并且它一直坚如磐石。
First, add the buildpack AFTER you already have the Ruby buildpack set. This should happen after your first deploy to the server
首先,在您已经设置好 Ruby 构建包之后添加构建包。这应该发生在您第一次部署到服务器之后
heroku buildpacks:add https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks
heroku buildpacks:add https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks
Second, set an environment variable called DEPLOY_TASKSwith just the names of the rake tasks you want to run separated by spaces.
其次,设置一个环境变量,该变量DEPLOY_TASKS仅使用要运行的 rake 任务的名称以空格分隔。
heroku config:set DEPLOY_TASKS='cache:clear
heroku config:set DEPLOY_TASKS='cache:clear
回答by Alexis Rabago Carvajal
heroku run rake tmp:cache:clear
heroku 运行 rake tmp:cache:clear
回答by Jonathan Mui
Heroku doesn't currently support a pipeline of actions to occur after deployment. You'll need something like Codeship or TravisCI to create a recipe of steps that happen during deployment
Heroku 目前不支持在部署后发生的一系列操作。您将需要 Codeship 或 TravisCI 之类的东西来创建部署期间发生的步骤的配方
Disclosure: I am a customer of Codeship.
披露:我是 Codeship 的客户。

