Skip to content

Cache Control Plugin

Todd Fredrich edited this page Jul 3, 2013 · 5 revisions

For GET requests, adds a Date: header, if not already present. This enables clients to determine age of a representation for caching purposes. Where <timestamp> is in RFC1123 full date format.

Note that HEAD requests are not provided with a Date header via this postprocessor. This is due to the fact that most external caches forward HEAD requests to the origin server as a GET request and cache the result.

For GET requests, adds caching control headers. May be used in conjunction with DateHeaderPostprocessor to add Date header for GET requests.

If the route has a Parameters.Cache.MAX_AGE parameter, whose value is the max-age in seconds then the following are added:

  • Cache-Control: max-age=<seconds>
  • Expires: now + max-age

If the route has a Flags.Cache.NO_CACHE flag, then the following headers are set on the response:

  • Cache-Control: no-cache
  • Pragma: no-cache

The MAX_AGE parameter takes precidence, in that, if present, the NO_CACHE flag is ignored.

If the response body is non-null, adds an ETag header. ETag is computed from the body object's hash code combined with the hash code of the resulting format.

NOTE: To fully support basic caching capability, also implement a LastModifiedHeaderPostprocessor() that inspects the date on your domain or presentation model and sets the 'Last-Modified' header.

Example Usage:

RestExpress server = new RestExpress();
...
new CacheControlPlugin()
    .register(server);
server.addPostprocessor(new LastModifiedHeaderProcessor());

Here's an example LastModifiedHeaderPostprocessor:

public class LastModifiedHeaderPostprocessor
implements Postprocessor
{
DateAdapter fmt = new HttpHeaderTimestampAdapter();

@Override
public void process(Request request, Response response)
{
	if (!request.isMethodGet()) return;
	if (!response.hasBody()) return;

	Object body = response.getBody();

	if (!response.hasHeader(LAST_MODIFIED) && body instanceof Timestamped)
	{
		response.addHeader(LAST_MODIFIED, fmt.format(((Timestamped) body).getUpdatedAt()));
	}
}
}
Clone this wiki locally