HTTP cache

Detailed description of the caching options available through the HTTP protocol

Caching is a technique that can help network connections faster, because the less content that needs to be transferred, the better.

Many resources can be very large and very expensive in terms of time and actual cost of retrieval (for example, on mobile devices).

HTTP provides different caching strategies, and browsers use them.

No cache

first of all,Cache-ControlThe header can tell the browser to never use the cached version of the resource without first checking the ETag value (more on that later) by usingno-cachevalue:

Cache-Control: no-cache

More restrictiveno-storeThe option tells the browser (and all intermediate network devices) not to even store the resource in its cache:

Cache-Control: no-store

in caseCache-ControlHavemax-ageValue, used to determine the number of seconds that this resource is valid as a cache:

Cache-Control: max-age=3600

ThisExpiresHeader

After sending the HTTP request, the browser will check whether it has a copy of the page in the cache based on the URL you need.

If it exists, it will check if the page existsfresh.

If the page is freshHTTP responseExpiresHeaderThe value is less than the current date and time.

The Expires header takes the following form:

Expires: Sat, 01 Dec 2018 16:00:00 GMT

Conditional GET

There are many ways to perform condition acquisition. All are based on usingIf-*Request header:

  • useIf-Modified-SincewithLast-Modified
  • useIf-None-MatchwithETag

useIf-Modified-SincewithLast-Modified

The browser can send a request to the server, not just request a page, it also adds aIf-Modified-SinceHeader,based onLast-ModifiedThe header value obtained from the currently cached page.

This tells the server that if the resource has been updated since that date, only the response body (page content) is returned.

Otherwise, the server returns a304 Not ModifiedReply.

useIf-None-MatchwithETag

Web server (depending on settings, how the page is provided, etc.) can be sentETag header.

That is the identifier of the resource. Every time a resource changes (such as an update), the ETag should also change.

It's like a checksum.

The browser sends aIf-None-MatchHeaderContains one (or more) ETag values.

If there is no match, the server returns the latest version of the resource, otherwise it returns304 Not ModifiedReply.


More web tutorials: