Caching now has a fresh importance factor.

This commit is contained in:
Harrison Deng 2020-04-25 01:32:01 -05:00
parent 28b9a8005c
commit a7db7d289a

View File

@ -7,11 +7,21 @@ public class Cache <CacheValueType> {
private final int maxCacheSize; private final int maxCacheSize;
private final HashMap<Identifier, CacheValue<CacheValueType>> dataCache; private final HashMap<Identifier, CacheValue<CacheValueType>> dataCache;
private final PriorityQueue<Identifier> ids; private final PriorityQueue<Identifier> ids;
private final float freshImportanceFactor;
public Cache(int maxCacheSize) { public Cache(int maxCacheSize, float freshImportanceFactor) {
dataCache = new HashMap<>(maxCacheSize); dataCache = new HashMap<>(maxCacheSize);
ids = new PriorityQueue<>(maxCacheSize); ids = new PriorityQueue<>(maxCacheSize);
this.maxCacheSize = maxCacheSize; this.maxCacheSize = maxCacheSize;
this.freshImportanceFactor = freshImportanceFactor;
}
public Cache() {
this(1024, 1f);
}
public Cache(int maxCacheSize) {
this(maxCacheSize, 1f);
} }
public synchronized CacheValue<CacheValueType> retrieveCache(Identifier identifier) { public synchronized CacheValue<CacheValueType> retrieveCache(Identifier identifier) {
@ -27,7 +37,7 @@ public class Cache <CacheValueType> {
} }
CacheValue<CacheValueType> value = new CacheValue<>(identifier); CacheValue<CacheValueType> value = new CacheValue<>(identifier);
if (!ids.isEmpty()) { if (!ids.isEmpty()) {
identifier.usage += ids.peek().usage; identifier.usage += ids.peek().usage * freshImportanceFactor;
} }
ids.add(identifier); ids.add(identifier);
dataCache.put(identifier, value); dataCache.put(identifier, value);