当前位置:网站首页>彻底弄懂浏览器强缓存和协商缓存
彻底弄懂浏览器强缓存和协商缓存
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
边栏推荐
猜你喜欢

党史纪实主题公益数字文创产品正式上线

Leetcode skimming -- sum of two integers 371 medium

语义分割学习笔记(一)

Base64 coding can be understood this way

百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香

Download blender on Alibaba cloud image station
![[c voice] explain the advanced pointer and points for attention (2)](/img/fb/515e25899bd9a2905ee63cb041934a.png)
[c voice] explain the advanced pointer and points for attention (2)

Solve the problem of frequent interruption of mobaxterm remote connection

18_ Redis_ Redis master-slave replication & cluster building

5. Practice: jctree implements the annotation processor at compile time
随机推荐
20_Redis_哨兵模式
16_ Redis_ Redis persistence
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
Sharp tool SPL for post SQL calculation
Yolo format data set processing (XML to txt)
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
学习使用php将时间戳转换为大写日期的方法代码示例
Leetcode question brushing - parity linked list 328 medium
How to avoid 7 common problems in mobile and network availability testing
Oracle primary key auto increment
Tidb environment and system configuration check
List set & UML diagram
I made an istio workshop. This is the first introduction
QML pop-up frame, customizable
18_Redis_Redis主从复制&&集群搭建
SQL transaction
14_ Redis_ Optimistic lock
Apprendre le Code de la méthode de conversion du calendrier lunaire grégorien en utilisant PHP
04_ Stack
LeetCode刷题——去除重复字母#316#Medium