[tor-commits] [metrics-web/master] Cache .csv files and graph data, too.

karsten at torproject.org karsten at torproject.org
Tue Mar 20 11:32:24 UTC 2012


commit 77384cf27925203b92573320d12811126cffef7e
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Tue Mar 20 12:15:35 2012 +0100

    Cache .csv files and graph data, too.
    
    So far we were only caching graph images and table data, but there's no
    reason why we shouldn't cache .csv files and graph data, too.  Speeds up
    subsequent requests for the same .csv file or graph data by a lot.
---
 src/org/torproject/ernie/web/RObjectGenerator.java |   37 ++++++++++++--------
 1 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/src/org/torproject/ernie/web/RObjectGenerator.java b/src/org/torproject/ernie/web/RObjectGenerator.java
index 10aa3b8..814d79b 100644
--- a/src/org/torproject/ernie/web/RObjectGenerator.java
+++ b/src/org/torproject/ernie/web/RObjectGenerator.java
@@ -99,24 +99,31 @@ public class RObjectGenerator implements ServletContextListener {
    * has a placeholder for the absolute path to the file to be created. */
   public String generateCsv(String rQuery, String csvFilename) {
 
-    /* Update the R query to contain the absolute path to the file to be
-     * generated, create a connection to Rserve, run the R query, and
-     * close the connection. The generated csv file will be on disk in the
-     * same directory as the generated graphs. */
+    /* See if we need to generate this .csv file. */
     File csvFile = new File(this.cachedGraphsDirectory + "/"
         + csvFilename);
-    rQuery = String.format(rQuery, csvFile.getAbsolutePath());
-    try {
-      RConnection rc = new RConnection(rserveHost, rservePort);
-      rc.eval(rQuery);
-      rc.close();
-    } catch (RserveException e) {
-      return null;
-    }
+    long now = System.currentTimeMillis();
+    if (!csvFile.exists() || csvFile.lastModified() < now
+        - this.maxCacheAge * 1000L) {
 
-    /* Check that we really just generated the file */
-    if (!csvFile.exists()) {
-      return null;
+      /* We do. Update the R query to contain the absolute path to the
+       * file to be generated, create a connection to Rserve, run the R
+       * query, and close the connection. The generated csv file will be
+       * on disk in the same directory as the generated graphs. */
+      rQuery = String.format(rQuery, csvFile.getAbsolutePath());
+      try {
+        RConnection rc = new RConnection(rserveHost, rservePort);
+        rc.eval(rQuery);
+        rc.close();
+      } catch (RserveException e) {
+        return null;
+      }
+
+      /* Check that we really just generated the file */
+      if (!csvFile.exists() || csvFile.lastModified() < now
+          - this.maxCacheAge * 1000L) {
+        return null;
+      }
     }
 
     /* Read the text file from disk and write it to a string. */



More information about the tor-commits mailing list