当前位置:网站首页>聊一聊浏览器缓存控制
聊一聊浏览器缓存控制
2022-04-23 16:38:00 【summer·】
缓存是计算机领域一个很重要的概念,今天我们主要聊一聊浏览器中缓存的策略
首先聊一下需要缓存的时候的过程是什么样子:
客户端发起HTTP请求,服务器响应
响应内容包含所需数据和缓存规则(标示)并且浏览器会将数据和缓存规则存入浏览器缓存中
客户端再次发起上一请求,会带着缓存规则先向浏览器缓存询问,缓存是否过期
没有过期则直接返回缓存内容,过期则重新想服务器发送请求
以上过程其实分为两种方法:强缓存和协商缓存
强制内存和协商缓存
`浏览器每次发起请求,都会先在浏览器缓存中查找该请求的结果以及缓存标识`
`浏览器每次拿到返回的请求结果都会将该结果和缓存标识存入浏览器缓存中`
强制缓存:向浏览器缓存查找该请求结果,并根据该结果的缓存规则来决定是否使用该缓存结果的过程
强缓存分为三种情况:
- 不存在该缓存结果和缓存标识,强制缓存失效,则直接向服务器发起请求(跟第一次发起请求一致)
- 存在该缓存结果和缓存标识,但该结果已失效,强制缓存失效,则使用协商缓存(暂不分析)
- 存在该缓存结果和缓存标识,且该结果尚未失效,强制缓存生效,直接返回该结果
强制缓存的缓存规则:
当浏览器向服务器发起请求时,服务器会将缓存规则放入HTTP响应报文的HTTP头中和请求结果一起返回给浏览器,控制强制缓存的字段分别是Expires和Cache-Control,其中Cache-Control优先级比Expires高。
Expires是HTTP/1.0控制网页缓存的字段,其值为服务器返回该请求结果缓存的到期时间,即再次发起该请求时,如果客户端的时间小于Expires的值时,直接使用缓存结果
如果客户端与服务端的时间因为某些原因(例如时区不同;客户端和服务端有一方的时间不准确)发生误差,那么强制缓存则会直接失效,这样的话强制缓存的存在则毫无意义
协商缓存:强制缓存失效后,浏览器携带缓存标识向服务器发起请求,由服务器根据缓存标识决定是否使用缓存的过程
与强缓存的区别就是协商缓存是否使用缓存由服务器决定,强缓存只关心缓存规则中缓存是否过期
总结:
强制缓存优先于协商缓存进行,若强制缓存(Expires和Cache-Control)生效则直接使用缓存,若不生效则进行协商缓存(Last-Modified / If-Modified-Since和Etag / If-None-Match),协商缓存由服务器决定是否使用缓存,若协商缓存失效,那么代表该请求的缓存失效,重新获取请求结果,再存入浏览器缓存中;生效则返回304,继续使用缓存
版权声明
本文为[summer·]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45774485/article/details/124217004
边栏推荐
- 第九天 static 抽象类 接口
- Hypermotion cloud migration helped China Unicom. Qingyun completed the cloud project of a central enterprise and accelerated the cloud process of the group's core business system
- Day (6) of picking up matlab
- Selenium IDE and XPath installation of chrome plug-in
- How to conduct application security test (AST)
- There is a problem with the light switch from 1 to 100
- Disk management and file system
- Homewbrew installation, common commands and installation path
- 文件系统读写性能测试实战
- Database dbvisualizer Pro reported file error, resulting in data connection failure
猜你喜欢
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
OAK-D树莓派点云项目【附详细代码】
Detailed explanation of file operation (2)
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
Review 2021: how to help customers clear the obstacles in the last mile of going to the cloud?
Download and install mongodb
漫画:什么是IaaS、PaaS、SaaS?
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme
NVIDIA graphics card driver error
Research and Practice on business system migration of a government cloud project
随机推荐
力扣-198.打家劫舍
The most detailed Backpack issues!!!
Day (3) of picking up matlab
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
Real time operation of vim editor
力扣-746.使用最小花费爬楼梯
Loading order of logback configuration file
299. 猜数字游戏
RecyclerView advanced use - to realize drag and drop function of imitation Alipay menu edit page
MySQL personal learning summary
JSP learning 3
What is cloud migration? The four modes of cloud migration are?
5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
What is homebrew? And use
MySql主从复制
The most detailed knapsack problem!!!
Cartoon: what are IAAs, PAAS, SaaS?
Creation of RAID disk array and RAID5
Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin
Hyperbdr cloud disaster recovery v3 Version 2.1 release supports more cloud platforms and adds monitoring and alarm functions