This article explains the difference between mandatory and negotiated caching in HTTP

1. What is caching?

Brower Caching enables the browser to cache previously requested files so that they can be reused for next access, saving bandwidth, improving access speed, and reducing server pressure

The HTTP caching mechanism is mainly set in the HTTP response header, where the relevant fields are Expires, Cache-Control, Last-Modified, and Etag.

HTTP 1.0 protocol. So, basically, you tell the browser that you can retrieve resources from the cache without having to run to the server to retrieve them.

The other:

  • Expires is set to Time and Greenwich Mean Time (GMT), not local Time, so it has a high Time requirement.

2. How does the browser decide whether to use caching

First request:

3. Cache types

Browser caches are divided into strong cache and negotiated cache

Strong cache: The browser reads the file directly from the local cache without sending any requests from the server and returns Status Code: 200 OK

200 Form memory cache: does not access the server. Usually, the resource has been loaded and cached in the server’s memory. When the browser closes, the data will not exist (the resource has been freed), and when the same page is opened again, the from Memory cache will not appear.

200 From Disk cache: does not access the server. The resource has been loaded at a previous time. The cache is directly read from the hard disk.

Memory cache is accessed first, disk cache is accessed second, and network resources are requested last

Negotiation cache: sends a request to the server. The server will judge whether the negotiation cache is matched according to some parameters of the request header. If yes, the server will return the 304 status code with a new Response header to inform the browser to read resources from the cache.

4. Strong cache and negotiation cache header parameters

Strong cache:

Expires: Expires. If you set a time, the browser will simply read the cache within the set time and will not request anything again

Cache-control: when max-age=300, it means that the strong Cache will be hit if the resource is reloaded within 5 minutes of the correct return time of the request, which is also recorded by the browser.

Cache-control: In addition to this field, there are several common Settings:

(1) Max-age: used to set how long resources can be cached, in seconds. (2) s-maxage: the same as max-age, but only for proxy server cache; (3) public: indicates that the response can be cached by any cache; (4) private: only for individual users, and cannot be cached by proxy servers; (5) no-cache: Forces the client to send requests directly to the server, that is, each request must be sent to the server. The server receives the request and determines if the resource has changed, returns new content if it has, or 304 unchanged if it has not. This can be very misleading and can be mistaken for a response that is not cached. Cache-control: no-cache is actually cached, but the Cache evaluates the validity of the cached response to the server each time it provides response data to the client (browser). (6) no-store: disables all caching.Copy the code

Cache-control is an HTTP1.1 header field, and Expires is an HTTP1.0 header field. If both expires and cache-Control exist, cache-Control overwrites expires.

Negotiation cache:

Last-modifed/if-modified-since and Etag/ if-none-match are paired separately in a one-to-one relationship

Etag/If None – Match:

Etag:

Etag is an HTTP 1.1 attribute that is generated by the server (Apache or another tool) and returned to the front end to help the server control caching validation on the Web side. In Apache, the value of ETag is hashed by INode, Size, and last modified time.

If-None-Match:

When the resource expires and the browser finds an Etag in the response header, it will apply the request header if-none-match(the value of the Etag) to the server’s request. The server receives the request for comparison and decides to return either 200 or 304

The Last – the Modifed/If – Modified – Since:

Last-modified:

The browser sends the last modification of the resource to the server

The if-modified-since:

When a resource expires (the browser determines the max-age expiration of the cache-control identifier) and finds a last-Modified statement in the response header, the browser sends a request to the server again with if-Modified-since, indicating the request time. If the request is received, the server finds if-modified-since and compares it with the Last modified time of the requested resource (last-modified). If the Last modified time is new (large), it indicates that the resource has been modified again, and returns the latest resource, HTTP 200 OK; If the last modification time is earlier (smaller), the resource is not updated, and the response is HTTP 304.

  • Last-modifed/if-Modified-since time accuracy is in seconds, whereas Etag can be more accurate.
  • The Etag priority is higher than last-modiFed, so the server validates the Etag first
  • Last-modifed/if-Modified-since is the header field for HTTP1.0