从服务器或客户端上没有缓存开始
第一个请求
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) 后,这应该会命中缓存?