在Heroku部署上清除Memcached

Sol*_*mon 33 memcached ruby-on-rails heroku ruby-on-rails-3

将rails应用程序部署到Heroku时,自动清除Memcached的最佳方法是什么?

我正在缓存主页,当我进行更改并重新部署时,页面将从缓存中提供,并且不会包含更新.

我希望这完全自动化.我不希望每次部署时都必须清除heroku控制台中的缓存.

谢谢!

Fab*_*ega 32

我使用bash脚本部署我的应用程序,该脚本自动执行GitHub和Heroku推送,数据库迁移,应用程序维护模式激活和缓存清除操作.

在此脚本中,清除缓存的命令是:

heroku run --app YOUR_APP_NAME rails runner -e production Rails.cache.clear
Run Code Online (Sandbox Code Playgroud)

这适用于Celadon Cedar和Heroku Toolbelt包.我知道这不是基于Rake的解决方案,但它非常有效.

注意:请确保将命令的environment/ -e选项设置runnerproduction,否则将在该命令上执行development.

编辑:我几天后就在Heroku上遇到过这个命令的问题(Rails 3.2.21).我没有时间检查问题的根源但是删除-e production了诀窍,所以如果命令不成功,请运行这个:

heroku run --app YOUR_APP_NAME rails runner Rails.cache.clear
Run Code Online (Sandbox Code Playgroud)

  • 我将此更改为正确的答案,因为它目前有效,不像最高投票的答案,它有一个旧的解决方案. (3认同)

Hol*_*est 23

[在青瓷雪松堆上]

- [2012年6月18日更新 - 这不再有效,将会看到我是否可以找到另一种解决方法]

我发现处理这些部署后挂钩的最简洁方法是锁定资产:在slug编译期间已经调用的预编译任务.点头对asset_sync Gem提出了这个想法:

Rake::Task["assets:precompile"].enhance do
  # How to invoke a task that exists elsewhere
  # Rake::Task["assets:environment"].invoke if Rake::Task.task_defined?("assets:environment")

  # Clear cache on deploy
  print "Clearing the rails memcached cache\n"
  Rails.cache.clear
end
Run Code Online (Sandbox Code Playgroud)

我把它放在一个lib/tasks/heroku_deploy.rake文件中,它很好地被拾取.


Sol*_*mon 8

我最终做的是创建一个新的rake任务,部署到heroku然后清除缓存.我创建了一个deploy.rake文件,就是这样:

namespace :deploy do

    task :production do
        puts "deploying to production"
        system "git push heroku"
        puts "clearing cache"
        system "heroku console Rails.cache.clear"
        puts "done"
    end

end
Run Code Online (Sandbox Code Playgroud)

现在,我只需输入rake deploy:production,而不是输入git push heroku.


Mik*_*ike 7

2013年1月25日:这适用于在Cedar上的Ruby 1.9.3上运行的Rails 3.2.11应用程序

在你Gemfile添加以下行强制ruby 1.9.3:

ruby '1.9.3'
Run Code Online (Sandbox Code Playgroud)

创建一个lib/tasks/clear_cache.rake以此内容命名的文件:

if Rake::Task.task_defined?("assets:precompile:nondigest")
  Rake::Task["assets:precompile:nondigest"].enhance do
    Rails.cache.clear
  end
else
  Rake::Task["assets:precompile"].enhance do
    # rails 3.1.1 will clear out Rails.application.config if the env vars
    # RAILS_GROUP and RAILS_ENV are not defined. We need to reload the
    # assets environment in this case.
    # Rake::Task["assets:environment"].invoke if Rake::Task.task_defined?("assets:environment")
    Rails.cache.clear
  end
end
Run Code Online (Sandbox Code Playgroud)

最后,我还建议heroku labs:enable user-env-compile在您的应用程序上运行,以便在预编译过程中为您提供其环境.