Browser cache class

1: What are the types of browser caches? Please briefly explain.

There are two types: strong cache and negotiated cache

Strong cache

Strong caching is controlled by using expires and cache-control in the header of http

expires: It is an absolute time, indicating the expiration time of the cache. Before this time point, it indicates that the cache is hit. Disadvantages: This is the time on the server, which may be different from the time on the client.

cache-control: max-age is a relative time in seconds, which is more reliable than expires

Negotiate cache

When the browser requests a resource from the server for the first time, it will return the resource to the browser, and return some information of the resource such as (file summary, last modification time) to the browser, and tell the browser to cache this information Up, when the browser requests the server again, it will send the information to the server together. The server judges whether it needs to be updated according to the cache resources of the browser. If it does not need to be updated, it will return 304, and not-modified, otherwise it will return new resource

Negotiation caches are managed using [last-modified,if-modified-since] and [etag,if-not-match]

  1: When the browser requests the server resource for the first time, the server returns the resource and returns the last-modified to the browser

  2: When the browser requests the resource from the server again, it will bring if-modified-since, and its value is the last-modified returned by the server three times.

  3: The server compares the value of if-modified-since with the time when the resource was last modified. If the two are equal, if it returns 304, it will hit the negotiation cache, otherwise it will return to a new resource

 

Sometimes last-modified is not very accurate, such as modifying some comments, then you have to use etag and if-not-match

  1: When the browser requests the server resource for the first time, the server returns the resource and returns the etag to the browser. The etag represents the unique identifier of the resource content. Only when the content of the resource is modified, the etag will change.

    2: When the browser requests the resource from the server again, it will bring if-not-match, and its value is the etag returned by the server last time. At this time, the server will do according to if-not-match and the etag of the resource. Compare, if the two are equal, the resource content has not changed, return 304, otherwise return the new resource

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325229355&siteId=291194637