Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1-5 缓存类型 强制缓存 协商缓存 #5

Open
fanyuyingbest opened this issue Apr 28, 2021 · 0 comments
Open

1-5 缓存类型 强制缓存 协商缓存 #5

fanyuyingbest opened this issue Apr 28, 2021 · 0 comments

Comments

@fanyuyingbest
Copy link
Owner

fanyuyingbest commented Apr 28, 2021

参考1
参考2

缓存的意义:

  • 良好的缓存策略可以有效降低资源重复加载带来的带宽,可有效提升加载速度
  • 浏览器的缓存分为强制缓存和协商缓存

强制缓存

强制缓存是根据header的两个字段来控制的,如果在header里面设置了 expires(1.0) 或 cache-control(http1.1),则表示设置了强制缓存项;
cache-control高于 expires;cache-control有这几个设置项目:
public:表示服务端和第三方中转代理都做缓存
priality:表示只在服务端缓存
over:都不做缓存,强制重新请求
cache:启用协商缓存
强制缓存的原理:
强制缓存生效(过期),则直接读取服务端缓存(不需要请求,直接返回200)
强制缓存未生效,检查协商缓存
若未设置强制缓存,则直接重新发起web请求

协商缓存

协商缓存是在强制缓存未生效时,会根据 etag(类似签名)及last-modify来做判断,启用协商缓存时,还是需要向服务端发起请求的,来对比etag和last-modify的差别,如果没有变化,则直接返回304,读取缓存;如果发生了变化,则需要重新发起web请求,然后再走缓存逻辑

差别和联系

差别:强制缓存不会再发起请求,而且在生效后直接读取缓存;协商缓存还需要请求服务端,做对比;
建议:一般依赖文件可以使用强制缓存;资源文件(图片等)可以使用协商缓存

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant