Calculating Network Review---HTTP Cache

According to different caching strategies, it can be divided into two types: 强制缓存and对比缓存

Forced caching

The so-called forced caching means that when requesting network data, if there is cached data locally and the cached data is valid, the cached data is used directly without a network request; otherwise, the data is obtained through a network request.

Mainly the following two fields

Expires

(Refer to https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Expires )

The Expires response header contains the date/time, that is, after this time, the response expires.

For example: Expires: Wed, 21 Oct 2015 07:28:00 GMT

Disadvantages : It uses an absolute time. If the client modifies the local time, the cache may become invalid.

Cache-Control

(Refer to https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Cache-Control )

It uses relative time, for example, it can set the following fields

max-age=

Set the maximum period of cache storage, beyond which the cache is considered expired (in seconds). And Expirescontrast, with respect to time is the time of the request.

Contrast cache

The comparison cache will make a network request every time. When requesting network data, if there is cached data locally and it has not expired, it must be sent to the server for verification to determine whether the cached data needs to be updated. If it needs to be updated, the server will return new data, if it does not need to be updated, it will return 304 (Not Modified), let the client directly use the cached data.

Last-Modified/If-Modified-Since

The specific parameters in the http protocol are implemented by the Last-Modified/ in the head. The If-Modified-Sinceserver returns the Last-Modified parameter to mark the last modification time of the resource. When the client asks whether the cache expires, this time data is put into If-Modified- In the Since parameter, if the cached data is available, the 304 status code will be returned, otherwise the interface data and the updated Last-Modified parameter will be returned directly

Etag/If-None-Match

ETag/ If-None-MatchParameters are also often used for cache verification. When the client requests data, the server will return the data and ETag information. ETag is the unique identification of the resource on the server . The client will put the identification information when it requests data again. Initiate a check to the server in the If-None-Match parameter, and return a 304 status code if the cache is available, otherwise return the data (status code 200) and the updated Etag information.

The difference between forced caching and contrast caching

  • If the mandatory cache takes effect, there is no need to interact with the server, and the contrast cache needs to interact with the server regardless of whether it takes effect .
  • Two types of cache rules can exist at the same time, and the mandatory cache priority is higher than the comparison cache , that is, when the mandatory cache rule is executed, if the cache takes effect, the cache is used directly, and the comparison cache rule is no longer executed.

Get the cache process

Guess you like

Origin blog.csdn.net/why1092576787/article/details/114848232