improve cache file size check

This commit is contained in:
ayunami2000 2022-04-17 19:54:10 -04:00
parent c7658647fc
commit b980ea837b
3 changed files with 5 additions and 4 deletions

View file

@ -13,7 +13,7 @@ public class CacheItem
public CacheItem(File f) throws IOException public CacheItem(File f) throws IOException
{ {
this.path = f.getPath(); this.path = f.getPath();
this.file = Files.readAllBytes(f.toPath()); this.file = f.length() >= 1048576 ? null : Files.readAllBytes(f.toPath());
this.timestamp = System.currentTimeMillis(); this.timestamp = System.currentTimeMillis();
} }
} }

View file

@ -21,13 +21,13 @@ public class FileCache
if (theItem == null) if (theItem == null)
{ {
theItem = new CacheItem(new File(path)); theItem = new CacheItem(new File(path));
if (theItem.file.length < 1048576) cache.add(theItem); if (theItem.file != null) cache.add(theItem);
} }
if (System.currentTimeMillis() - theItem.timestamp > 3 * 60 * 1000) // 3 minutes if (System.currentTimeMillis() - theItem.timestamp > 3 * 60 * 1000) // 3 minutes
{ {
cache.remove(theItem); cache.remove(theItem);
theItem = new CacheItem(new File(path)); theItem = new CacheItem(new File(path));
if (theItem.file.length < 1048576) cache.add(theItem); if (theItem.file != null) cache.add(theItem);
} }
return theItem.file; return theItem.file;
} }

View file

@ -67,7 +67,8 @@ public class SchematicEndpoint extends AbstractServlet
{ {
try try
{ {
outputStream.write(HTTPDModule.fileCache.getFile(schemFile)); byte[] schemData = HTTPDModule.fileCache.getFile(schemFile);
if (schemData != null) outputStream.write(schemData);
} }
catch (IOException ignored) catch (IOException ignored)
{ {