Repository: groovy Updated Branches: refs/heads/parrot 3495bc2f6 -> d30f059bf
http://git-wip-us.apache.org/repos/asf/groovy/blob/d30f059b/src/main/org/apache/groovy/util/concurrentlinkedhashmap/package-info.java ---------------------------------------------------------------------- diff --git a/src/main/org/apache/groovy/util/concurrentlinkedhashmap/package-info.java b/src/main/org/apache/groovy/util/concurrentlinkedhashmap/package-info.java new file mode 100644 index 0000000..665de2e --- /dev/null +++ b/src/main/org/apache/groovy/util/concurrentlinkedhashmap/package-info.java @@ -0,0 +1,41 @@ +/* + * Copyright 2011 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This package contains an implementation of a bounded + * {@link java.util.concurrent.ConcurrentMap} data structure. + * <p> + * {@link org.apache.groovy.util.concurrentlinkedhashmap.Weigher} is a simple interface + * for determining how many units of capacity an entry consumes. Depending on + * which concrete Weigher class is used, an entry may consume a different amount + * of space within the cache. The + * {@link org.apache.groovy.util.concurrentlinkedhashmap.Weighers} class provides + * utility methods for obtaining the most common kinds of implementations. + * <p> + * {@link org.apache.groovy.util.concurrentlinkedhashmap.EvictionListener} provides the + * ability to be notified when an entry is evicted from the map. An eviction + * occurs when the entry was automatically removed due to the map exceeding a + * capacity threshold. It is not called when an entry was explicitly removed. + * <p> + * The {@link org.apache.groovy.util.concurrentlinkedhashmap.ConcurrentLinkedHashMap} + * class supplies an efficient, scalable, thread-safe, bounded map. As with the + * <tt>Java Collections Framework</tt> the "Concurrent" prefix is used to + * indicate that the map is not governed by a single exclusion lock. + * + * @see <a href="http://code.google.com/p/concurrentlinkedhashmap/"> + * http://code.google.com/p/concurrentlinkedhashmap/</a> + */ +package org.apache.groovy.util.concurrentlinkedhashmap; http://git-wip-us.apache.org/repos/asf/groovy/blob/d30f059b/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:realblue...@hotmail.com">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(); } } }