From dac66d0b5aac974af565b1924c472866b4956bf4 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Fri, 20 Sep 2013 13:38:27 +0200 Subject: Synchronize on the hardCache, as it occasionally causes exceptions. --- src/main/java/com/c2kernel/utils/SoftCache.java | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/c2kernel/utils/SoftCache.java b/src/main/java/com/c2kernel/utils/SoftCache.java index 2fd79f1..efc1614 100644 --- a/src/main/java/com/c2kernel/utils/SoftCache.java +++ b/src/main/java/com/c2kernel/utils/SoftCache.java @@ -39,9 +39,11 @@ public class SoftCache extends AbstractMap { hash.remove(key); else if (minSize > 0) { // add to hard cache so it's not reaped for a while - hardCache.addFirst(result); - if (hardCache.size() > minSize) // trim last one off - hardCache.removeLast(); + synchronized(hardCache) { + hardCache.addFirst(result); + if (hardCache.size() > minSize) // trim last one off + hardCache.removeLast(); + } } } return result; @@ -51,9 +53,11 @@ public class SoftCache extends AbstractMap { public V put(K key, V value) { processQueue(); if (minSize > 0) { - hardCache.addFirst(value); - if (hardCache.size() > minSize) - hardCache.removeLast(); + synchronized(hardCache) { + hardCache.addFirst(value); + if (hardCache.size() > minSize) + hardCache.removeLast(); + } } hash.put(key, new SoftValue(key, value, queue)); return value; @@ -68,7 +72,9 @@ public class SoftCache extends AbstractMap { @Override public void clear() { - hardCache.clear(); + synchronized(hardCache) { + hardCache.clear(); + } while(queue.poll()!=null); hash.clear(); } -- cgit v1.2.3