Summary of HTTP caching

2022-05-15 0 By

Foreword caching is a technique for saving a copy of a resource and using it directly on the next request.We use HTTP caching, which reduces client wait time and network traffic by reusing cached resources, as well as alleviating pressure on the server.Can significantly improve the performance of our website and application.Although HTTP caching is not necessary, it is often necessary to reuse cached resources, and HTTP caching is an important tool for Web performance optimization.Types of HTTP cache There are two types of HTTP cache policies: strong cache negotiated cache.The difference can be seen literally: strong caching forces the cache to be used directly.Negotiating a cache means you have to negotiate with the server to see if the cache works.Strong cache Strong cache does not send a request to the server, but reads resources directly from the cache. In the Network option of the Chrome console, this request returns a status code of 200, and size displays from disk cache or from Memory cache.Negotiation cache The negotiation cache first sends a request to the server. The server determines whether the negotiation cache is matched according to some parameters of the request header. If yes, the server returns a 304 status code with a new Response header to inform the browser to read resources from the cache.HTTP cache control In HTTP, we can control cache policy by setting response headers and request headers.Strong caching can be achieved by setting both Expires and cache-control response headers.If both exist, cache-control takes precedence over Expires.The ExpiresExpires response header, which is a product of HTTP/1.0.Represents the expiration time of the resource. The value is an absolute time.It tells the browser that data can be accessed directly from the browser cache before expiration time.Because it is an absolute time, the time difference or error between the client and the server may cause the time inconsistency between the client and the server, resulting in cache hit errors.If a max-age or s-max-age directive is set in the cache-control response header, Expires is ignored.Expires: Wed, 21 Oct 2015 07:28:00 GMtcache-controlCache-control appears in HTTP/1.1.The caching mechanism can be implemented by specifying multiple instructions.This parameter is used to indicate the maximum duration of the resource cache.That is, the client does not need to send requests to the server during this time.Precedence over Expires.The value of expiration time instruction is relative time, which solves the problem of absolute time.Cache-control: max-age= 315360000Cache-control has many attributes. Different attributes have different meanings.Cacheability public indicates that the response can be cached by any object, including: the client sending the request, the proxy server, and so on.Private Indicates that the response can only be cached by a single user and cannot be cached as a shared cache (that is, the proxy server cannot cache it). No-cache Does not use strong caching and requires negotiation with the server for cache validation.The no-store cache should not store anything about client requests or server responses, that is, no cache is used.Expiration max-age= Maximum period of cache storage. If the period exceeds this period, the cache is considered expired.S-maxage = Sets the shared cache.Max-age and Expires are overridden, and private caches ignore it max-stale[=] The client is willing to accept an expired resource. An optional number of seconds can be set to indicate that the response cannot be stale beyond that given time.Min-fresh = The client wants to get the latest response within the specified time to revalidate and reload must-revalidate if the page expires, go to the server to get it.Proxy-revalidate works the same as must-revalidate, but is used for shared caches.The other only-if-cached does not make network requests and only uses caching.No-transform Does not convert or transform resources.For example, image formats cannot be converted.The negotiated cache can be controlled with last-modified/if-modified-since and ETag/ if-none-match pairs of headers.The last-modified, if-Modified-sincelast-modified, and if-Modified-since values are all time strings in GMT format, representing the time when the file was Last Modified.When the server responds to a request, last-Modified tells the browser when the resource was Last Modified.When the browser requests the server again, the request header contains the last-Modified field, followed by the Last modification time obtained in the cache.The server receives the request with if-modified-since, and compares it with the last Modified time of the requested resource. If the request is consistent, 304 and the response header are returned. The browser only needs to retrieve the information from the cache.If it has been modified, then start transmitting the response as a whole, and the server returns:200 OK But it is often the case on a server that a resource has been Modified but its actual contents have not changed at all, and the entire entity is returned to the client because of a last-Modified time mismatch (even if the client has an identical resource in its cache).To solve this problem, HTTP/1.1 introduced Etag.Etag has a high priority and last-Modified.Etag and if-none-matchetag are unique identifiers generated by the server for each resource. Just like a fingerprint, Etag changes due to resource changes regardless of the last modification time. Etag ensures that each resource is unique.When the browser initiates a request, the request header of the browser contains if-none-match. The value of the Etag returned last time is sent to the server. After receiving the request, the server compares if-none-match with the unique identifier of the requested resource.If the same information indicates that the resource has not been modified, the response is 304, and the browser directly obtains the data from the cache.If not, it indicates that the resource has been changed. The status code 200 is returned in response to the entire resource content.Summary From the previous section, we learned that HTTP caching is divided into mandatory caching and negotiated caching.The mandatory Cache is controlled by cache-control, Exipres (HTTP1.0).The browser reads directly from the local cache and does not interact with the server. Status code 200.The negotiated cache is implemented by last-modified/ifModified-since, Etag/if-none-match. Each request requires the server to determine whether the resource has been updated to determine whether the browser is using the cache. If so, 304 will be returned, otherwise the response will be complete.~~ This article is over, thanks for reading!Learn interesting knowledge, meet interesting friends, build interesting soul!Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!