当前位置:网站首页>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
边栏推荐
- MySQL表历史数据清理总结
- RPD出品:Superpower Squad 保姆级攻略
- 定了,就是它!
- 从20s优化到500ms,我用了这三招
- 良心总结!Jupyter Notebook 从小白到高手,保姆教程来了!
- pxe装机「建议收藏」
- KT148A语音芯片ic的开发常见问题以及描述
- Kt148a voice chip IC software reference code c language, first-line serial port
- Embedded (PLD) series, epf10k50rc240-3n programmable logic device
- Infix expression is converted to suffix expression (C language code + detailed explanation)
猜你喜欢

定了,就是它!

Py's interpret: a detailed introduction to interpret, installation, and case application

JASMINER X4 1U深度拆解,揭开高效省电背后的秘密

Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem

Dictionaries

【NLP】一文详解生成式文本摘要经典论文Pointer-Generator
冒泡排序数组

自動生成VGG圖像注釋文件

AcWing 342. Road and route problem solving (shortest path, topological sorting)

Istio部署:快速上手微服务,
随机推荐
AcWing 1137. Select the best line solution (the shortest circuit)
Use IDM to download Baidu online disk files (useful for personal testing) [easy to understand]
Gmapping code analysis [easy to understand]
《代碼整潔之道》讀書筆記
Think about the huge changes caused by variables
AcWing 1137. 选择最佳线路 题解(最短路)
字典
MySQL table historical data cleaning summary
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
Notes de lecture sur le code propre
AcWing 1125. Cattle travel problem solution (shortest path, diameter)
简书自动阅读
[ERP software] what are the dangers of the secondary development of ERP system?
使用IDM下载百度网盘的文件(亲测有用)[通俗易懂]
RPD product: super power squad nanny strategy
职场四象限法则:时间管理四象限与职场沟通四象限「建议收藏」
AcWing 1128. 信使 题解(最短路—Floyd)
Is there any security guarantee for the ranking of stock and securities companies
JS如何取整数
Start practicing calligraphy