在http header里设置Expires为一个时间值,可以使浏览器在该时间内都直接使用缓存而不重新请求服务器,响应速度更快并节省流量。Rails默认没有加这个header,但只要稍微修改一下缓存filter即可使action cache生效。
?
?
class ActionController::Caching::Actions::ActionCacheFilter def before_with_expires_in(controller) should_continue = before_without_expires_in(controller) controller.set_expires_in(seconds) if !should_continue && (seconds = @options[:store_options][:expires_in]) should_continue end alias_method_chain :before, :expires_in end class ActionController::Base def set_expires_in(seconds, options = {}) expires_in(seconds, options) end end