In ancient times, not only the world’s talent, but also the indomitable ambition – Su Shi

Say first concept

The browser caching mechanism, also known as the HTTP caching mechanism, is based on the cache identity of HTTP packets.

What is a cache

Caching is a technique for saving a copy of a resource and using it directly on the next request. When the Web cache discovers that the requested resource has been stored, it intercepts the request and returns a copy of the resource rather than going to the source server to download it again.

Caches need to be configured properly because not all resources are permanent. It is important that the cache of a resource expires until its next change (i.e., no cache of expired resources).

Advantages of caching

Caching server-side data has the following advantages:

  • Reduces resource consumption and operating pressure on the server and improves the overall performance of the server.
  • Reduce the latency of server-side resource loading, which in turn reduces the time it takes to display a resource.
  • Reduce bandwidth pressure and prevent network congestion
  • Web sites are becoming more responsive

Caching application

Common HTTP caches store only GET responses, not other types of responses.

Common cache cases:

  • Successful response to retrieve request: If the response status code is 200, it is successful. Contains responses such as HTML documents, images, or files.
  • Constant redirection: Response status code 301.
  • Error response: a page with response status code 404.
  • Incomplete response: The response status code is 206 and only partial information is returned.
  • If a response is matched as a defined cache key name, except for a GET request.

The process of caching mechanism

When the browser has no caching mechanism, when the browser makes an HTTP request, the server responds to the request, repeating the result each time

When the browser has a caching mechanism, when the browser makes an HTTP request, the server responds to the request, caches it, and when the same request is made, reads the data from the cache.

Cache type

Private cache

Private caches can only be used by individual users. The browser cache holds all documents downloaded by the user over HTTP. These caches provide backward/forward navigation of browsed documents, save web pages, view source code, and so on, avoiding unnecessary requests to the server again. It also provides offline browsing of cached content.

The private Cache is set to cache-control with a private value

Shared cache

The shared cache can be used by multiple users. For example, an ISP or a company might set up a Web proxy to provide to users as part of the local network infrastructure. Hot resources can then be reused, reducing network congestion and latency.

The shared Cache is set to cache-control as public

The cache control

The cache-control header

HTTP/1.1 defines the cache-control header to distinguish between support for caching mechanisms. Both request and response headers support this property. The cache policy is defined by the different values it provides.

  • Disable caching

    Cache-Control: no-store
    Cache-Control: no-cache, no-store, must-revalidate
    Copy the code
  • Forced acknowledgement cache

    Cache-Control: no-cache
    Copy the code
  • Cache expiration mechanism

    Cache-Control: max-age=604800
    Copy the code

    Cached content will expire after 604,800 seconds (one week)

  • Cache validation

    Cache-Control: must-revalidate
    Copy the code

Pragma header

The Pragma header is a header attribute defined in the HTTP/1.0 standard. Including Pragma in a request has the same effect as defining cache-control: no-cache in the header. However, HTTP response headers do not support this property, so it cannot be used as a complete replacement for the cache-control header defined in HTTP/1.1. Pragma is generally defined for backward compatibility with HTTP/ 1.0-based clients.

Pragma: no-cache
Copy the code

Expires header

The Expires response header contains the date/time after which the response Expires.

  • An invalid date, such as 0, represents a past date, meaning that the resource has expired.
  • If theCache ControlThe response header is setmax-ageors-max-ageInstruction, thenExpiresThe head will be ignored.
Expires: Tue, 15 Dec 2020 01:38:36 GMT
Copy the code

(after)