热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Rails3.1和Http页面缓存-Rails3.1andHttpPageCaching

GiventhatHerokuCedardoesnthavehttpcachingprovidedbyVarnishIwouldliketouseRack::Cach

Given that Heroku Cedar doesn't have http caching provided by Varnish I would like to use Rack::Cache. I have been told that rails 3.1.1 have Rack::Cache active by default, I just need to make sure to have in the configuration:

鉴于Heroku Cedar没有Varnish提供的http缓存,我想使用Rack :: Cache。我被告知rails 3.1.1默认情况下Rack :: Cache处于活动状态,我只需要确保在配置中:

config.action_controller.perform_caching = true

and I need to pick a cache store, for this experiment I'm using:

我需要选择一个缓存存储,对于我正在使用的这个实验:

config.cache_store = :memory_store

In the action of the page I want to cache I've added the following lines:

在我要缓存的页面的操作中,我添加了以下行:

response.header['Cache-Control'] = 'public, max-age=300'
response.header['Expires'] = CGI.rfc1123_date(Time.now + 300)

This code used to work fine with Varnish, the first request would return a 200 and the subsequent (for 5 mins) would return a 304.

此代码用于与Varnish一起使用,第一个请求将返回200,随后(5分钟)将返回304。

This doesn't happen with Rails 3.1 and Heroku Cedar Stack. I do get those headers in the response but subsequent requests returns 200 instead of 304.

Rails 3.1和Heroku Cedar Stack不会发生这种情况。我确实在响应中获得了这些标头,但后续请求返回200而不是304。

What am I doing wrong? Thank you.

我究竟做错了什么?谢谢。

1 个解决方案

#1


11  

As you noted, the Cedar stack doesn't use Varnish. That means a web request will always hit the ruby server.

如您所述,Cedar堆栈不使用Varnish。这意味着Web请求将始终命中ruby服务器。

With that in mind, Rack::Cache will respect your headers and serve the cached content.

考虑到这一点,Rack :: Cache将尊重您的标头并提供缓存的内容。

However, since the request is actually going past the http layer into the rails app, the response will always be 200 since the cache doesn't happen at the http layer anymore.

但是,由于请求实际上是通过http层进入rails应用程序,因此响应将始终为200,因为缓存不再发生在http层。

To confirm this is true, insert this in one of your cached actions:

要确认这是真的,请在您的一个缓存操作中插入:

<%= Time.now.to_i %>

<%= Time.now.to_i%>

Then, reload the page several times and you'll notice the timestamp won't change.

然后,重新加载页面几次,你会发现时间戳不会改变。


推荐阅读
author-avatar
国民男神-权志龙
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有