After profiling, add another cache where it matters -- direcoryentries

This commit is contained in:
Knut Forkalsrud 2010-05-22 13:19:07 -07:00
parent 4d068ed1bb
commit ca752abf64

View file

@ -3,10 +3,11 @@ package org.forkalsrud.album.web;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
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;
@ -91,6 +92,8 @@ public class AlbumServlet
private Environment environment;
ThumbnailDatabase thumbDb;
DirectoryDatabase dirDb;
Timer timer;
Entry cachedRootNode = null;
@Override
public void init()
@ -113,6 +116,16 @@ 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);
base = new File(props.getProperty("base", "photos")).getAbsoluteFile();
basePrefix = "/" + base.getName();
@ -296,11 +309,19 @@ public class AlbumServlet
Entry resolve(File file) {
if (base.equals(file.getAbsoluteFile())) {
return new DirectoryEntry(dirDb, null, file);
synchronized (this) {
if (cachedRootNode == null) {
cachedRootNode = new DirectoryEntry(dirDb, null, file);
}
}
return cachedRootNode;
} else {
return ((DirectoryEntry)resolve(file.getParentFile())).get(file);
}
}
public synchronized void clearDirCache() {
cachedRootNode = null;
}
@Override