Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X 15c7c3f4b -> 5f2424e73
Refine LRUCache Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/5f2424e7 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/5f2424e7 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/5f2424e7 Branch: refs/heads/GROOVY_2_5_X Commit: 5f2424e7357ea22dd20b4b0dc676a60f12f2f0a4 Parents: 15c7c3f Author: sunlan <[email protected]> Authored: Sat Dec 2 01:37:59 2017 +0800 Committer: sunlan <[email protected]> Committed: Sat Dec 2 01:37:59 2017 +0800 ---------------------------------------------------------------------- .../groovy/runtime/memoize/LRUCache.java | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/5f2424e7/src/main/org/codehaus/groovy/runtime/memoize/LRUCache.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/runtime/memoize/LRUCache.java b/src/main/org/codehaus/groovy/runtime/memoize/LRUCache.java index 9f587ee..bf041ae 100644 --- a/src/main/org/codehaus/groovy/runtime/memoize/LRUCache.java +++ b/src/main/org/codehaus/groovy/runtime/memoize/LRUCache.java @@ -18,22 +18,26 @@ */ package org.codehaus.groovy.runtime.memoize; +import org.apache.groovy.util.concurrentlinkedhashmap.ConcurrentLinkedHashMap; + import java.lang.ref.SoftReference; -import java.util.Collections; import java.util.Iterator; import java.util.Map; /** - * A cache backed by a Collections.SynchronizedMap + * A cache backed by a ConcurrentLinkedHashMap * * @author Vaclav Pech + * @author <a href="mailto:[email protected]">Daniel.Sun</a> */ public final class LRUCache implements MemoizeCache<Object, Object> { - private final Map<Object, Object> cache; public LRUCache(final int maxCacheSize) { - cache = Collections.synchronizedMap(new LRUProtectionStorage(maxCacheSize)); +// cache = Collections.synchronizedMap(new LRUProtectionStorage(maxCacheSize)); + cache = new ConcurrentLinkedHashMap.Builder<>() + .maximumWeightedCapacity(maxCacheSize) + .build(); } public Object put(final Object key, final Object value) { @@ -45,15 +49,17 @@ public final class LRUCache implements MemoizeCache<Object, Object> { } /** - * Replying on the Collections.SynchronizedMap thread-safe iteration implementation the method will remove all entries holding - * SoftReferences to gc-evicted objects. + * Remove all entries holding SoftReferences to gc-evicted objects. */ public void cleanUpNullReferences() { synchronized (cache) { final Iterator<Map.Entry<Object, Object>> iterator = cache.entrySet().iterator(); while (iterator.hasNext()) { final Map.Entry<Object, Object> entry = iterator.next(); - if (((SoftReference) entry.getValue()).get() == null) iterator.remove(); + final Object value = entry.getValue(); + + if (!(value instanceof SoftReference)) continue; + if (((SoftReference) value).get() == null) iterator.remove(); } } }
