[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [onionoo/master] Add Cache-Control header.
commit cbc03ebf6e0a3ad772440f62357ea3baa7c165e5
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Wed Aug 13 15:54:16 2014 +0200
Add Cache-Control header.
Set max-age to a value between 5 and 40 minutes, depending on how old the
node index is.
---
.../org/torproject/onionoo/server/ResourceServlet.java | 14 +++++++++++---
.../java/org/torproject/onionoo/util/DateTimeHelper.java | 1 +
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/src/main/java/org/torproject/onionoo/server/ResourceServlet.java b/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
index 481462e..ee71218 100644
--- a/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
+++ b/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
@@ -61,12 +61,18 @@ public class ResourceServlet extends HttpServlet {
return;
}
- if (ApplicationFactory.getNodeIndexer().getLastIndexed(
- DateTimeHelper.TEN_SECONDS) + DateTimeHelper.SIX_HOURS
- < ApplicationFactory.getTime().currentTimeMillis()) {
+ long nowMillis = ApplicationFactory.getTime().currentTimeMillis();
+ long indexWrittenMillis =
+ ApplicationFactory.getNodeIndexer().getLastIndexed(
+ DateTimeHelper.TEN_SECONDS);
+ long indexAgeMillis = nowMillis - indexWrittenMillis;
+ if (indexAgeMillis > DateTimeHelper.SIX_HOURS) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
+ long cacheMaxAgeMillis = Math.max(DateTimeHelper.FIVE_MINUTES,
+ ((DateTimeHelper.FOURTY_FIVE_MINUTES - indexAgeMillis)
+ / DateTimeHelper.FIVE_MINUTES) * DateTimeHelper.FIVE_MINUTES);
NodeIndex nodeIndex = ApplicationFactory.getNodeIndexer().
getLatestNodeIndex(DateTimeHelper.TEN_SECONDS);
@@ -304,6 +310,8 @@ public class ResourceServlet extends HttpServlet {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
+ response.setHeader("Cache-Control", "public, max-age="
+ + (cacheMaxAgeMillis / DateTimeHelper.ONE_SECOND));
PrintWriter pw = response.getWriter();
rb.buildResponse(pw);
int relayDocumentsWritten = rh.getOrderedRelays().size();
diff --git a/src/main/java/org/torproject/onionoo/util/DateTimeHelper.java b/src/main/java/org/torproject/onionoo/util/DateTimeHelper.java
index 1fcf6e1..b350b9a 100644
--- a/src/main/java/org/torproject/onionoo/util/DateTimeHelper.java
+++ b/src/main/java/org/torproject/onionoo/util/DateTimeHelper.java
@@ -19,6 +19,7 @@ public class DateTimeHelper {
ONE_MINUTE = 60L * ONE_SECOND,
FIVE_MINUTES = 5L * ONE_MINUTE,
FIFTEEN_MINUTES = 15L * ONE_MINUTE,
+ FOURTY_FIVE_MINUTES = 45L * ONE_MINUTE,
ONE_HOUR = 60L * ONE_MINUTE,
FOUR_HOURS = 4L * ONE_HOUR,
SIX_HOURS = 6L * ONE_HOUR,
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits