Home >>HTTP Tutorial >HTTP Caching

HTTP Caching

HTTP Caching

Typically, HTTP is used for distributed information systems, where the use of response caches can improve performance. The HTTP/1.1 protocol contains a number of elements designed to make caching work.

The goal of caching in HTTP/1.1 is to eliminate the need to send requests in many cases, and in many other cases to eliminate the need to send full replies.

In HTTP/1.1 the basic cache mechanisms are implicit directives for caches where expiration times and validators are specified by the server. To this end, we use the header Cache-Control.

The header Cache-Control allows a client or server to convey a variety of directives in either requests or responses. Typically these directives override the standard caching algorithms. The directives for caching are set out in a comma-separated list. Take , for example:

Cache-control: no-cache

The client may use the following cache request directives in its HTTP request:

Sr No Cache Request Directive and Description
1. no-cache
A cache must not use the answer to satisfy a subsequent request with the origin server without successful revalidation.
2. no-store
Nothing about the client request or the server response should be stored in the cache.
3. max-age = seconds
Indicates the client's willingness to accept a response whose age in seconds is no greater than the time specified.
4. max-stale [ = seconds ]
Indicates that the client is prepared to accept a response that has exceeded its expiry time. If seconds are given, they should not expire by more than that time.
5. min-fresh = seconds
Indicates that the client is willing to accept a response whose lifetime of freshness is no less than its current age plus the time in seconds specified.
6. no-transform
Does not convert the entity-body.
7. only-if-cached
Recovers no new data. The cache can only send a document if it is in the cache, and should not contact the origin-server to see if there is a newer copy.

The server may use the following cache response directives in its HTTP response:

Sr No Cache Response Directive and Description
1. public
Indicates that every cache can cache the response.
2. private
Indicates that all or part of the response message is for a single user and must not be cached through a shared cache.
3. no-cache
Without successful re-validation with the origin server, a cache must not use the response to satisfy a subsequent request.
4. no-store
Nothing about the client request or the server response should be stored in the cache.
5. no-transform
Does not convert the entity-body.
6. must-revalidate
Before using it, the cache must check the status of stale documents, and should not use expired documents.
7. proxy-revalidate
The proxy-revalidate directive has the same meaning as the directive on the need-revalidate except that it does not apply to caches of non-shared user agents.
8. max-age = seconds
Indicates the client's willingness to accept a response whose age in seconds is no greater than the time specified.
9. s-maxage = seconds
The maximum age specified by this directive overrides the maximum age specified by either the directive max-age or the header Expires. A private cache shall always ignore the s-maxage directive