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 2017/12/09 13:44:15 UTC

[2/2] groovy git commit: Refine SimpleCache

Refine SimpleCache


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

Branch: refs/heads/master
Commit: 1202cd513783ae12898622ac82ac2129c4ec5604
Parents: ba570df
Author: sunlan <su...@apache.org>
Authored: Sat Dec 9 21:44:04 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 9 21:44:04 2017 +0800

----------------------------------------------------------------------
 .../groovy/runtime/memoize/SimpleCache.java     | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/1202cd51/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
index 1aec2b5..1ee86d0 100644
--- a/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
+++ b/src/main/org/codehaus/groovy/runtime/memoize/SimpleCache.java
@@ -71,12 +71,18 @@ public class SimpleCache<K, V> implements EvictableCache<K, V> {
     }
 
     public V getAndPut(K key, ValueProvider<K, V> valueProvider, boolean shouldCache) {
+        if (null == key) {
+            return null;
+        }
+
         V value;
 
         readLock.lock();
         try {
             value = map.get(key);
-            if (null != value) return value;
+            if (null != value) {
+                return value;
+            }
         } finally {
             readLock.unlock();
         }
@@ -85,10 +91,18 @@ public class SimpleCache<K, V> implements EvictableCache<K, V> {
         try {
             // try to find the cached value again
             value = map.get(key);
-            if (null != value) return value;
+            if (null != value) {
+                return value;
+            }
+
+            if (null == valueProvider) {
+                return null;
+            }
 
             value = valueProvider.provide(key);
-            if (shouldCache) map.put(key, value);
+            if (shouldCache) {
+                map.put(key, value);
+            }
         } finally {
             writeLock.unlock();
         }