当前位置:网站首页>Cache control of HTTP
Cache control of HTTP
2022-06-24 22:28:00 【Nice2cu_ Code】
HTTP Cache control
List of articles
Because the network delay is uncontrollable , Browsers use HTTP The cost of obtaining resources from the server is high . therefore , It's very necessary to put “ Don't come easy ” Data is cached , The next time you request again, reuse it as much as possible without having to look in the server . such , You can avoid multiple requests - The communication cost of the response , Save network bandwidth , It can also speed up the response .
1. Cache control of the server
The process of caching is as follows :
- The browser found no data in the cache , So send a request , Get resources from the server
- Server responds to requests , Returns the resource , meanwhile Mark the validity period of the resource
- Browser cache resources , Wait for next reuse
The header field used by the server to mark the resource validity period is “Cache-Control”, The value of the inside “max-age=30” Is the effective time of resources , Equivalent to telling the browser ,“ This page can only cache 30 second , After that, even if it is overdue , Out-of-service , If you want to access, you must get it from the server again . Without disabling the cache and not exceeding the effective time , Accessing this resource again hits the cache , Instead of requesting resources from the server, it fetches them directly from the browser cache .
Be careful : The starting point for calculating the effective time is the creation time of the response message on the server side , Not when the client receives the message , That is, it includes the time spent in the link transmission process . such as , Server settings “max-age=5”, But because the network quality is terrible , When the browser receives the response message, it has passed 4 second , Then this resource can be saved at most on the client 1 Second , Then it will fail .
Other common cache properties :
- no_store: Caching is not allowed , For some data that changes very frequently , For example, the seckill page
- no_cache: Easy and its literal meaning no_store Mix up , The actual meaning is not to disallow caching , Instead, it can cache , But in Before use, you must go to the server to verify whether it expires , Is there an up-to-date version of , If so, use the latest version of the server
- must-revalidate: It means that if the cache does not expire, it can continue to be used , But if you still want to use it after it expires, you must go to the server to verify whether it is available
Add :
And header fields “Cache-Control” similar ,Expires The response header indicates that the cache has expired Point in time , Beyond this point in time, the resource expires , However, due to the different time zones, this method is almost no longer used , When two exist at the same time Cache-Control Higher priority .
2. Client cache control
Not only the server can send “Cache-Control” head , Browsers can also send “Cache-Control”, That is to say, request - Both sides of the response can use this field for cache control , Negotiate the use strategy of cache .
When you click “ Refresh ” Button time , The browser will add a “Cache-Control: max-age=0”. Indicates that you need an up-to-date content , The data in the local cache has existed for at least a few seconds , It's not the latest , So the browser will not use the cache , Instead, send a request to the server . The server sees max-age=0, It will also respond to the browser with a newly generated message .
When you click Ctrl+F5 “ Forced to refresh ” when , The browser will send a “Cache-Control: no-cache”, Meaning and “max-age=0” Is essentially the same , Usually the effect is the same , That is, no caching .
When using “ Forward ”、“ back off ”、” Redirect jump “ when , The browser uses a cache , In doing so , Use only the most basic request header , No, “Cache-Control”, So it checks the cache , Direct use of previous resources , No more network communication .
3. Condition request
The cached resource has expired , It doesn't mean that the content of resources has changed , If there is no difference from the resources on the server , In fact, there is no need to request the server again . The client uses a conditional request to verify whether the current resource has been modified , Conditional requests have two pairs of mechanisms :Last-Modified / If-Modified-Since and Etag / If-None-Match.
3.1 Last-Modified / If-Modified-Since
When the server first responds to a request , Tell the browser when the resource was last modified Last-Modified

When the browser requests the resource again , If there is no expiration, read directly from the cache , If it's out of date , You have to make a request to the server , adopt If-Modified-Since Field notifies the server of the last request , The last modification time of the resource returned by the server

The server found a header after receiving the request If-Modified-Since, Compare with the last modification time of the requested resource
If the comparison is successful , It indicates that the resource has no new modification , Respond to 304 NOT Modified, Tell the browser to continue using the saved cache,304 Only return header part , Notify the client to use the cache through the status code , There is no need to return the main part of the message to the client
If the comparison fails , The resource has been changed , Respond to the entire resource content , Return status code 200, The latest content of the resource is in the response body
3.2 Etag / If-None-Match
HTTP1.1 use ETag To determine whether the requested file has been modified , Mainly to solve Last-Modified Some unsolvable problems :
Some files may change periodically , But his content doesn't change ( Just change the modification time ), At this time, you don't want the client to think that the file has been modified GET;
Some files are modified very frequently ,1 I've changed N Time ,If-Modified-Since The granularity that can be detected is second , This modification cannot be judged
Some servers can't get the exact last modification time of the file
Etag / If-None-Match Workflow :
When the server responds to a request , Tells the browser the unique identity of the current resource on the server Etag

When the browser requests the server again , adopt If-None-Match Field notifies the server that the client received the unique identification of the cached data last notified by the server

The server found a header after receiving the request If-None-Match, It is compared with the unique ID of the requested resource , Different , Indicates that the resource has been changed , Response to the whole resource content , Return status code 200. identical , Description no new changes to resources , The response HTTP 304, Tell the browser to continue using the saved cache
Be careful :
If the last modification time of some resources changes , But the content didn't change , Use Last-modified I can't see that the content hasn't changed , however Etag You can judge whether changes have taken place , therefore Etag It's more accurate than Last-modified high . When two pairs of conditional requests are used at the same time ,Etag / If-None-Match Higher priority .
ETag also “ strong ”、“ weak ” Points
strong ETag Requires resources to match exactly at the byte level , weak ETag There is one before the value “W/” Mark , It only requires that the resource has no semantic change , But there may be some internal changes ( for example HTML The order of the labels in , Or a few more spaces ).
边栏推荐
- leetcode:45. 跳跃游戏 II【经典贪心】
- Docker 安装 Redis-5.0.12,详细步骤
- TCP RTT measurement tips
- Flutter 库冲突问题解决
- Redis-跳表
- 如何抓手机的包进行分析,Fiddler神器或许能帮到您!
- Publicity of the second batch of shortlisted enterprises! Annual Top100 smart network supplier selection
- Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
- Creating files, recursively creating directories
- NIO 零拷贝
猜你喜欢

如何比较两个或多个分布:从可视化到统计检验的方法总结

How does flutter use the online transcoding tool to convert JSON to model

故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法

How to refine permissions to buttons?

如何抓手机的包进行分析,Fiddler神器或许能帮到您!

Flutter 如何使用在线转码工具将 JSON 转为 Model

How to grab the mobile phone bag for analysis? Fiddler artifact may help you!

干货丨产品的可行性分析要从哪几个方面入手?

I really can't do it. After 00, I collapsed and wanted to leave

Cannot find reference 'imread' in 'appears in pycharm__ init__. py‘
随机推荐
学习笔记23--多传感器信息融合基础理论(上)
Flutter: Unsupported value: false/true
NIO 零拷贝
Development of live broadcast software app, and automatic left-right sliding rotation chart advertising
字符串习题总结2
try-with-resources 中的一个坑,注意避让
leetcode:45. Jumping game II [classic greed]
SAP interface debug setting external breakpoints
Object. Defineproperty and reflect Fault tolerance of defineproperty
Xinlou: Huawei's seven-year building journey of sports health
img2pdf
04A interrupt configuration
磁盘的结构
故障安全移动面板KTP900F Mobile下载程序提示无法下载,目标设备正在运行或未处于传输模式的解决办法
Seven principles of software design
String exercise summary 2
Flutter 如何使用在线转码工具将 JSON 转为 Model
Structure du disque
Detailed explanation of agency mode
The ktp900f mobile download program of the fail safe mobile panel prompts that the download cannot be performed, and the target device is running or not in the transmission mode