In-depth understanding HTTP (ii)------HTTP caching mechanism and principle

Source: Internet
Author: User
Tags browser cache

I. Conceptual BasisReference Original: 73468040

The caching of HTTP, mainly in the local browser and Web proxy server. Here, we are talking about the browser cache.

1. Response header ETag (Entity Tag)

The etag is used to represent a resource. This value is included when the server returns a response, and different resource response return the same etag. The next time the same resource is requested, the browser will pass the ETag to the server in If-none-match, if the server discovers the ETag and the same as the last time, the direct return of 304 resources has not changed, the browser uses cached data. as long as the resource changes, the ETag is regenerated.

Request Header If-none-match. If-none-match indicates that the client accesses the checksum value of the resource, which is the ETag

ETag is the last time a resource is loaded, the response header returned by the server is a unique identifier for the resource, and the ETag is regenerated as long as the resource changes. When the browser sends a request to the server the next time the resource is loaded, the last ETag value returned is placed in the If-none-match in the request header. The server only needs to compare the if-none-match of the client with the etag of the resource on its own server, and it will be well judged whether the resource has been modified relative to the client. If the server finds that the ETag does not match, it sends the new resource (and of course the new ETag) to the client directly in the form of a regular get 200 package, and if the etag is consistent, it returns 304 to the client directly using the local cache.
  

The comparison between ETag and last-modified:
First, in precision, the etag is superior to last-modified. Last-modified Time Unit is the second, if a file in 1 seconds change several times, then their last-modified actually does not reflect the changes, but the etag will change every time to ensure the accuracy; if it's a load-balanced server, The last-modified generated by each server may also be inconsistent.
Second in performance, the etag is inferior to last-modified, after all, last-modified only need to record time, and the ETag needs the server through the algorithm to calculate a hash value.
Third, on the priority level, the server check prioritizes the ETag

2. Response header Cache-control

Cache_-control is primarily used to control cache-related things. The Cache-control header is defined in HTTP 1.1, replacing the header used to define the response cache policy in the http1.0 version (Expires), for example

Cache directives mainly include:

1.public private public is used to declare that both the browser and the Web proxy server can be cached. Private means only browser caching is allowed, proxy server does not allow caching

2.no-cache No-cache does not mean not to cache, but to verify that the resource has changed before each request for the resource

3.no-store means no caching, that is, neither the browser nor the proxy server is cached.

4.max-age indicates that the cached content is invalidated after xxx seconds.

5.must-revalidate enables the client to browse the current page again by sending HTTP header information to the server for authentication before deciding whether to load the client local cache

Comparison of If-modified-since and last-modified

When the browser accesses the resource for the first time, the server returns the resource at the same time, adds the last-modified header in the response header, the value is the last modification time of the resource on the server, the cache file and header after the browser receives it;

    Last-modified:fri, 01:47:00 GMT

The next time the browser requests this resource, the browser detects a last-modified this header, so add if-modified-since this header, the value is the value of last-modified; The server receives this resource request again, depending on The value in If-modified-since is compared to the last modification time of this resource in the server, and if there is no change, returns 304 and an empty response body, read directly from the cache, if the if-modified-since time is less than the last modification time of this resource in the server, The description file is updated, so the new resource file and 200 are returned.



  

Ii. process for caching to take effect

Reference Original:https://www.cnblogs.com/mamimi/p/6900987.html

When the server receives the request, it returns the resource's last-modified and ETag headers in 200OK, which the client saves in the cache and records both properties. When a client needs to send the same request, if-modified-since and If-none-match Two headers are carried in the request. The value of two headers is the value of the last-modified and ETag headers in the response, respectively. The server uses these two headers to determine that the local resource has not changed and that the client does not need to re-download and return a 304 response. The common process is as follows:

 
third, the caching mechanism of HTTPReference: https://www.jianshu.com/p/54cc04190252
Personal understanding of Cache data failure: The individual believes that caching data failure is not the cache data disappearing in memory or on disk, but does not provide access, but it is still real with memory or disk.
Force cache invalidation: In fact, the time is more than expires or Cache-control in the max-age, resulting in cache data failure. Negotiation cache Invalidation: In fact, the file changes, the ETag changes. Force cache and Negotiate cache forced caching: Requests are not sent to the server, and resources are read directly from the browser cache.    In the network option in Chrome's console, you can see that the request returns a status code of 200, and size displays the from disk cache or from memory cache. The From memory cache represents the use of a cached buffer, and the from disk cache represents the cache on the hard disk, and the browser reads the cache in the order of memory–> disk. In the browser, the browser will be in the JS and pictures, such as file resolution directly into the memory cache, then when the page is refreshed simply read from the memory cache (from memories Cache), and the CSS file will be stored in the hard disk file, so each render page needs to read the cache from the hard disk ( From disk cache).
Caching mechanism WorkflowWhen the client requests the data for the first time, there is no corresponding cached data in the browser cache, the server needs to be requested, the server returns the data, and the data is stored to the browser cache.

There are two situations when requesting data for the second time: 1. Cache data is invalid.

2. Invalid cache data.

Cache identity: The value that the individual understands as the etag.

The negotiated cache is the process by which the browser initiates a request to the server with the cache identity to determine whether to use the cache, depending on the cache identity, after the forced cache is invalidated (that is, after the cache data is invalidated). There are two main types of cases:

1. The negotiation cache is in effect and returns 304 and not Modified.

    

2. Negotiate cache invalidation, return 200 and request results

        

A summary of the caching mechanism

Forcing the cache to take precedence over the negotiation cache, and if the force cache (expires and Cache-control) is in effect, uses the cache directly, and negotiates the cache if it does not take effect (last-modified/if-modified-since and ETag/ If-none-match), the negotiation cache is determined by the server whether to use the cache, if the negotiation cache fails, then the cache on behalf of the request expires, return 200, re-return the resource and cache identity, and then into the browser cache, the entry into force returned 304, continue to use the cache.

  

In-depth understanding HTTP (ii)------HTTP caching mechanism and principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.