From 8acc653b5b08efe500f750121d8cc0a49f799045 Mon Sep 17 00:00:00 2001 From: Harrison Date: Fri, 24 Apr 2020 15:08:03 -0500 Subject: [PATCH] Attempted to make cache system thread compatible. --- .../islandsurvivalcraft/caching/Cache.java | 2 +- .../islandsurvivalcraft/caching/CacheValue.java | 7 ++++--- .../caching/CoordinateIdentifier.java | 17 ++++++++++++++++- .../islandsurvivalcraft/caching/Identifier.java | 4 ++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/caching/Cache.java b/src/main/java/ca/recrown/islandsurvivalcraft/caching/Cache.java index 983b5fc..67b6ee7 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/caching/Cache.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/caching/Cache.java @@ -14,7 +14,7 @@ public class Cache { this.maxCacheSize = maxCacheSize; } - public CacheValue retrieveCache(Identifier identifier) { + public synchronized CacheValue retrieveCache(Identifier identifier) { if (dataCache.containsKey(identifier)) { ids.remove(identifier); dataCache.get(identifier); diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/caching/CacheValue.java b/src/main/java/ca/recrown/islandsurvivalcraft/caching/CacheValue.java index 5b00335..b1e333c 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/caching/CacheValue.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/caching/CacheValue.java @@ -1,9 +1,9 @@ package ca.recrown.islandsurvivalcraft.caching; public class CacheValue { - private ValueType value; + private volatile ValueType value; protected final Identifier identifier; - private boolean fresh = true; + private volatile boolean fresh = true; public CacheValue(Identifier identifier) { this.identifier = identifier; @@ -13,9 +13,10 @@ public class CacheValue { return fresh; } - public void setValue(ValueType value) { + public synchronized void setValue(ValueType value) { this.fresh = false; this.value = value; + identifier.usage ++; } public ValueType getValue() { diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/caching/CoordinateIdentifier.java b/src/main/java/ca/recrown/islandsurvivalcraft/caching/CoordinateIdentifier.java index 1f5bafd..01d5022 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/caching/CoordinateIdentifier.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/caching/CoordinateIdentifier.java @@ -5,10 +5,12 @@ import java.util.Objects; public class CoordinateIdentifier extends Identifier { private final int x; private final int y; + private final int hash; public CoordinateIdentifier(int x, int y) { this.x = x; this.y = y; + hash = Objects.hash(x, y); } @Override @@ -22,7 +24,20 @@ public class CoordinateIdentifier extends Identifier { @Override public int hashCode() { - return Objects.hash(x, y); + return hash; } + /** + * @return the x + */ + public int getX() { + return x; + } + + /** + * @return the y + */ + public int getY() { + return y; + } } \ No newline at end of file diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/caching/Identifier.java b/src/main/java/ca/recrown/islandsurvivalcraft/caching/Identifier.java index 20df26b..f788034 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/caching/Identifier.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/caching/Identifier.java @@ -1,7 +1,7 @@ package ca.recrown.islandsurvivalcraft.caching; public abstract class Identifier implements Comparable { - protected int usage; + protected volatile int usage; @Override public abstract boolean equals(Object obj); @@ -10,7 +10,7 @@ public abstract class Identifier implements Comparable { public abstract int hashCode(); @Override - public int compareTo(Identifier o) { + public synchronized int compareTo(Identifier o) { return usage - o.usage; } } \ No newline at end of file