From 784935a975251d6c39622a2e38158d2c518d9a29 Mon Sep 17 00:00:00 2001 From: Jikoo Date: Mon, 3 Jul 2017 18:06:18 -0400 Subject: [PATCH] Synchronize cache (for real), updated dependencies Closes #69 --- .../java/com/lishid/openinv/util/Cache.java | 59 ++++++++++--------- pom.xml | 4 +- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/common/src/main/java/com/lishid/openinv/util/Cache.java b/common/src/main/java/com/lishid/openinv/util/Cache.java index 74986df..6d96f6d 100644 --- a/common/src/main/java/com/lishid/openinv/util/Cache.java +++ b/common/src/main/java/com/lishid/openinv/util/Cache.java @@ -72,10 +72,10 @@ public class Cache { * @return the value to which the specified key is mapped, or null if no value is mapped for the key */ public V get(K key) { - synchronized (internal) { - // Run lazy check to clean cache - lazyCheck(); + // Run lazy check to clean cache + lazyCheck(); + synchronized (internal) { return internal.get(key); } } @@ -87,10 +87,10 @@ public class Cache { * @return true if a mapping exists for the specified key */ public boolean containsKey(K key) { - synchronized (internal) { - // Run lazy check to clean cache - lazyCheck(); + // Run lazy check to clean cache + lazyCheck(); + synchronized (internal) { return internal.containsKey(key); } } @@ -101,10 +101,10 @@ public class Cache { * @param key key to invalidate */ public void invalidate(K key) { - synchronized (internal) { - // Run lazy check to clean cache - lazyCheck(); + // Run lazy check to clean cache + lazyCheck(); + synchronized (internal) { if (!internal.containsKey(key)) { // Value either not present or cleaned by lazy check. Either way, we're good return; @@ -143,27 +143,30 @@ public class Cache { private void lazyCheck() { long now = System.currentTimeMillis(); long nextExpiry = now + retention; - for (Iterator> iterator = expiry.entries().iterator(); iterator.hasNext();) { - Map.Entry entry = iterator.next(); + synchronized (internal) { + for (Iterator> iterator = expiry.entries().iterator(); iterator + .hasNext();) { + Map.Entry entry = iterator.next(); - if (entry.getKey() > now) { - break; + if (entry.getKey() > now) { + break; + } + + iterator.remove(); + + if (inUseCheck.run(internal.get(entry.getValue()))) { + expiry.put(nextExpiry, entry.getValue()); + continue; + } + + V value = internal.remove(entry.getValue()); + + if (value == null) { + continue; + } + + postRemoval.run(value); } - - iterator.remove(); - - if (inUseCheck.run(internal.get(entry.getValue()))) { - expiry.put(nextExpiry, entry.getValue()); - continue; - } - - V value = internal.remove(entry.getValue()); - - if (value == null) { - continue; - } - - postRemoval.run(value); } } diff --git a/pom.xml b/pom.xml index e7da34c..b63ebfe 100644 --- a/pom.xml +++ b/pom.xml @@ -74,7 +74,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.4.3 + 3.0.0 @@ -98,7 +98,7 @@ maven-compiler-plugin - 3.5.1 + 3.6.1 1.6 1.6