Search code examples
ruby-on-railsherokuhttp-caching

How to expire Rails conditional GET caching?


I'm using the stale? method to do conditional GET caching in my JSON API.

I use

@post = Post.find(params[:post_id])
if stale? @post
  #continue
end

AFAIK This uses the post's updated_at to make a cache key. Is there any way I can expire the cache entry for this item without doing post.touch? I'm using Heroku.


Solution

  • create a file in config/initializers called bust_cache.rb with following contents:

    ENV["RAILS_CACHE_ID"] = 'version1'
    

    If in future you wish to bust the cache again change the value to 'version2' etc. If you wish to bust the cache with every deploy use:

    ENV["RAILS_CACHE_ID"] = Time.now.to_s
    

    ***Note that this last strategy will not work with multiple dynos on Heroku or other such similar situations

    thanks to Nathan Kontny