From a102f53365680379f4bdff8b3bfe7480881611e9 Mon Sep 17 00:00:00 2001 From: Harrison Date: Wed, 29 Apr 2020 13:05:04 -0500 Subject: [PATCH] Changed locking positions. --- .../recrown/islandsurvivalcraft/caching/Cache.java | 14 +++++++------- 1 file changed, 7 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 ce859da..cb48cf6 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/caching/Cache.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/caching/Cache.java @@ -30,11 +30,11 @@ public class Cache { * @param value The value to store. */ public void set(K key, V value) { - writeLock.lock(); - try { - if (data.containsKey(key)) { - data.get(key).value = value; - } else { + if (data.containsKey(key)) { + data.get(key).value = value; + } else { + writeLock.lock(); + try { CacheValue val = new CacheValue<>(); val.key = key; val.value = value; @@ -43,9 +43,9 @@ public class Cache { if (data.size() > maxSize) { data.remove(usage.pop().key); } + } finally { + writeLock.unlock(); } - } finally { - writeLock.unlock(); } }