Flush cache contents to disk occationally, so that it will survive a restart
This commit is contained in:
parent
648d38df1a
commit
0f7263d9d0
1 changed files with 7 additions and 0 deletions
|
|
@ -29,6 +29,7 @@ public class AlbumServlet
|
|||
Cache imageCache;
|
||||
CacheManager cacheManager;
|
||||
PictureScaler pictureScaler;
|
||||
long lastCacheFlushTime;
|
||||
|
||||
@Override
|
||||
public void init()
|
||||
|
|
@ -43,6 +44,7 @@ public class AlbumServlet
|
|||
cacheManager = CacheManager.create();
|
||||
imageCache = cacheManager.getCache("imageCache");
|
||||
pictureScaler = new PictureScaler();
|
||||
lastCacheFlushTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -177,6 +179,11 @@ public class AlbumServlet
|
|||
try {
|
||||
cimg = pictureScaler.scalePicture(file, thumbnail, size);
|
||||
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");
|
||||
} catch (TimeoutException e) {
|
||||
res.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue