1

从服务器或客户端上没有缓存开始

第一个请求

GET /post/1 HTTP/1.1

HTTP/1.1 200 OK
Date: Fri, 05 Mar 2010 09:05:46 GMT
Last-Modified: Thu, 04 Mar 2010 21:00:08 GMT
X-Rack-Cache: miss
Etag: "c226165d5817af7c91592dab0bc0ac63"
Cache-Control: max-age=3600, public

缓存丢失,Rails 被命中并查询数据库:

if stale?(:etag => @document, :last_modified => @document.updated_at.utc) # => true
  expires_in 1.hour, :public => true
  @post = Post.find(params[:id])
end

第二个请求

GET /post/1 HTTP/1.1
If-Modified-Since: Thu, 04 Mar 2010 21:00:08 GMT
If-None-Match: "c226165d5817af7c91592dab0bc0ac63"
Cache-Control: max-age=0

HTTP/1.1 304 Not Modified
Date: Fri, 05 Mar 2010 09:10:04 GMT
X-Rack-Cache: miss
Etag: "c226165d5817af7c91592dab0bc0ac63"
Cache-Control: max-age=3600, public

缓存丢失,Rails 被命中,但这次它发送 304 Not Modified 并且数据库没有被命中:

if stale?(:etag => @document, :last_modified => @document.updated_at.utc) # => false
  expires_in 1.hour, :public => true
  @post = Post.find(params[:id])
end

但是我的印象是,自从 etag 匹配 (If-None-Match/Etag) 后,这应该会命中缓存?

4

2 回答 2

4

我正在执行完全刷新 (F5),因此浏览器正在添加 Cache-Control: max-age=0 这会阻止缓存认为页面是新鲜的。

于 2010-03-05T10:36:45.207 回答
0

据我了解,If-None-Match 和 If-Modified-Since 标头是在浏览器的缓存内容过期时发送的(内容在浏览器缓存中的时间超过了 Cache-Control 标头中的最大值)。这允许您的应用程序验证内容是否仍然新鲜,如果是,则使用新的 maxage 和与以前相同的 Etag 发送 304。

于 2011-03-02T21:35:53.743 回答