diff options
| -rw-r--r-- | src/main/java/com/c2kernel/utils/SoftCache.java | 20 |
1 files 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<K, V> extends AbstractMap<K, V> { 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<K, V> extends AbstractMap<K, V> { 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<V>(key, value, queue));
return value;
@@ -68,7 +72,9 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { @Override
public void clear() {
- hardCache.clear();
+ synchronized(hardCache) {
+ hardCache.clear();
+ }
while(queue.poll()!=null);
hash.clear();
}
|
