Network basics: HTTP (6): HTTP caching process

1. What is the caching process of HTTP?
The usual steps are:

1. The client sends a request to the server, requesting resources.

2. The server returns the resource and determines the caching strategy through the response header.

3. The client decides whether to cache the resource according to the response header strategy (assuming it is here), and caches the response header and resource.

4. When the client requests the resource again and hits the resource, the client checks the cache strategy of the last cache, and judges whether to read the local cache directly or negotiate the cache with the server according to the difference of the strategy, whether it expires or not.
Insert picture description here
2. When will strong caching or negotiation caching be triggered?

1. Strong caching
Strong caching is inseparable from the two response headers Expires and Cache-Control

①Expires: Expires is a header that represents the expiration time of resources proposed by http1.0. It describes an absolute time and is returned by the server. Expires is limited to the local time. If the local time is modified, the cache may become invalid.

Format: Expires: Wed, 11 May 2020 10:34:00 GMT

②Cache-Control: Cache-Control appears in HTTP/1.1, has a higher priority than Expires, which means relative time

Format: Cache-Control: max-age=315360000

The current mainstream approach uses Cache-Control to control the cache. In addition to max-age control time, there is also

①Cache-Control: public can be cached by all users, including intermediate proxy servers such as terminals and CDNs.

②Cache-Control: private can only be cached by the terminal browser, and no relay cache server is allowed to cache.

③Cache-Control: no-cache caches the local first, but after hitting the cache, the freshness of the cache must be verified with the server before it can be used.

④Cache-Control: no-store will not generate any cache.
Insert picture description here
If the name hits the cache during the cache validity period, the browser will directly read the local cache resources, and negotiate with the server when the cache expires.

2. Negotiation caching
When the response header returned by the server does not contain Cache-Control and Expires or Cache-Control and Expires expires or its attribute is set to no-cache when the first request is made, then the browser will request it for the second time Will negotiate with the server.

If the cache and the latest version of the server resource are the same, then there is no need to download the resource again, and the server directly returns a 304 Not Modified status code. If the server finds that the cache in the browser is already an old version, the server will save the latest resource The complete content of is returned to the browser, the status code is 200 OK.

The way for the server to determine whether the cache is fresh is to rely on the other two sets of HTTP information.

Last-Modified/If-Modified-Since

When the client requests a resource for the first time, the server will send the latest modification time of the resource Last-Modified: Thu, 19 Feb 2020 08:23:23 GMT to the client through the response header. When the request is sent again, the client will return the server The modification time is placed in the request header. If-Modified-Since: Thu, 19 Feb 2020 08:23:23 GMT is sent to the server, and the server is compared with the corresponding resources on the server. If the resources of the server are newer, the latest is returned At this time, the status code is 200. When the server resource is consistent with the first time requested by the client, it proves that the client's resource is new. A 304 status code is returned, indicating that the client can directly use the cache.

ETag/If-None-Match

The process of ETag is similar to Last-Modified. The difference is that ETag is hashed according to the content of the resource to generate an information summary. As long as the content of the resource changes, this will change dramatically. You can determine by comparing the summary information. Whether the client's cache resource is updated is more accurate than Last-Modified.

Response header:
Insert picture description here
So the overall caching flow chart is as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/imagine_tion/article/details/110422884