Added simple caching system.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package ca.recrown.islandsurvivalcraft.caching;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.PriorityQueue;
|
||||
|
||||
public class Cache <CacheValueType> {
|
||||
private final int maxCacheSize;
|
||||
private final HashMap<Identifier, CacheValue<CacheValueType>> dataCache;
|
||||
private final PriorityQueue<Identifier> ids;
|
||||
|
||||
public Cache(int maxCacheSize) {
|
||||
dataCache = new HashMap<>(maxCacheSize);
|
||||
ids = new PriorityQueue<>(maxCacheSize);
|
||||
this.maxCacheSize = maxCacheSize;
|
||||
}
|
||||
|
||||
public CacheValue<CacheValueType> retrieveCache(Identifier identifier) {
|
||||
if (dataCache.containsKey(identifier)) {
|
||||
ids.remove(identifier);
|
||||
dataCache.get(identifier);
|
||||
CacheValue<CacheValueType> val = dataCache.get(identifier);
|
||||
val.getValue();
|
||||
ids.add(val.identifier);
|
||||
return dataCache.get(identifier);
|
||||
}
|
||||
if (ids.size() >= maxCacheSize) {
|
||||
dataCache.remove(ids.poll());
|
||||
}
|
||||
CacheValue<CacheValueType> value = new CacheValue<>(identifier);
|
||||
ids.add(identifier);
|
||||
dataCache.put(identifier, value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public void clearCache() {
|
||||
ids.clear();
|
||||
dataCache.clear();
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package ca.recrown.islandsurvivalcraft.caching;
|
||||
|
||||
public class CacheValue <ValueType> {
|
||||
private ValueType value;
|
||||
protected final Identifier identifier;
|
||||
private boolean fresh = true;
|
||||
|
||||
public CacheValue(Identifier identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return fresh;
|
||||
}
|
||||
|
||||
public void setValue(ValueType value) {
|
||||
this.fresh = false;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public ValueType getValue() {
|
||||
identifier.usage++;
|
||||
return value;
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
package ca.recrown.islandsurvivalcraft.caching;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class CoordinateIdentifier extends Identifier {
|
||||
private final int x;
|
||||
private final int y;
|
||||
|
||||
public CoordinateIdentifier(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof CoordinateIdentifier) {
|
||||
CoordinateIdentifier other = (CoordinateIdentifier) obj;
|
||||
return x == other.x && y == other.y;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(x, y);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package ca.recrown.islandsurvivalcraft.caching;
|
||||
|
||||
public abstract class Identifier implements Comparable<Identifier> {
|
||||
protected int usage;
|
||||
|
||||
@Override
|
||||
public abstract boolean equals(Object obj);
|
||||
|
||||
@Override
|
||||
public abstract int hashCode();
|
||||
|
||||
@Override
|
||||
public int compareTo(Identifier o) {
|
||||
return usage - o.usage;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user