Get rid of separate thread for caching

This commit is contained in:
Knut Forkalsrud 2013-11-07 21:10:21 -08:00
parent 269cc018fb
commit 99a01f1a6e

View file

@ -9,8 +9,6 @@ import java.io.PrintWriter;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Properties; import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
@ -102,10 +100,10 @@ public class AlbumServlet
ThumbnailDatabase thumbDb; ThumbnailDatabase thumbDb;
DirectoryDatabase dirDb; DirectoryDatabase dirDb;
MovieDatabase movieDb; MovieDatabase movieDb;
Timer timer;
Entry cachedRootNode = null; Entry cachedRootNode = null;
MovieCoder movieCoder; MovieCoder movieCoder;
DirectoryEntryFactory dirEntryFactory; DirectoryEntryFactory dirEntryFactory;
long nextCacheRefresh;
@Override @Override
public void init() public void init()
@ -129,14 +127,7 @@ public class AlbumServlet
log4jInit("/log4j.properties"); log4jInit("/log4j.properties");
log.info("in init of Album"); log.info("in init of Album");
long minute = 60 * 1000L; long minute = 60 * 1000L;
timer = new Timer("cache evictor"); nextCacheRefresh = System.currentTimeMillis() + minute;
timer.schedule(new TimerTask() {
@Override
public void run() {
clearDirCache();
}
}, minute, minute);
base = new File(props.getProperty("base", "photos")).getAbsoluteFile(); base = new File(props.getProperty("base", "photos")).getAbsoluteFile();
basePrefix = "/" + base.getName(); basePrefix = "/" + base.getName();
@ -223,6 +214,13 @@ public class AlbumServlet
return; return;
} }
long now = System.currentTimeMillis();
if (now > nextCacheRefresh) {
cachedRootNode = null;
long minute = 60 * 1000L;
nextCacheRefresh = now + minute;
}
try { try {
if (pathInfo.startsWith(basePrefix)) { if (pathInfo.startsWith(basePrefix)) {
pathInfo = pathInfo.substring(basePrefix.length()); pathInfo = pathInfo.substring(basePrefix.length());
@ -428,7 +426,7 @@ public class AlbumServlet
fos.close(); fos.close();
res.setContentType("text/html"); res.setContentType("text/html");
res.getWriter().println(HtmlUtils.htmlEscape(value)); res.getWriter().println(HtmlUtils.htmlEscape(value));
clearDirCache(); cachedRootNode = null;
return; return;
} }
res.setContentType("text/html"); res.setContentType("text/html");
@ -545,11 +543,9 @@ public class AlbumServlet
return ((DirectoryEntry)resolve(file.getParentFile())).get(file); return ((DirectoryEntry)resolve(file.getParentFile())).get(file);
} }
} }
public synchronized void clearDirCache() {
cachedRootNode = null;
}
@Override @Override
public String getServletInfo() { public String getServletInfo() {
return "Display of org.forkalsrud.album"; 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 * assuming base is /home/joe/photos
*/ */
public class Mapper { public class Mapper {