What Is the Cache-Control Header?

Cache control is an HTTP header used to specify browser caching policies. The Cache-Control header that specifies the caching policies of server responses as well as client requests. Cache-control it gives information about the manner in which a resource is cached, location of the cached resource, and its maximum age attained before getting expired i.e. time to live.

The following example of the HTTP Response Header from www.apitier.com

What are HTTP headers?

The Hypertext Transfer Protocol (HTTP) outlines the syntax for communications on the World Wide Web, and this communication consists of requests from clients to servers and responses from servers back to clients. These HTTP requests and responses each come stamped with a series of key-value pairs called headers.

Syntax

Cache-Control: <directive> [, <directive>]*

Caching directive

  • Some directives have an optional argument
  • Multiple directives are comma-separated
  • case-insensitive
  • Affect caching on both response directives and request directives

1. Cache-Control: no-cache

The no-cache directive indicates that the response can be stored in caches without any restriction even if it is non-cacheable. But the response must be validated with the origin server before each reuse, even when the cache is disconnected from the origin server.

2. Cache-Control: No-Store

The no-store directive indicates that the response cannot be stored by any cache. This is usually used for sensitive data, such as personal banking details.

3. Cache-Control: max-age

The max-age request directive defines in seconds. It indicates the response remains fresh until N seconds after the response is generated.

4. Cache-Control: Public

The public response directive indicates that the response can be stored by any cache without any restriction. In case the response is non-cacheable, it can still be cached.

5. Cache-Control: Private

The private response directive indicates that the only browser cache is eligible to store the response.
A response with a ‘private’ directive can only be cached by the client and never by an intermediary agent, For example CDN or a proxy. These are often resources containing private data, such as a website displaying a user’s personal information.

Use cases

  • Preventing storing
  • Caching static assets with “cache-busting”
  • Up-to-date contents always
  • clearing already-stored responses from caches

Supported Browsers

  • Google Chrome
  • Edge
  • Firefox
  • Internet Explorer
  • Opera
  • Safari

What is browser caching?

Browser caching is when a web browser saves website resources so it doesn’t have to fetch them again from a server. For example, a background image on a website might be saved locally in cache so that when a user visits that page for the second time, the image will load from the user’s local files and the page will load much faster.

Browsers will only store these resources for a specified period of time, known as the Time To Live (TTL).