中关村村草 发表于 2011-03-02 13:21

为Rails页面缓存加上过期时间header

在http header里设置Expires为一个时间值,可以使浏览器在该时间内都直接使用缓存而不重新请求服务器,响应速度更快并节省流量。Rails默认没有加这个header,但只要稍微修改一下缓存filter即可使action cache生效。

Ruby代码1.class ActionController::Caching::Actions::ActionCacheFilter   
2.def before_with_expires_in(controller)   
3.    should_continue = before_without_expires_in(controller)   
4.    controller.set_expires_in(seconds) if !should_continue && (seconds = @options[:store_options][:expires_in])   
5.    should_continue   
6.end
7.alias_method_chain :before, :expires_in
8.end
9.
10.class ActionController::Base   
11.def set_expires_in(seconds, options = {})   
12.    expires_in(seconds, options)   
13.end   
14.end
页: [1]
查看完整版本: 为Rails页面缓存加上过期时间header