免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2585 | 回复: 0
打印 上一主题 下一主题

为Rails页面缓存加上过期时间header [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-02 13:21 |只看该作者 |倒序浏览
在http header里设置Expires为一个时间值,可以使浏览器在该时间内都直接使用缓存而不重新请求服务器,响应速度更快并节省流量。Rails默认没有加这个header,但只要稍微修改一下缓存filter即可使action cache生效。

Ruby代码
  1. 1.class ActionController::Caching::Actions::ActionCacheFilter   
  2. 2.  def before_with_expires_in(controller)   
  3. 3.    should_continue = before_without_expires_in(controller)   
  4. 4.    controller.set_expires_in(seconds) if !should_continue && (seconds = @options[:store_options][:expires_in])   
  5. 5.    should_continue   
  6. 6.  end  
  7. 7.  alias_method_chain :before, :expires_in  
  8. 8.end  
  9. 9.  
  10. 10.class ActionController::Base   
  11. 11.  def set_expires_in(seconds, options = {})   
  12. 12.    expires_in(seconds, options)   
  13. 13.  end   
  14. 14.end  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP