Doing HTTP Caching Right: Introducing httplib2
by Joe Gregorio
|
Pages: 1, 2, 3, 4
Cache-Control
While validators are used to test if a cached entry is still valid, the Cache-Control: header is used to signal how long a representation can be cached. The most fundamental of all the cache-control directives is max-age. This directive asserts that the cached response can be only max-age seconds old before being considered stale. Note that max-age can appear in both request headers and response headers, which gives both the client and server a chance to assert how old they like their responses cached. If a cached response is fresh then we can return the cached response immediately; if it's stale then we need to validate the cached response before returning it.
Let's take another look at our example response from above. Note that the Cache-Control: header is set and that a max-age of 7200 means that the entry can be cached for up to two hours.
HTTP/1.1 200 OK
Date: Fri, 30 Dec 2005 17:32:47 GMT
Server: Apache
ETag: "0192384-9023-1a929893"
Accept-Ranges: bytes
Content-Length: 33286
Vary: Accept-Encoding,User-Agent
Cache-Control: max-age=7200
Expires: Fri, 30 Dec 2005 19:30:56 GMT
Content-Type: text/xml
There are lots of directives that can be put in the Cache-Control: header, and the Cache-Control: header may appear in both requests and/or responses.
| Directive | Description |
|---|---|
no-cache |
The cached response must not be used to satisfy this request. |
no-store |
Do not store this response in a cache. |
max-age=delta-seconds |
The client is willing to accept a cached reponse that is delta-seconds
old without validating. |
max-stale=delta-seconds |
The client is willing to accept a cached response that is no more than
delta-seconds stale. |
min-fresh=delta-seconds |
The client is willing to accept only a cached response that will still
be fresh delta-seconds from now. |
no-transform |
The entity body must not be transformed. |
only-if-cached |
Return a response only if there is one in the cache. Do not validate
or GET a response if no cache entry exists. |
| Directive | Description |
|---|---|
public |
This can be cached by any cache. |
private |
This can be cached only by a private cache. |
no-cache |
The cached response must not be used on subsequent requests without first validating it. |
no-store |
Do not store this response in a cache. |
no-transform |
The entity body must not be transformed. |
must-revalidate |
If the cached response is stale it must be validated before it is returned
in any response. Overrides max-stale. |
max-age=delta-seconds |
The client is willing to accept a cached reponse that is delta-seconds
old without validating. |
s-maxage=delta-seconds |
Just like max-age but it applies only to shared caches. |
proxy-revalidate |
Like must-revalidate, but only for proxies. |
Let's look at some Cache-Control: header examples.
Cache-Control: private, max-age=3600-
If sent by a server, this
Cache-Control:header states that the response can only be cached in a private cache for one hour. Cache-Control: public, must-revalidate, max-age=7200-
The included response can be cached by a public cache and can be cached for two hours; after that the cache must revalidate the entry before returning it to a subsequent request.
Cache-Control: must-revalidate, max-age=0-
This forces the client to revalidate every request, since a
max-age=0forces the cached entry to be instantly stale. See Mark Nottingham's Leveraging the Web: Caching for a nice example of how this can be applied. Cache-Control: no-cache-
This is pretty close to
must-revalidate, max-age=0, except that a client could use amax-staleheader on a request and get a stale response. Themust-revalidatewill override themax-staleproperty. I told you that giving both client and server some control would make things a bit complicated.
So far all of the Cache-Control: header examples we have looked at are on the response side, but they can also be added on the request too.
Cache-Control: no-cache-
This forces an "end-to-end reload," where the client forces the cache to reload its cache from the origin server.
Cache-Control: min-fresh=200-
Here the client asserts that it wants a response that will be fresh for at least 200 seconds.