浏览器缓存segmentfault
浏览器缓存分为几个阶段:浏览器
1.cache-control
: 决定了浏览器端和服务器端缓存的策略,能够出如今响应头response header中,或者 请求头 request header中缓存
max-age
:指定缓存的最大有效时间,eg:cache-control:max-age=315360000
,注意与expires
作区分(与cache-control
平级),max-age
优先级高于 expires
,这个属性时HTTP1.1中新增的属性s-maxage
:指定public
的缓存,缓存设备有不少,不单单浏览器是缓存设备,在整个网络中,可能会存在代理服务器,CDN
属于public
缓存设备,由于能够多用户访问并读取信息;什么是private缓存呢,指的是只是你我的访问的设备,浏览器就属于private缓存设备,eg:s-maxage=31536000;他的优先级高于max-age,只能设定public的缓存设备private
public
no-cache
:错误理解:不使用缓存;no-cache指的是无论本地是否设置了max-age(即忽略本地浏览器端的缓存策略),都要向服务器端发送请求,由服务器端来判断缓存状况no-store
:彻底不使用任何的缓存策略,无论是服务器端仍是浏览器端的2.expires:Thu, 14 Mar 2019 17:29:17 GMT
,这个属性时HTTP1.0
中配置,服务器
基于客户端和服务器端的协商缓存机制网络
1.last-modified
代理
last-modified
- response headerif-modified-since
- request headercode
须要与cache-control共同使用,若是配置了max-age
且没有过时,就不会使用last-modified;过时了以后,才会使用last-modified。资源
last-modified
缺点:文档
2.ETag
get
文档内容的hash值
ETag ---- response header
if-None-Match ----request header
etag
优先级高于last-modified
200(from cache)
: 浏览器端缓存,cache-control:max-age=315360000
expires
起做用304
: 服务器端缓存,last-modified
或者 etag
起做用注意:Chrome浏览器,手动点击刷新按钮都会 在请求头中,添加 chche-control:max-age=0
,这样就确定不会使用浏览器端的缓存!
更加详细,请参考:缓存详解