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