How to get caches_action to set expires headers when there is a hit in memcached?

风流意气都作罢 提交于 2019-12-06 13:08:41

I had the very same problem a few days ago. Debugging it seems that rails is not taking into acount the expires_in for caches_action.

What I found that works is to put the same inside a cache_path. For example insted of doing

caches_action :monthly_sales_by_category, :expires_in => 10.minutes, :cache_path => proc { |c|
    category = c.params[:category]
    {:cat => category}
}

what I did was the following

caches_action :monthly_sales_by_category, :cache_path => proc { |c|
    expires_in 10.minutes, :public => false
    category = c.params[:category]
    {:cat => category} 
}

And works like a charm. :)

Make sure your environment is configured to support caching. So in config/environments/development.rb (or where ever) you should see:

config.action_controller.perform_caching = true

Also, the private cache tag tells intermediate cache servers not to store the content. This is secure by default. If you want to change this behaviour, just set your cache like this:

expires_in(1.hours, :private => false, :public => true)

In order to skip expensive processing on the server if the content has not changed, use:

if stale?(:etag => @model, :last_modified => @model.updated_at.utc)
  # Expensive stuff in here.
  respond_to do |format|
    ...
  end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!