Allow user agents to cache /diff responses

The /diff response is expensive to compute at the server side, and is
rather large to transfer to the client.  Allow user agents to cache
the resource locally for up to 7 days.

This should eventually allow navigation within a patch set to be
quicker as the browser will be able to load previously viewed files
from cache, saving round-trip time to the server.

Change-Id: I1c2f3e237a0c4c802564e5d5d87cd13d0d1b358f
This commit is contained in:
Shawn Pearce
2013-05-19 02:16:37 -07:00
parent cad2310922
commit 3fab79c200
4 changed files with 58 additions and 2 deletions

View File

@@ -19,6 +19,10 @@ public abstract class Response<T> {
@SuppressWarnings({"rawtypes"})
private static final Response NONE = new None();
public enum CacheControl {
NONE, PUBLIC, PRIVATE;
}
/** HTTP 200 OK: pointless wrapper for type safety. */
public static <T> Response<T> ok(T value) {
return new Impl<T>(200, value);
@@ -50,11 +54,14 @@ public abstract class Response<T> {
public abstract int statusCode();
public abstract T value();
public abstract CacheControl caching();
public abstract Response<T> caching(CacheControl c);
public abstract String toString();
private static final class Impl<T> extends Response<T> {
private final int statusCode;
private final T value;
private CacheControl caching = CacheControl.NONE;
private Impl(int sc, T val) {
statusCode = sc;
@@ -71,6 +78,17 @@ public abstract class Response<T> {
return value;
}
@Override
public CacheControl caching() {
return caching;
}
@Override
public Response<T> caching(CacheControl c) {
caching = c;
return this;
}
@Override
public String toString() {
return "[" + statusCode() + "] " + value();
@@ -90,6 +108,16 @@ public abstract class Response<T> {
throw new UnsupportedOperationException();
}
@Override
public CacheControl caching() {
return CacheControl.NONE;
}
@Override
public Response<Object> caching(CacheControl c) {
throw new UnsupportedOperationException();
}
@Override
public String toString() {
return "[204 No Content] None";