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 14:07:33 UTC
[5/6] groovy git commit: Refine SimpleCache
Refine SimpleCache
(cherry picked from commit 1202cd5)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/30fdba0f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/30fdba0f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/30fdba0f
Branch: refs/heads/GROOVY_2_6_X
Commit: 30fdba0f7b870b4aa266b047aa740f5a81ee83e4
Parents: 4de00ef
Author: sunlan <su...@apache.org>
Authored: Sat Dec 9 21:44:04 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Dec 9 22:07:02 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/30fdba0f/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();
}