Added simple caching system.

This commit is contained in:
Harrison Deng 2020-04-23 20:15:45 -05:00
parent 5ffea1aa08
commit e50a983b25
6 changed files with 204 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,63 @@
package ca.recrown.islandsurvivalcraft.caching;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
@TestInstance(Lifecycle.PER_CLASS)
public class CacheTest {
Cache<Integer> integerCache;
@BeforeAll
public void setUp() {
integerCache = new Cache<>(3);
}
@AfterEach
public void cleanUp() {
integerCache.clearCache();
}
@Test
public void testBasicCaching() {
CoordinateIdentifier coordsA = new CoordinateIdentifier(3, 0);
CoordinateIdentifier coordsB = new CoordinateIdentifier(4, 0);
CacheValue<Integer> val = integerCache.retrieveCache(coordsA);
assertTrue(val.isEmpty());
val.setValue(0);
assertFalse(val.isEmpty());
assertSame(val, integerCache.retrieveCache(coordsA));
val = integerCache.retrieveCache(coordsB);
assertTrue(val.isEmpty());
val.setValue(1);
assertFalse(val.isEmpty());
assertSame(val, integerCache.retrieveCache(coordsB));
}
@Test
public void testUsageBasedClean() {
integerCache.retrieveCache(new CoordinateIdentifier(1, 0)).setValue(1);
integerCache.retrieveCache(new CoordinateIdentifier(2, 0)).setValue(2);
integerCache.retrieveCache(new CoordinateIdentifier(3, 0)).setValue(3);
integerCache.retrieveCache(new CoordinateIdentifier(1, 0)).getValue();
integerCache.retrieveCache(new CoordinateIdentifier(4, 0)).setValue(4);
assertEquals(1, integerCache.retrieveCache(new CoordinateIdentifier(1, 0)).getValue());
assertFalse(integerCache.retrieveCache(new CoordinateIdentifier(1, 0)).isEmpty());
integerCache.retrieveCache(new CoordinateIdentifier(5, 0)).setValue(5);
assertFalse(integerCache.retrieveCache(new CoordinateIdentifier(1, 0)).isEmpty());
}
}

View File

@ -0,0 +1,32 @@
package ca.recrown.islandsurvivalcraft.caching;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
@TestInstance(Lifecycle.PER_CLASS)
public class CoordinateIdentifierTest {
Cache<Integer> integerCache;
@BeforeAll
public void setUp() {
integerCache = new Cache<>(4);
}
@AfterEach
public void cleanUp() {
integerCache.clearCache();
}
@Test
public void testIdentityChecking() {
integerCache.retrieveCache(new CoordinateIdentifier(1, 2)).setValue(8);
integerCache.retrieveCache(new CoordinateIdentifier(1, 2)).setValue(4);
assertEquals(4, integerCache.retrieveCache(new CoordinateIdentifier(1, 2)).getValue());
}
}