Skip to content

Cache-Control

Description

The Cache-Control request header is used by clients to specify caching directives that control how caching mechanisms should handle requests and responses. It allows clients to define rules for storing and retrieving cached content, ensuring that fresh and relevant data is used while minimizing unnecessary network requests.

This header is crucial for optimizing performance and reducing server load by leveraging caching mechanisms effectively. It is widely used in HTTP/1.1 and works with both requests and responses.

Syntax

The syntax of the Cache-Control header follows this structure:

Cache-Control: <directive>[, <directive>]*
  • <directive>: A specific caching instruction (e.g., no-cache, max-age, no-store).
  • Multiple directives can be specified, separated by commas.

Example Syntax

Cache-Control: no-cache, no-store, must-revalidate
Cache-Control: max-age=3600

Examples

Prevent Caching

To prevent caching of a response, a client can send:

GET /resource HTTP/1.1
Host: example.com
Cache-Control: no-cache, no-store, must-revalidate

This instructs the server and intermediaries not to cache the response. The server might reply with:

HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, must-revalidate

Specify Cache Expiration

A client can request cached content that is no older than a specified time:

GET /data HTTP/1.1
Host: example.com
Cache-Control: max-age=600

This tells caching mechanisms that the content should be used only if it is no older than 600 seconds (10 minutes).

Requesting Fresh Data

To force the server to validate the cached content before using it:

GET /latest-news HTTP/1.1
Host: example.com
Cache-Control: no-cache

The no-cache directive ensures that cached content is revalidated with the server before being served.

Summary

The Cache-Control request header allows clients to define caching policies that optimize performance and ensure content freshness. By using directives like no-cache, no-store, max-age, and must-revalidate, clients can control how content is stored and served. Proper use of caching can significantly enhance web performance while reducing server load and network traffic.