You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/02/05 02:35:39 UTC

groovy git commit: Refine `ConcurrentCommonCache`: delegate to `CommonCache` instance

Repository: groovy
Updated Branches:
  refs/heads/master c170300cd -> 2c4a9b694


Refine `ConcurrentCommonCache`: delegate to `CommonCache` instance


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/2c4a9b69
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/2c4a9b69
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/2c4a9b69

Branch: refs/heads/master
Commit: 2c4a9b694154d699492c2491df5e4d068d93aa26
Parents: c170300
Author: sunlan <su...@apache.org>
Authored: Mon Feb 5 10:35:22 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Feb 5 10:35:34 2018 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/CommonCache.java     | 17 +++---
 .../runtime/memoize/ConcurrentCommonCache.java  | 55 +++++++++++---------
 2 files changed, 42 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/2c4a9b69/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 6012181..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 <K> type of the keys
  * @param <V> type of the values
- *
  * @since 2.5.0
  */
-public class CommonCache<K, V> implements EvictableCache<K, V> {
+public class CommonCache<K, V> implements EvictableCache<K, V>, Serializable {
+    private static final long serialVersionUID = 934699400232698324L;
     /**
      * The default load factor
      */
@@ -46,6 +46,7 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
      * The default initial capacity
      */
     public static final int DEFAULT_INITIAL_CAPACITY = 16;
+
     private final Map<K, V> map;
 
     /**
@@ -57,8 +58,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
 
     /**
      * 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) {
@@ -75,8 +77,9 @@ public class CommonCache<K, V> implements EvictableCache<K, V> {
     /**
      * Constructs a LRU cache with the specified initial capacity and max size.
      * 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<K, V> implements EvictableCache<K, V> {
 
     /**
      * 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<K, V> implements EvictableCache<K, V> {
 
     /**
      * Constructs a cache backed by the specified {@link java.util.Map} instance
+     *
      * @param map the {@link java.util.Map} instance
      */
     public CommonCache(Map<K, V> map) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/2c4a9b69/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 24ae6c3..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 <K> type of the keys
  * @param <V> type of the values
- *
  * @since 2.5.0
  */
-public class ConcurrentCommonCache<K, V> extends CommonCache<K, V> {
+public class ConcurrentCommonCache<K, V> implements EvictableCache<K, V>, 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<K, V> commonCache;
 
     /**
      * Constructs a cache with unlimited size
      */
-    public ConcurrentCommonCache() {}
+    public ConcurrentCommonCache() {
+        commonCache = new CommonCache<K, V>();
+    }
 
     /**
      * 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<K, V>(initialCapacity, maxSize, evictionStrategy);
     }
 
     /**
      * Constructs a LRU cache with the specified initial capacity and max size.
      * 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<K, V>(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<K, V>(maxSize);
     }
 
     /**
      * Constructs a cache backed by the specified {@link java.util.Map} instance
+     *
      * @param map the {@link java.util.Map} instance
      */
     public ConcurrentCommonCache(Map<K, V> map) {
-        super(map);
+        commonCache = new CommonCache<K, V>(map);
     }
 
     /**
@@ -86,7 +94,7 @@ public class ConcurrentCommonCache<K, V> extends CommonCache<K, V> {
     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<K, V> extends CommonCache<K, V> {
     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<K, V> extends CommonCache<K, V> {
         return getAndPut(key, valueProvider, true);
     }
 
-    @Override
     public V getAndPut(K key, ValueProvider<? super K, ? extends V> 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<K, V> extends CommonCache<K, V> {
         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<K, V> extends CommonCache<K, V> {
     public Collection<V> values() {
         readLock.lock();
         try {
-            return super.values();
+            return commonCache.values();
         } finally {
             readLock.unlock();
         }
@@ -166,7 +173,7 @@ public class ConcurrentCommonCache<K, V> extends CommonCache<K, V> {
     public Set<K> keys() {
         readLock.lock();
         try {
-            return super.keys();
+            return commonCache.keys();
         } finally {
             readLock.unlock();
         }
@@ -179,7 +186,7 @@ public class ConcurrentCommonCache<K, V> extends CommonCache<K, V> {
     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<K, V> extends CommonCache<K, V> {
     public int size() {
         readLock.lock();
         try {
-            return super.size();
+            return commonCache.size();
         } finally {
             readLock.unlock();
         }
@@ -205,7 +212,7 @@ public class ConcurrentCommonCache<K, V> extends CommonCache<K, V> {
     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<K, V> extends CommonCache<K, V> {
     public Map<K, V> clear() {
         writeLock.lock();
         try {
-            return super.clear();
+            return commonCache.clear();
         } finally {
             writeLock.unlock();
         }
@@ -231,7 +238,7 @@ public class ConcurrentCommonCache<K, V> extends CommonCache<K, V> {
     public void cleanUpNullReferences() {
         writeLock.lock();
         try {
-            super.cleanUpNullReferences();
+            commonCache.cleanUpNullReferences();
         } finally {
             writeLock.unlock();
         }