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.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,9 +543,7 @@ public class AlbumServlet
return ((DirectoryEntry)resolve(file.getParentFile())).get(file);
}
}
public synchronized void clearDirCache() {
cachedRootNode = null;
}
@Override
@ -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 {