Flush cache contents to disk occationally, so that it will survive a restart

This commit is contained in:
Erik Forkalsrud 2009-02-08 17:07:56 -08:00
parent 648d38df1a
commit 0f7263d9d0

View file

@ -29,6 +29,7 @@ public class AlbumServlet
Cache imageCache; Cache imageCache;
CacheManager cacheManager; CacheManager cacheManager;
PictureScaler pictureScaler; PictureScaler pictureScaler;
long lastCacheFlushTime;
@Override @Override
public void init() public void init()
@ -43,6 +44,7 @@ public class AlbumServlet
cacheManager = CacheManager.create(); cacheManager = CacheManager.create();
imageCache = cacheManager.getCache("imageCache"); imageCache = cacheManager.getCache("imageCache");
pictureScaler = new PictureScaler(); pictureScaler = new PictureScaler();
lastCacheFlushTime = System.currentTimeMillis();
} }
@Override @Override
@ -177,6 +179,11 @@ public class AlbumServlet
try { try {
cimg = pictureScaler.scalePicture(file, thumbnail, size); cimg = pictureScaler.scalePicture(file, thumbnail, size);
imageCache.put(new Element(key, cimg)); imageCache.put(new Element(key, cimg));
long millisSinceLastFlush = System.currentTimeMillis() - lastCacheFlushTime;
if (millisSinceLastFlush > 10 * 60 * 1000L) {
imageCache.flush();
lastCacheFlushTime = System.currentTimeMillis();
}
System.out.println(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + imageCache.getSize() + " entries"); System.out.println(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + imageCache.getSize() + " entries");
} catch (TimeoutException e) { } catch (TimeoutException e) {
res.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); res.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);