HTTP caching study notes

Various types of cache

Caching is a technique to save a copy of the resource and use the copies directly on the next request. When the web cache found the requested resource has been stored, it intercepts the request and returns a copy of the resource, and not to re-download the source server.
There are many types of caches that generally fall into two categories: private and shared cache. Shared cache memory in response to a plurality of users can be used only for individual users private cache.
The following describes the browser cache, in addition to proxy caching, gateway cache, CDN, caching reverse proxy and load balancer deployed on the server, etc., to provide better stability and performance for sites and web applications and scalability.
Common HTTP GET response cache can only store for other types of responses are powerless.

Cache Rules

To facilitate understanding, we believe that there is a browser cache database for storing cached information (in fact static resources are cached in memory and on disk), when the browser first request data, at this time there is no corresponding cache database cache data, you need to request a server, the server will return data and cache rules, the browser will cache the rules and data storage into the cache database.
We can be divided into two categories strong caching, caching negotiation

Strong Cache

If it is judged browser local cache has not expired, it is used directly without initiating http request.
Cache-Control is the most important rule, the default is private.

private     私有缓存
public      共享缓存
max-age     缓存的内容将在 xxx 秒后失效
no-cache    需要使用对比缓存来验证缓存数据
no-store    所有内容都不会缓存,强缓存、协商缓存都不会触发

Note: In the HTTP version 1.0, the absolute time Expires field is obtained from the server, because the request takes time, so the request time with the server browser receives a request acquired time errors, which also led to a cache hit errors in HTTP 1.1 version, since the cache-Control value max-age = xxx xxx is the relative time in units of seconds, so the browser receiving the resource start the countdown, to avoid a cache hit in the HTTP 1.0 error shortcomings, to be compatible with the low version of HTTP protocol, in response to the normal development of two heads simultaneously used, version 1.1 of HTTP achieve higher priority than HTTP 1.0.

Cache consultation

First browser request data, the server will return with the data cache identifier to the client, the client cache database backup to both. When the request data again, the client cache identifier is sent to the backup server, the server identifier is determined according to the buffer, after determining the success, returned status code 304, the client notifies the comparison is successful, the data cache may be used.

HTTP 1.0

If-Modified-Since / Last- Modified
these two are paired, are cached content negotiation, where the browser is the head of If-Modified-Since, while the server is the Last-Modified, its role is, when initiating the request, if the If-Modified-Since and Last-Modified match, on behalf of server resources has not changed, so the server does not return the resource entity, but only to return the head, tells the browser can use the local cache. Last-Modified, by definition, refers to the last file modification time, and can only be accurate to within 1s.

HTTP 1.1

If-None-Match / E- tag
these two are paired, the contents of the cache belonging to negotiation, which is the browser header If-None-Match, while the server is the E-tag, similarly, after making the request if the If-None-match and E-tag match, it means that the content has not changed, notify the browser uses the local cache, and different Last-Modified, E-tag more precisely, it is something similar to fingerprints, based FileEtag INode mtime Size generation, as long as the files change, the fingerprint will change, and there is no limit 1s accuracy.

To make caching policy more robust, flexible, HTTP 1.0 and HTTP 1.1 version cached version of the strategy will be used simultaneously, and even force the cache and cache consultation will be used simultaneously for mandatory cache, the server tells the browser cache a time, within the cache time , the next request, the cache directly, beyond the effective time, perform caching policy consultation, negotiation for the cache, the cache information in Etag and Last-Modified header sent to the server by requesting the If-None-Match and If-Modified-Since, when setting up a new cache at the same time forced by the server check, check and return the 304 status code through the browser directly using the cache, the cache if negotiations have not hit, the server resets identity negotiation cache.

Guess you like

Origin www.cnblogs.com/superlizhao/p/12667904.html