mirror of
https://github.com/plexusorg/Module-HTTPD.git
synced 2025-06-16 23:28:50 +00:00
add caching (untested)
This commit is contained in:
parent
01d2cf44ea
commit
b6b5a7f227
11 changed files with 132 additions and 25 deletions
34
src/main/java/dev/plex/cache/FileCache.java
vendored
Normal file
34
src/main/java/dev/plex/cache/FileCache.java
vendored
Normal file
|
@ -0,0 +1,34 @@
|
|||
package dev.plex.cache;
|
||||
|
||||
import com.google.common.collect.EvictingQueue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Queue;
|
||||
|
||||
public class FileCache
|
||||
{
|
||||
private final Queue<CacheItem> cache = EvictingQueue.create(15);
|
||||
|
||||
public byte[] getFile(File file) throws IOException
|
||||
{
|
||||
return getFile(file.getPath());
|
||||
}
|
||||
|
||||
public byte[] getFile(String path) throws IOException
|
||||
{
|
||||
CacheItem theItem = cache.stream().filter(cacheItem -> cacheItem.path.equals(path)).findFirst().orElse(null);
|
||||
if (theItem == null)
|
||||
{
|
||||
theItem = new CacheItem(new File(path));
|
||||
cache.add(theItem);
|
||||
}
|
||||
if (System.currentTimeMillis() - theItem.timestamp > 3 * 60 * 1000) // 3 minutes
|
||||
{
|
||||
cache.remove(theItem);
|
||||
theItem = new CacheItem(new File(path));
|
||||
cache.add(theItem);
|
||||
}
|
||||
return theItem.file;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue