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

@@ -33,6 +33,7 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gwtexpui.server.CacheHeaders;
import com.google.inject.Inject;
import org.kohsuke.args4j.CmdLineException;
@@ -79,6 +80,7 @@ class ParameterParser {
msg.write('\n');
clp.printUsage(msg, null);
msg.write('\n');
CacheHeaders.setNotCacheable(res);
replyBinaryResult(req, res,
BinaryResult.create(msg.toString()).setContentType("text/plain"));
return false;

View File

@@ -112,6 +112,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.zip.GZIPOutputStream;
@@ -184,7 +185,6 @@ public class RestApiServlet extends HttpServlet {
protected final void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
long auditStartTs = System.currentTimeMillis();
CacheHeaders.setNotCacheable(res);
res.setHeader("Content-Disposition", "attachment");
res.setHeader("X-Content-Type-Options", "nosniff");
int status = SC_OK;
@@ -302,9 +302,13 @@ public class RestApiServlet extends HttpServlet {
@SuppressWarnings("rawtypes")
Response r = (Response) result;
status = r.statusCode();
configureCaching(req, res, r);
} else if (result instanceof Response.Redirect) {
CacheHeaders.setNotCacheable(res);
res.sendRedirect(((Response.Redirect) result).location());
return;
} else {
CacheHeaders.setNotCacheable(res);
}
res.setStatus(status);
@@ -349,6 +353,26 @@ public class RestApiServlet extends HttpServlet {
}
}
private static <T> void configureCaching(HttpServletRequest req,
HttpServletResponse res, Response<T> r) {
if ("GET".equals(req.getMethod())) {
switch (r.caching()) {
case NONE:
default:
CacheHeaders.setNotCacheable(res);
break;
case PRIVATE:
CacheHeaders.setCacheablePrivate(res, 7, TimeUnit.DAYS);
break;
case PUBLIC:
CacheHeaders.setCacheable(req, res, 7, TimeUnit.DAYS);
break;
}
} else {
CacheHeaders.setNotCacheable(res);
}
}
private void checkPreconditions(HttpServletRequest req, RestResource rsrc)
throws PreconditionFailedException {
if ("*".equals(req.getHeader("If-None-Match"))) {
@@ -818,6 +842,7 @@ public class RestApiServlet extends HttpServlet {
static void replyError(HttpServletResponse res, int statusCode, String msg)
throws IOException {
res.setStatus(statusCode);
CacheHeaders.setNotCacheable(res);
replyText(null, res, msg);
}