Cache-Control Headers
Andrew Antonopoulos
Senior Solutions Architect at Sony Professional Solutions Europe
The performance of content that is available via web sites and applications can be significantly improved by reusing previously fetched resources. Web caches reduce latency and network traffic as well as the time needed to display a representation of a resource. By making use of HTTP caching, Web sites become more responsive.
There are several kinds of caches: these can be grouped into two main categories:
A?shared cache?is a cache that stores responses for reuse by more than one user. A?private cache?is dedicated to a single user.
The primary cache key consists of the request method and target URI (oftentimes only the URI is used as only GET requests are caching targets). Common forms of caching entries are:
The?Cache-Control HTTP/1.1 general-header field is used to specify directives for caching mechanisms in both requests and responses.
The headers which can be used in the header field are:
Cache-Control: Max-Age
The max-age request directive defines, in seconds, the amount of time it takes for a cached copy of a resource to expire. After expiring, a browser must refresh its version of the resource by sending another request to a server.
Cache-Control: No-Cache
The no-cache directive means that a browser may cache a response, but must first submit a validation request to an origin server.
Cache-Control: No-Store
The no-store directive means browsers aren’t allowed to cache a response and must pull it from the server each time it’s requested. This setting is usually used for sensitive data, such as personal banking details.
Cache-Control: Public
The public response directive indicates that a resource can be cached by any cache.
领英推荐
Cache-Control: Private
The private response directive indicates that a resource is user specific—it can still be cached, but only on a client device. For example, a web page response marked as private can be cached by a desktop browser, but not a content delivery network (CDN).
Additional HTTP Cache Headers
Expires?– This header specifies a fixed date/time for the expiration of a cached resource. The expires header is ignored when a cache-control header containing a max-age directive is present.
ETag?– A response header that identifies the version of served content according to a token – a string of characters in quotes, e.g., “675af34563dc-tr34” – that changes after a resource is modified. If a token is unchanged before a request is made, the browser continues to use its local version.
Vary?– A header that determines the responses that must match a cached resource for it to be considered valid. For example, the header Vary: Accept-Language, User-Agent specifies that a cached version must exist for each combination of user-agent and language.
Sources: