当前位置:网站首页>Overview of browser caching mechanism
Overview of browser caching mechanism
2022-07-02 19:47:00 【Orpheus group】
What is browser caching ?
Browser cache (Browser Caching) It's to save network resources and speed up browsing , The browser is on the local disk ( Or memory ) Store recently requested documents on , When the visitor requests the page again , The browser can read from the local disk ( Or memory ) Show document , In this way, you can speed up the reading of the page .
There are two main types of browser caching :
- Service end link check : Cache negotiation :Last-modified (HTTP1.0),Etag (HTTP1.1)
- Browser link check : Cache thoroughly ( Hard cache / Strong cache ):cache-control(HTTP1.1),Expires(HTTP1.0)
Which files the browser throws into memory ? What goes into the hard drive ?
There are two main factors that browsers consider when storing resources : Usage and size
- For large files , The high probability is not stored in memory , Whereas the priority
- Current system memory usage is high , Files are stored in the hard disk first
Browser caching process
Browser caching starts with the second request :
- The first time a resource is requested , Browser normal request , The server returns resources normally , And send back the cache policy of resources in the response header
- The second time a resource is requested , The browser judges these request parameters , Hit the strong cache to intercept the request, read the browser cache, and return the cache to the browser , Otherwise, the request parameters are added to the request header and passed to the server , Check whether the negotiation cache is hit , Hit and return 304, Otherwise, the server will return new resources . This is the overall flow chart of cache operation
## advantage
- Reduced redundant data transfer , Save network fee
- Reduced server burden , Greatly improved the performance of the website
- Speed up the client to load web pages
difference
- Strong cache
- No requests will be made to the server , If hit , Read the resource directly from the cache , stay chrome Console network You can see the request return in the options 200 The status code
- Negotiate the cache
- Send request to server , The server will follow this request request header To determine whether to hit the negotiation cache , If hit , Then return to 304 Status code and bring new response header Notify browser to read resources from cache
About browser caching header Parameters
Strong cache
- Expires: Expiration time in the request header , When the browser loads the resource again , If within this expiration time , Then hit the strong cache .
- There is a drawback , It determines whether the expiration date is based on the local time , The local time can be modified by yourself .
- Cache-Control:
- max-age Set the duty as max-age=300 when , Represents the correct return time for this request ( The browser will also record it ) Of 5 Load the resource again in minutes , It hits the strong cache ( Maximum survival time )
- no-cache: Do not use local cache . Cache negotiation required , First confirm with the server whether the returned response has been changed , If a previous response exists ETag, Then the request will be verified with the server , If the resource has not been changed , You can avoid re downloading ( Client cache resources , However, whether the cache needs to be verified through negotiation cache )
- no-store: Directly prohibit the browser from caching data , Every time a user requests the resource , Will send a request to the server , Download full resources every time ( Do not use cache , Ask for a new one every time )
- public: Can be cached by all users , Including end users and CDN Wait for intermediate proxy server ( Both the resource client and the server can cache )
- private: Can only be cached by the end user's browser , Don't allow CDN Wait for the relay cache server to cache it ( Only the client can cache resources )
Router.get('/', async (ctx) => {
const getResource = () => {
return new Promise((res) => {
fs.readFile("./fs/a.txt", (err, data) => {
if (err) {
return;
}
res(data)
})
})
}
ctx.set('Cache-Control', 'max-age=10') // Set strong cache , The expiration date is 10 second
ctx.body = await getResource();
})
priority :Cache-Control Has a higher priority than Expries
Negotiate the cache
- Last-Modify / If-Modify-Since:
The last modification time returned by the server to resources , Put this time in the request header when requesting , The server judges whether the resource has been modified by comparing the last modification time of the resource with the time transmitted from the request header
- The first time a browser requests a resource , Server return response Of header I will add Last-Modify,Last-modify Is a time to identify the last modification time of the resource ; When the browser requests the resource again ,request The request header of will contain If-Modify-Since, This value is returned before the cache Last-Modify. Server received If-Modify-Since after , Determine whether to hit the cache according to the last modification time of the resource
- Etag / If-None-Match
Each resource has a unique identifier Etag, When the server returns resources , At the same time, the Etag return , When requested by the client , Ask for a headband If-None-Match( Previously returned Etag value ) The header information , The server compares and judges whether the resources have changed
- Etag:web When the server responds to a request , Tells the browser the unique identity of the current resource on the server ( The generation rules are determined by the server ).
- If-None-Match: When a resource expires ( Use Cache-Control Identification of the max-age), Discovery resources have Etag Statement , Again to web Server request with header If-None-Match (Etag Value ).web The server found a header after receiving the request If-None-Match Compare with the corresponding verification string of the requested resource , Decide whether to hit the negotiation cache ;
Router.get('/pp', async (ctx) => {
const ifModifiedSince = ctx.request.header['if-modified-since'];
const getResource = () => {
return new Promise((res) => {
fs.stat("./fs/a.txt", (err, stats) => {
if (err) {
console.log(err);
}
res(stats)
})
})
}
let resource = await getResource();
// atime Access Time Access time
// Last access to file ( Read or execute ) Time for
// ctime Change Time Time of change
// Change the file for the last time ( Property or permission ) Or catalogue ( Property or permission ) Time for
// mtime Modify Time Modification time
// The last modification of the file ( Content ) Or catalogue ( Content ) Time for
if (ifModifiedSince === resource.mtime.toGMTString()) { // Convert the specific date into ( according to GMT) character string
ctx.status = 304;
}
ctx.set('Last-Modified', resource.mtime.toGMTString());
ctx.body = resource
})
priority :Etag / If-None-Match Priority and Last-Modified / If-Modified-Since
边栏推荐
- Istio1.12: installation and quick start
- Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
- How to set priorities in C language? Elaborate on C language priorities
- 中缀表达式转换为后缀表达式(C语言代码+详解)
- 简书自动阅读
- AcWing 1137. Select the best line solution (the shortest circuit)
- AcWing 1127. 香甜的黄油 题解(最短路—spfa)
- CheckListBox control usage summary
- Automatically generate VGg image annotation file
- Bubble sort array
猜你喜欢
蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么
Kt148a voice chip IC software reference code c language, first-line serial port
What is the Bluetooth chip ble, how to select it, and what is the path of subsequent technology development
After writing 100000 lines of code, I sent a long article roast rust
KT148A语音芯片使用说明、硬件、以及协议、以及常见问题,和参考代码
Zabbix5 client installation and configuration
励志!大凉山小伙全奖直博!论文致谢看哭网友
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
高并发下如何避免产生重复数据?
KT148A语音芯片ic的软件参考代码C语言,一线串口
随机推荐
AcWing 903. Expensive bride price solution (the shortest path - building map, Dijkstra)
Istio1.12:安装和快速入门
Refactoring: improving the design of existing code (Part 2)
Gmapping code analysis [easy to understand]
How to set priorities in C language? Elaborate on C language priorities
[ERP software] what are the dangers of the secondary development of ERP system?
AcWing 341. 最优贸易 题解 (最短路、dp)
Automatic reading of simple books
Notes on hardware design of kt148a voice chip IC
程序猿入门攻略(十二)——数据的存储
AcWing 383. 观光 题解(最短路)
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
Postman下载安装
NMF-matlab
Refactoring: improving the design of existing code (Part 1)
Reading notes of code neatness
Introduction to program ape (XII) -- data storage
KT148A语音芯片ic的硬件设计注意事项
RPD product: super power squad nanny strategy
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)