浏览器缓存默认情况研究

缓存原理

用户第一次请求页面,服务器没有设置expired和max-age,设置了etag和last-modified,
用户第二次请求页面(正常请求,非强制刷新),浏览器会返回304
用户后续继续请求页面,浏览器返回几次304之后,浏览器会使用一个尝试性的时间来使用本地的缓存,而不是直接去请求服务器。
RFC地址:

Since origin servers do not always provide explicit expiration times, a cache MAY assign a heuristic expiration time when an explicit time is not specified, employing algorithms that use other header field values (such as the Last-Modified time)… If the response has a Last-Modified header field (Section 2.2 of [RFC7232]), caches are encouraged to use a heuristic expiration value that is no more than some fraction of the interval since that time. A typical setting of this fraction might be 10%. [https://tools.ietf.org/html/rfc7234#section-4.2.2]

源码地址:https://cs.chromium.org/chromium/src/net/http/http_response_headers.cc?q=lifetimes.freshness&dr=C
截图:
在这里插入图片描述

测试情况

访问http://172.21.34.30:11000/app/call_example/页面,jquery正常返回

在这里插入图片描述

在地址栏回车,重新加载页面
在这里插入图片描述

其中Last-Modified是 Tue, 04 Sep 2018 01:16:08 GMT
按照上面的逻辑,过期时间是now + (now-lastmodified)/10, 也就是Tue Sep 04 2018 09:47:40 GMT+0800
我将本地时间修改为Tue Sep 04 2018 10:47:40 GMT+0800
然后地址栏回车,重新加载页面
在这里插入图片描述

浏览器返回的是304,符合预期

但是问题是,经过一段时间,这些缓存在浏览器中的资源被清除了,再次访问需要重新去下载。

那么浏览器缓存的这些304的资源,什么时候会被浏览器清除?这取决于浏览器的情况(例如浏览器的存储情况)和用户设置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值