关于http缓存的理解

来源:互联网 发布:公司网站源码 编辑:程序博客网 时间:2024/05/29 07:34

以 http://szgaea.iteye.com/ 站点下的 c.js 为例。按 ctrl + F5 请求:
Request URL:http://cpro.baidustatic.com/cpro/ui/c.jsRequest Method:GETStatus Code:200 OKRemote Address:222.216.229.34:80Referrer Policy:no-referrer-when-downgradeResponse Headersview sourceAccept-Ranges:bytesAge:3460Cache-Control:max-age=3600Connection:closeContent-Encoding:gzipContent-Length:32402Content-Type:application/x-javascriptDate:Mon, 24 Jul 2017 02:45:20 GMTETag:"59703cac-7e92"Expires:Mon, 24 Jul 2017 02:47:40 GMTLast-Modified:Thu, 20 Jul 2017 05:16:28 GMTOhc-Response-Time:1 0 0 0 0 0Server:JSP3/2.0.14Request Headersview sourceAccept:*/*Accept-Encoding:gzip, deflate, sdchAccept-Language:zh-CN,zh;q=0.8Cache-Control:no-cacheConnection:keep-aliveCookie:CPROID=2A5C3AD238D066F52F727E5D12B8148A:FG=1Host:cpro.baidustatic.comPragma:no-cacheReferer:http://szgaea.iteye.com/User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36


说一下相关字段的理解:
Request Headers :
Cache-Control:no-cache //强制刷新,不使用缓存,取最新数据
Connection:keep-alive //客户端要求使用长连接
Response Headers :
Age:3460 //获取的该资源已经经过的时间,经测试,多次强刷页面,这个字段的值并不是递增的,很有可能作了负载均衡或者代理,在每台真实服务器上都作了资源缓存,且缓存的开始时间不一样,因此这里的Age也不一样
Cache-Control:max-age=3600 //该资源可经过的最大时间
Connection:close //服务器端已关闭连接.不论request还是response的header中包含了值为close的connection,都表明当前正在使用的tcp链接在请求处理完毕后会被断掉。以后client再进行新的请求时就必须创建新的tcp链接了
Date:Mon, 24 Jul 2017 02:45:20 GMT //响应的创建时间
ETag:"59703cac-7e92" //文件标记,下一个同请求用来判断文件是否修改
Expires:Mon, 24 Jul 2017 02:47:40 GMT //缓存有效期,在该有效期内普通方式请求该资源将从缓存中读取,这个时间由 Date + (max_age - Age) 得出
Last-Modified:Thu, 20 Jul 2017 05:16:28 GMT //标志该资源文件最近的修改时间

在Expires时间内刷新页面:
Request URL:http://cpro.baidustatic.com/cpro/ui/c.jsRequest Method:GETStatus Code:200 OK (from memory cache)Remote Address:222.216.229.34:80Referrer Policy:no-referrer-when-downgradeResponse Headersview sourceAccept-Ranges:bytesAge:3363Cache-Control:max-age=3600Connection:closeContent-Encoding:gzipContent-Length:32402Content-Type:application/x-javascriptDate:Mon, 24 Jul 2017 03:51:49 GMTETag:"59703cac-7e92"Expires:Mon, 24 Jul 2017 03:55:46 GMTLast-Modified:Thu, 20 Jul 2017 05:16:28 GMTOhc-Response-Time:1 0 0 0 0 0Server:JSP3/2.0.14Request Headersview sourceAccept:*/*Accept-Encoding:gzip, deflate, sdchAccept-Language:zh-CN,zh;q=0.8Cache-Control:no-cacheConnection:keep-aliveCookie:CPROID=2A5C3AD238D066F52F727E5D12B8148A:FG=1Host:cpro.baidustatic.comPragma:no-cacheReferer:http://szgaea.iteye.com/User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36

这里虽然看得到Request Headers ,但是 fiddler 抓不到这个包,因此这个资源实际并没有请求服务器,而是直接从缓存中读取。

当过了 Expires 时间后,刷新页面:

Request URL:http://cpro.baidustatic.com/cpro/ui/c.jsRequest Method:GETStatus Code:304 Not ModifiedRemote Address:222.216.229.34:80Referrer Policy:no-referrer-when-downgradeResponse Headersview sourceAccept-Ranges:bytesAge:375Cache-Control:max-age=3600Connection:closeContent-Encoding:gzipContent-Length:0Content-Type:application/x-javascriptDate:Mon, 24 Jul 2017 06:55:21 GMTETag:"59703cac-7e92"Expires:Mon, 24 Jul 2017 07:49:06 GMTLast-Modified:Thu, 20 Jul 2017 05:16:28 GMTOhc-Response-Time:1 0 0 0 0 0Server:JSP3/2.0.14Request Headersview sourceAccept:*/*Accept-Encoding:gzip, deflate, sdchAccept-Language:zh-CN,zh;q=0.8Connection:keep-aliveCookie:CPROID=2A5C3AD238D066F52F727E5D12B8148A:FG=1Host:cpro.baidustatic.comIf-Modified-Since:Thu, 20 Jul 2017 05:16:28 GMTIf-None-Match:"59703cac-7e92"Referer:http://szgaea.iteye.com/User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36


此时请求了服务器。请求头中携带:
If-Modified-Since:Thu, 20 Jul 2017 05:16:28 GMT //该字段取自第一次请求响应中的 Last-Modified:Thu, 20 Jul 2017 05:16:28 GMT
If-None-Match:"59703cac-7e92" //该字段取自第一次请求响应中的 ETag:"59703cac-7e92"
服务器接收到这个请求后,判断如果 If-None-Match 和计算得到的 Etag 值相同,而且 If-Modified-Since 和该资源的最近修改时间相同,则返回 304 Not Modify 告诉浏览器继续使用该缓存,同时重新设置 Age , max-age, Date, Expires . 否则返回 200 OK , 并将最新的资源文件发到客户端。

原创粉丝点击