From commits-return-5379-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Mon Feb 5 04:30:27 2018 Return-Path: X-Original-To: archive-asf-public@eu.ponee.io Delivered-To: archive-asf-public@eu.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by mx-eu-01.ponee.io (Postfix) with ESMTP id 1CC7918064A for ; Mon, 5 Feb 2018 04:30:27 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 0C460160C59; Mon, 5 Feb 2018 03:30:27 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 07C36160C41 for ; Mon, 5 Feb 2018 04:30:25 +0100 (CET) Received: (qmail 82869 invoked by uid 500); 5 Feb 2018 03:30:24 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 82860 invoked by uid 99); 5 Feb 2018 03:30:24 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Feb 2018 03:30:24 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2B66DE9638; Mon, 5 Feb 2018 03:30:24 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sunlan@apache.org To: commits@groovy.apache.org Message-Id: <66d45ad4760747688229d47ed0703301@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: groovy git commit: Refine `ConcurrentCommonCache`: delegate to `CommonCache` instance Date: Mon, 5 Feb 2018 03:30:24 +0000 (UTC) Repository: groovy Updated Branches: refs/heads/GROOVY_2_5_X 2a567e18f -> e4c4d663f Refine `ConcurrentCommonCache`: delegate to `CommonCache` instance (cherry picked from commit f08bb6b) Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/e4c4d663 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e4c4d663 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e4c4d663 Branch: refs/heads/GROOVY_2_5_X Commit: e4c4d663fe1ad5bd78c524cf16bf569455d944fc Parents: 2a567e1 Author: sunlan Authored: Mon Feb 5 10:35:22 2018 +0800 Committer: sunlan Committed: Mon Feb 5 11:30:19 2018 +0800 ---------------------------------------------------------------------- .../groovy/runtime/memoize/CommonCache.java | 19 ++++--- .../runtime/memoize/ConcurrentCommonCache.java | 57 +++++++++++--------- 2 files changed, 44 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/e4c4d663/src/main/java/org/codehaus/groovy/runtime/memoize/CommonCache.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/runtime/memoize/CommonCache.java b/src/main/java/org/codehaus/groovy/runtime/memoize/CommonCache.java index 094a465..29f048f 100644 --- a/src/main/java/org/codehaus/groovy/runtime/memoize/CommonCache.java +++ b/src/main/java/org/codehaus/groovy/runtime/memoize/CommonCache.java @@ -19,6 +19,7 @@ package org.codehaus.groovy.runtime.memoize; +import java.io.Serializable; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; import java.util.Collection; @@ -29,15 +30,14 @@ import java.util.Map; import java.util.Set; /** - * * Represents a simple key-value cache, which is NOT thread safe and backed by a {@link java.util.Map} instance * * @param type of the keys * @param type of the values - * * @since 2.5.0 */ -public class CommonCache implements EvictableCache { +public class CommonCache implements EvictableCache, Serializable { + private static final long serialVersionUID = 934699400232698324L; /** * The default load factor */ @@ -46,6 +46,7 @@ public class CommonCache implements EvictableCache { * The default initial capacity */ public static final int DEFAULT_INITIAL_CAPACITY = 16; + private final Map map; /** @@ -57,8 +58,9 @@ public class CommonCache implements EvictableCache { /** * Constructs a cache with limited size - * @param initialCapacity initial capacity of the cache - * @param maxSize max size of the cache + * + * @param initialCapacity initial capacity of the cache + * @param maxSize max size of the cache * @param evictionStrategy LRU or FIFO, see {@link org.codehaus.groovy.runtime.memoize.EvictableCache.EvictionStrategy} */ public CommonCache(final int initialCapacity, final int maxSize, final EvictionStrategy evictionStrategy) { @@ -74,9 +76,10 @@ public class CommonCache implements EvictableCache { /** * Constructs a LRU cache with the specified initial capacity and max size. - * The LRU cache is slower than {@link LRUCache} but will not put same value multi-times concurrently + * The LRU cache is slower than {@link LRUCache} + * * @param initialCapacity initial capacity of the LRU cache - * @param maxSize max size of the LRU cache + * @param maxSize max size of the LRU cache */ public CommonCache(int initialCapacity, int maxSize) { this(initialCapacity, maxSize, EvictionStrategy.LRU); @@ -84,6 +87,7 @@ public class CommonCache implements EvictableCache { /** * Constructs a LRU cache with the default initial capacity + * * @param maxSize max size of the LRU cache * @see #CommonCache(int, int) */ @@ -93,6 +97,7 @@ public class CommonCache implements EvictableCache { /** * Constructs a cache backed by the specified {@link java.util.Map} instance + * * @param map the {@link java.util.Map} instance */ public CommonCache(Map map) { http://git-wip-us.apache.org/repos/asf/groovy/blob/e4c4d663/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java b/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java index ec93199..8e090a9 100644 --- a/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java +++ b/src/main/java/org/codehaus/groovy/runtime/memoize/ConcurrentCommonCache.java @@ -18,65 +18,73 @@ */ package org.codehaus.groovy.runtime.memoize; +import java.io.Serializable; import java.util.Collection; import java.util.Map; import java.util.Set; import java.util.concurrent.locks.ReentrantReadWriteLock; /** - * * Represents a simple key-value cache, which is thread safe and backed by a {@link java.util.Map} instance * * @param type of the keys * @param type of the values - * * @since 2.5.0 */ -public class ConcurrentCommonCache extends CommonCache { +public class ConcurrentCommonCache implements EvictableCache, Serializable { + private static final long serialVersionUID = -7352338549333024936L; + private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); private final ReentrantReadWriteLock.ReadLock readLock = rwl.readLock(); private final ReentrantReadWriteLock.WriteLock writeLock = rwl.writeLock(); + private final CommonCache commonCache; /** * Constructs a cache with unlimited size */ - public ConcurrentCommonCache() {} + public ConcurrentCommonCache() { + commonCache = new CommonCache(); + } /** * Constructs a cache with limited size - * @param initialCapacity initial capacity of the cache - * @param maxSize max size of the cache + * + * @param initialCapacity initial capacity of the cache + * @param maxSize max size of the cache * @param evictionStrategy LRU or FIFO, see {@link org.codehaus.groovy.runtime.memoize.EvictableCache.EvictionStrategy} */ public ConcurrentCommonCache(int initialCapacity, int maxSize, EvictionStrategy evictionStrategy) { - super(initialCapacity, maxSize, evictionStrategy); + commonCache = new CommonCache(initialCapacity, maxSize, evictionStrategy); } /** * Constructs a LRU cache with the specified initial capacity and max size. - * The LRU cache is slower than {@link LRUCache} but will not put same value multi-times concurrently + * The LRU cache is slower than {@link LRUCache} + * * @param initialCapacity initial capacity of the LRU cache - * @param maxSize max size of the LRU cache + * @param maxSize max size of the LRU cache */ public ConcurrentCommonCache(int initialCapacity, int maxSize) { - super(initialCapacity, maxSize); + commonCache = new CommonCache(initialCapacity, maxSize); } /** * Constructs a LRU cache with the default initial capacity(16) + * * @param maxSize max size of the LRU cache * @see #ConcurrentCommonCache(int, int) */ public ConcurrentCommonCache(int maxSize) { - super(maxSize); + commonCache = new CommonCache(maxSize); } /** * Constructs a cache backed by the specified {@link java.util.Map} instance + * * @param map the {@link java.util.Map} instance */ public ConcurrentCommonCache(Map map) { - super(map); + commonCache = new CommonCache(map); } /** @@ -86,7 +94,7 @@ public class ConcurrentCommonCache extends CommonCache { public V get(K key) { readLock.lock(); try { - return super.get(key); + return commonCache.get(key); } finally { readLock.unlock(); } @@ -99,7 +107,7 @@ public class ConcurrentCommonCache extends CommonCache { public V put(K key, V value) { writeLock.lock(); try { - return super.put(key, value); + return commonCache.put(key, value); } finally { writeLock.unlock(); } @@ -113,13 +121,12 @@ public class ConcurrentCommonCache extends CommonCache { return getAndPut(key, valueProvider, true); } - @Override public V getAndPut(K key, ValueProvider valueProvider, boolean shouldCache) { V value; readLock.lock(); try { - value = super.get(key); + value = commonCache.get(key); if (null != value) { return value; } @@ -130,14 +137,14 @@ public class ConcurrentCommonCache extends CommonCache { writeLock.lock(); try { // try to find the cached value again - value = super.get(key); + value = commonCache.get(key); if (null != value) { return value; } value = null == valueProvider ? null : valueProvider.provide(key); if (shouldCache && null != value) { - super.put(key, value); + commonCache.put(key, value); } } finally { writeLock.unlock(); @@ -153,7 +160,7 @@ public class ConcurrentCommonCache extends CommonCache { public Collection values() { readLock.lock(); try { - return super.values(); + return commonCache.values(); } finally { readLock.unlock(); } @@ -166,7 +173,7 @@ public class ConcurrentCommonCache extends CommonCache { public Set keys() { readLock.lock(); try { - return super.keys(); + return commonCache.keys(); } finally { readLock.unlock(); } @@ -179,7 +186,7 @@ public class ConcurrentCommonCache extends CommonCache { public boolean containsKey(K key) { readLock.lock(); try { - return super.containsKey(key); + return commonCache.containsKey(key); } finally { readLock.unlock(); } @@ -192,7 +199,7 @@ public class ConcurrentCommonCache extends CommonCache { public int size() { readLock.lock(); try { - return super.size(); + return commonCache.size(); } finally { readLock.unlock(); } @@ -205,7 +212,7 @@ public class ConcurrentCommonCache extends CommonCache { public V remove(K key) { writeLock.lock(); try { - return super.remove(key); + return commonCache.remove(key); } finally { writeLock.unlock(); } @@ -218,7 +225,7 @@ public class ConcurrentCommonCache extends CommonCache { public Map clear() { writeLock.lock(); try { - return super.clear(); + return commonCache.clear(); } finally { writeLock.unlock(); } @@ -231,7 +238,7 @@ public class ConcurrentCommonCache extends CommonCache { public void cleanUpNullReferences() { writeLock.lock(); try { - super.cleanUpNullReferences(); + commonCache.cleanUpNullReferences(); } finally { writeLock.unlock(); }