From 99a01f1a6e3dbe441398dfbe2d2f386bf4e7e88d Mon Sep 17 00:00:00 2001 From: Knut Forkalsrud Date: Thu, 7 Nov 2013 21:10:21 -0800 Subject: [PATCH] Get rid of separate thread for caching --- .../forkalsrud/album/web/AlbumServlet.java | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java index 2062da6..60faddc 100644 --- a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java +++ b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java @@ -9,8 +9,6 @@ import java.io.PrintWriter; import java.util.Calendar; import java.util.Date; import java.util.Properties; -import java.util.Timer; -import java.util.TimerTask; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; @@ -102,10 +100,10 @@ public class AlbumServlet ThumbnailDatabase thumbDb; DirectoryDatabase dirDb; MovieDatabase movieDb; - Timer timer; Entry cachedRootNode = null; MovieCoder movieCoder; DirectoryEntryFactory dirEntryFactory; + long nextCacheRefresh; @Override public void init() @@ -129,14 +127,7 @@ public class AlbumServlet log4jInit("/log4j.properties"); log.info("in init of Album"); long minute = 60 * 1000L; - timer = new Timer("cache evictor"); - timer.schedule(new TimerTask() { - - @Override - public void run() { - clearDirCache(); - } - }, minute, minute); + nextCacheRefresh = System.currentTimeMillis() + minute; base = new File(props.getProperty("base", "photos")).getAbsoluteFile(); basePrefix = "/" + base.getName(); @@ -223,6 +214,13 @@ public class AlbumServlet return; } + long now = System.currentTimeMillis(); + if (now > nextCacheRefresh) { + cachedRootNode = null; + long minute = 60 * 1000L; + nextCacheRefresh = now + minute; + } + try { if (pathInfo.startsWith(basePrefix)) { pathInfo = pathInfo.substring(basePrefix.length()); @@ -428,7 +426,7 @@ public class AlbumServlet fos.close(); res.setContentType("text/html"); res.getWriter().println(HtmlUtils.htmlEscape(value)); - clearDirCache(); + cachedRootNode = null; return; } res.setContentType("text/html"); @@ -545,11 +543,9 @@ public class AlbumServlet return ((DirectoryEntry)resolve(file.getParentFile())).get(file); } } - public synchronized void clearDirCache() { - cachedRootNode = null; - } - - + + + @Override public String getServletInfo() { return "Display of org.forkalsrud.album"; @@ -557,7 +553,7 @@ public class AlbumServlet /** - * maps files to uris relative to servlet, e.g. /home/joe/photos/holiday/arrival.jpg -> /photos/holiday/arrival.jpg + * maps files to URIs relative to servlet, e.g. /home/joe/photos/holiday/arrival.jpg -> /photos/holiday/arrival.jpg * assuming base is /home/joe/photos */ public class Mapper {