当前位置:网站首页>彻底弄懂浏览器强缓存和协商缓存
彻底弄懂浏览器强缓存和协商缓存
2022-07-02 12:06:00 【Mr.指尖舞者】
在工作中,前端代码打包之后的生成的静态资源就要发布到静态服务器上,这时候就要做对这些静态资源做一些运维配置,其中,gzip和设置缓存是必不可少的。这两项是最直接影响到网站性能和用户体验的。
缓存的优点:
- 减少了不必要的数据传输,节省带宽
- 减少服务器的负担,提升网站性能
- 加快了客户端加载网页的速度
- 用户体验友好
缺点:
- 资源如果有更改但是客户端不及时更新会造成用户获取信息滞后,如果老版本有bug的话,情况会更加糟糕。
一.强缓存
到底什么是强缓存?强在哪?其实强是强制的意思。当浏览器去请求某个文件的时候,服务端就在respone header里面对该文件做了缓存配置。缓存的时间、缓存类型都由服务端控制,具体表现为:
respone header 的cache-control,常见的设置是max-age public private no-cache no-store等
强缓存总结
cache-control: max-age=xxxx,public
客户端和代理服务器都可以缓存该资源;
客户端在xxx秒的有效期内,如果有请求该资源的需求的话就直接读取缓存,statu code:200 ,如果用户做了刷新操作,就向服务器发起http请求cache-control: max-age=xxxx,private
只让客户端可以缓存该资源;代理服务器不缓存
客户端在xxx秒内直接读取缓存,statu code:200cache-control: max-age=xxxx,immutable
客户端在xxx秒的有效期内,如果有请求该资源的需求的话就直接读取缓存,statu code:200 ,即使用户做了刷新操作,也不向服务器发起http请求cache-control: no-cache
跳过设置强缓存,但是不妨碍设置协商缓存;一般如果你做了强缓存,只有在强缓存失效了才走协商缓存的,设置了no-cache就不会走强缓存了,每次请求都回询问服务端。cache-control: no-store
不缓存,这个会让客户端、服务器都不缓存,也就没有所谓的强缓存、协商缓存了。
二、协商缓存
上面说到的强缓存就是给资源设置个过期时间,客户端每次请求资源时都会看是否过期;只有在过期才会去询问服务器。所以,强缓存就是为了给客户端自给自足用的。而当某天,客户端请求该资源时发现其过期了,这是就会去请求服务器了,而这时候去请求服务器的这过程就可以设置协商缓存。这时候,协商缓存就是需要客户端和服务器两端进行交互的。
参考文献:https://www.jianshu.com/p/9c95db596df5
边栏推荐
- 搭建自己的语义分割平台deeplabV3+
- 18_Redis_Redis主从复制&&集群搭建
- 学习使用php实现公历农历转换的方法代码
- QML pop-up frame, customizable
- LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
- Force deduction solution summary 2029 stone game IX
- 11_ Redis_ Hyperloglog_ command
- 11_Redis_Hyperloglog_命令
- 党史纪实主题公益数字文创产品正式上线
- The past and present lives of visual page building tools
猜你喜欢
Leetcode skimming -- incremental ternary subsequence 334 medium
Yolov5 code reproduction and server operation
【网络安全】网络资产收集
yolo格式数据集处理(xml转txt)
The past and present lives of visual page building tools
Mavn builds nexus private server
List set & UML diagram
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
Data analysis thinking analysis methods and business knowledge - business indicators
随机推荐
JVM architecture, classloader, parental delegation mechanism
Topology architecture of the minimum deployment of tidb cluster
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
. Solution to the problem of Chinese garbled code when net core reads files
02.面向容器化后,必须面对golang
5. Practice: jctree implements the annotation processor at compile time
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
03_ Linear table_ Linked list
18_Redis_Redis主从复制&&集群搭建
Tidb hybrid deployment topology
How to write sensor data into computer database
Summary of the first three passes of sqli Labs
Bing. Site Internet
哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
19_ Redis_ Manually configure the host after downtime
LeetCode刷题——递增的三元子序列#334#Medium
10_ Redis_ geospatial_ command
MD5 encryption
Leetcode question brushing - parity linked list 328 medium