This is the 15th day of my participation in the August More Text Challenge

preface

HTTP cache should be the front developing one of the most often contact caching mechanism, it can be divided into mandatory buffer cache and negotiation, the two biggest difference is that a judge a cache hit, the browser will need to ask the server to negotiate the cached information, and then determine whether need to the content of the response to the request

Mandatory cache

In the case of the forced cache, if the browser determines that the requested target resource is a valid hit, it can return the request response directly from the forced cache without any communication with the server.

Part of a common response header:

access-control-allow-origin: *
age: 734978
cache-control: max-age=3153600
content-length: 40830
content-type: image/jpeg
date: Sun, 15 Aug 2021 22:08:28 GMT
expires: Sun, 15 Aug 2022 22:08:28 GMT 
Copy the code

The two fields related to forced caching are Expires and cache-control. Expires is a field declared in HTTP1.0 to control the expiration date and timestamp of the cache. It is specified by the server and notified by the response header to the browser, which will cache the response body with this field.

If the browser makes the same resource request again, it compares expires with the current local timestamp. If the local timestamp of the current request is less than the value of expires, the response cached by the browser has not expired and can be used directly without making a request to the server. Only when the local timestamp is greater than the expires value and the cache expires can a request be re-made to the server.

Judging from the above mandatory cache is out of date mechanism of it is not hard to see, this means there is a big loophole, namely of a strong reliance on local timestamp, local time if the client and the server time out of sync, or the client time to take the initiative to change, then for the judgment of the cache expiration may be prevented from and expectations.

To address the limitations of Expires, cache-Control fields have been added since HTTP1.1 to extend and improve expires capabilities. Cache-control sets max-age=31536000 to control the validity period of the response resource. This is the length of time in seconds, indicating that the resource will be valid for 31536000 seconds after the request is received. This avoids the problem of server – and client-side timestamps being out of sync. In addition, cache-Cotrol can be configured with other property values to more accurately control the cache, as described below.

No – the cache and no – the store

The no-cache function does not mean that the cache is not used. It means that the negotiated cache is forcibly implemented. That is, the server negotiates with the server to verify the validity of the cache for each request. If no-store is set, no cache policy is allowed. Each request from the client requires a new response from the server. No-cache and no-store are mutually exclusive attributes and cannot be set at the same time.

Private and public

Private and public are also a set of mutually exclusive cache-control attributes that specify whether a response resource can be cached by a proxy server. If the cache-Control field in the resource response header sets the public property value, the response resource can be cached by both the browser and the proxy server. Private limits the response resource to be cached only by the browser. If not explicitly specified, the default value is private.

Max – age and s – maxage

The value of the Max −age attribute is more commonly used than s− Maxage and indicates how long the server tells the client browser that the response resource has expired. It can be used in general project scenarios, but large architecture projects usually involve the use of a variety of proxy servers, which needs to consider the effectiveness of caching on proxy servers. This is why s-maxage exists. It represents the expiration time cached in the proxy server and is valid only if the public property value is set.

So cache-control can be a complete replacement for Expires, and with some cache control features it doesn’t have, it’s enough to use it in project practice. The only reason expires still exists is because of downward compatibility in terms of usability.