Clear Memcached on Heroku Deploy

前端 未结 9 1725
旧巷少年郎
旧巷少年郎 2020-12-13 09:18

What is the best way to automatically clear Memcached when I deploy my rails app to Heroku?

I\'m caching the home page, and when I make changes and redeploy, the pag

相关标签:
9条回答
  • 2020-12-13 09:57

    I've added config/initializers/expire_cache.rb with

    ActionController::Base.expire_page '/'
    

    Works sweet!

    0 讨论(0)
  • 2020-12-13 10:06

    [On the Celadon Cedar Stack]

    -- [Update 18 June 2012 -- this no longer works, will see if I can find another workaround]

    The cleanest way I have found to handle these post-deploy hooks is to latch onto the assets:precompile task that is already called during slug compilation. With a nod to asset_sync Gem for the idea:

    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
    

    I just put this in a lib/tasks/heroku_deploy.rake file and it gets picked up nicely.

    0 讨论(0)
  • 2020-12-13 10:08

    The solution I like to use is the following:

    First, I implement a deploy_hook action that looks for a parameter that I set differently for each app. Typically I just do this on the on the "home" or "public" controller, since it doesn't take that much code.

    ### routes.rb ###
    
    post 'deploy_hook' => 'home#deploy'
    
    ### home_controller.rb ###
    
    def deploy_hook
      Rails.cache.clear if params[:secret] == "a3ad3d3"
    end
    

    And, I simply tell heroku to setup a deploy hook to post to that action whenever I deploy!

    heroku addons:add deployhooks:http \
       --url=http://example.com/deploy_hook?secret=a3ad3d3
    

    Now, everytime that I deploy, heroku will do an HTTP post back to the site to let me know that the deploy worked just fine.

    Works like a charm for me. Of course, the secret token not "high security" and this shouldn't be used if there were a good attack vector for taking your site down if caches were cleared. But, honestly, if the site is that critical to attack, then don't host it on Heroku! However, if you wanted to increase the security a bit, then you could use a Heroku configuration variable and not have the 'token' in the source code at all.

    Hope people find this useful.

    0 讨论(0)
提交回复
热议问题