The upper part is the English version, and the lower part is the Chinese version, with the same content. If there are any wrong, or you have anything hard to understand, especially in the English version(it is modifications based on Google Translate), pls feel free to let me know.many thx.
Overview
Based on chrome Browser, analysis of front-end caching
The front-end involves DNS caching, CDN caching, HTTP caching, and Browser local caching. This article only discusses HTTP caching and Browser local caching in detail.
DNS caching: Generally, it takes about 20ms, and the Browser will cache DNS records by default for more than one minute, for reducing queries to DNS.
CDN caching: The user resolves to the CDN DNS server through the domain name, gets the CDN's load balancing server ip address, then obtains static resources nearby.
1. Why do we need to cache
HTTP caching in order to reduce requests or reduce response content
Local caching is also to reduce requests
2. HTTP caching flowchart
Service Worker is special. It actually belongs to the Browser local caching, but it also plays an important role in the HTTP caching process.
The important nodes in the flow chart are explained below
3.Service Worker
3.1 How to set
Write code (cache.put) by the developer to set which files to cache, set routing rules, and return directly if it matches the caching in CacheStorage, and fetch data when not match.
Note: Regardless of cacheStorage/member caching/ disk caching, which exceeds the size limit of the Browser, the Browser will automatically clear one part according to a certain algorithm
3.3 Examples
4. Memory caching
4.1 How to set
Automatically set by the Browser and stored in the memory
4.2 Caching Strategy
When the current chrome tab is closed, it will be cleared
Automatically cache Get requests with the same URL, such as images/fonts/scripts, etc.
Note: Only no-store in the response header Cache-Control will affect the member caching not to be cached. Other any setting of Cache-Control have nothing to do with memory caching
4.3 Examples
5. Disk caching
5.1 How to set
It strictly abides by the HTTP response header, is automatically set by the Browser, and stored in the hard disk
5.2 Caching Strategy
Clear based on HTTP response header
Divided into two types: mandatory caching and negotiation caching
Mandatory caching means that the client caches the response data to reduce the number of requests
Negotiation cache means that the client caches the response data, but it still needs to send a request every time, and the server needs to compare it. If the resource is modified, will return 200. If it is not modified, will return 304.
disk caching controls caching through http request header and response header, so it is also called http caching
5.2.1 Mandatory caching: Expires
Refers to how long it will expire
It is an absolute time, in seconds, it is invalid if the client changes the time
This is old and has been replaced by the relative time setting of cache-control:max-age of HTTP1.1
5.2.1.1 Example
5.2.2 Mandatory caching: cache-control
key
description
no-cache
ETag response header to inform the client (Browser, proxy server) that this resource first needs to be checked whether it has been modified on the server side, modified, response 200 and resource content, unmodified response 304
no-store
Prohibited to be cached, cache will be re-requested every time
public
Allow proxy servers such as CDN caching
private
Do not allow proxy servers such as CDN caching
max-age
The maximum effective time of the cache, the unit is s
must-revalidate
If the max-age time is exceeded, send a request to the server to verify whether the resource has been modified
s-maxage
Similar to max-age, it is used to set the cache time of the proxy server, the priority is higher than max-age
no-transform
Proxy server is not allowed to change the file format such as pictures
These values can be mixed View the priority of mixed use
no-cache almost = max-age=0, must-revalidate
Pragma before HTTP1.0 can also be set to no-cache and used in conjunction with Expire
Its unit is s
If the file is dynamically generated by the server, a new time will be generated even if the content has not changed. Based on this shortcoming, HTTP 1.1 supplements Etag
5.2.3.1 Step
The server reponses the client of Last-Modified
The Browser saves time and content to disk
Next time request, take out the time and assign value to If-Modified-Since
The server compares If-Modified-Since and Last-Modified
Unmodified response 304, modified response 200
5.2.3.2 Example
5.2.4 Negotiation caching: Etag & If-None-Match
Etag is the identification of the file, generally refers to the generated hash
Its process is similar to Last-Modified, the difference is that the hash value is uploaded through If-None-Match to the server for comparison
Note: If the chrome dev tool is set to Disable cache, the upload of If-None-Match will also be disabled, resulting in only response 200
5.2.4.1 Example
6. Browser local caching
The Browser's local storage has problems with back-end data synchronization, local update, etc. It is recommended to give priority to http cache
6.1 Storage
type
localStorage
httponly cookie
Convenience
Requires manual access by the developer, does not support pan-domain storage
Cannot be obtained manually, the Browser automatically handles it
It is inconvenient to store cookies in clear text, it needs to be accessed manually, and it is not safe than httponly cookies, so it is rarely used.
sessionStorage can only be used in a page tab, which is not the same as the session technology used in the back-end. The session in the back-end stores the sessionID through cookies.
6.2 Database
Since cookies are generally <= 4KB, and localStorage is generally between 2.5MB and 10MB (depending on Browsers), and index queries, etc. are not supported. The Browser launched webSQL indexDB etc. databases
WebSQL is currently obsolete
IndexDB is close to NoSql database, key-value storage, generally >= 250MB.
IndexDB supports asynchronous operations, supports transactions, and is restricted by the JS same-origin policy.
7. HTTP cache used business scenarios
7.1 Resources that don't change often
E.g. picture/public library js/css, set more than 1 year cache-control:max-age=36006024*365
There are two types of updates: hash file name, such as the hash name file typed with webpack; plus the version number, such as ?v=xxx or ?_=xxx
7.2 Frequently changing resources
Set cache-control:max-age=no-cache, let the server compare with Etag
7.3 Defects
When a website/SPA has multiple cache files A1.js B1.css C1.css. At this time, a file becomes invalid, which will cause an error. Therefore, the business should be divided into independent function module. Try to make an independent function module into a js file, reduce the probability of it going wrong
8. Browser local cache used business scenarios
8.1 Performance optimization
Generally used for performance optimization, e.g. saving pictures, js, css, html templates, large amounts of data, etc.
However, HTTP caching should be preferred. Or HTTP cache + Browser local cache strategy, because the Browser caches data, it may cause two data sources, the Browser and the back-end, need to synchronize with the back-end data, update Browser local data, and destroy them, it's increased the complexity of data maintenance
8.2 Authentication token
JWT token, stored in a cookie with http-only set, or in local storage, use https communication
The following is the Chinese version, the same content as above.
The upper part is the English version, and the lower part is the Chinese version, with the same content. If there are any wrong, or you have anything hard to understand, especially in the English version(it is modifications based on Google Translate), pls feel free to let me know.many thx.
Overview
1. Why do we need to cache
2. HTTP caching flowchart
3.Service Worker
3.1 How to set
3.2 Caching Strategy
3.3 Examples
4. Memory caching
4.1 How to set
4.2 Caching Strategy
4.3 Examples
5. Disk caching
5.1 How to set
5.2 Caching Strategy
5.2.1 Mandatory caching: Expires
5.2.1.1 Example
5.2.2 Mandatory caching: cache-control
5.2.2.1 Example
5.2.3 Negotiation caching: Last-Modified & If-Modified-Since
5.2.3.2 Example
5.2.4 Negotiation caching: Etag & If-None-Match
5.2.4.1 Example
6. Browser local caching
6.1 Storage
6.2 Database
7. HTTP cache used business scenarios
7.1 Resources that don't change often
7.2 Frequently changing resources
7.3 Defects
8. Browser local cache used business scenarios
8.1 Performance optimization
8.2 Authentication token
The following is the Chinese version, the same content as above.
概述
1. 为什么需要做缓存
2.HTTP缓存流程图
3.Service Worker
3.1 如何设置
3.2 缓存策略
3.3 举例
4. memory cache
4.1 如何设置
4.2 缓存策略
4.3 举例
5. disk cache
5.1 如何设置
5.2 缓存策略
5.2.1 强制缓存 Expires
5.2.1.1 举例
5.2.2 强制缓存 cache-control
5.2.2.1 举例
5.2.3 协商缓存 Last-Modified & If-Modified-Since
5.2.3.2 举例
5.2.4 协商缓存 Etag & If-None-Match
5.2.4.1 举例
6. 浏览器本地缓存
6.1 storage
6.2 database
7. HTTP缓存 使用业务场景
7.1 不常变化的资源
7.2 经常变化的资源
7.3 缺陷
8. 浏览器本地缓存 使用业务场景
8.1 性能优化
8.2 鉴权token