You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2012/05/19 08:36:42 UTC

svn commit: r1340351 - in /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache: UtilCache.java test/UtilCacheTests.java

Author: jacopoc
Date: Sat May 19 06:36:41 2012
New Revision: 1340351

URL: http://svn.apache.org/viewvc?rev=1340351&view=rev
Log:
Added new method "putIfAbsentAndGet" to UtilCache: it is similar to putIfAbsent but it always return the value even if the value was missing and added to the cache by method.
This can be useful to improve code like:

cache.putIfAbsent("key", "value");
value = cache.get("key"); // if a concurrent thread has cleared the cache the value could be null

into this:

value = cache.putIfAbsentAndGet("key", "value");


Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/test/UtilCacheTests.java

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java?rev=1340351&r1=1340350&r2=1340351&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/UtilCache.java Sat May 19 06:36:41 2012
@@ -289,6 +289,11 @@ public class UtilCache<K, V> implements 
         return putIfAbsentInternal(key, value, expireTimeNanos);
     }
 
+    public V putIfAbsentAndGet(K key, V value) {
+        V cachedValue = putIfAbsent(key, value);
+        return (cachedValue != null? cachedValue: value);
+    }
+
     CacheLine<V> createSoftRefCacheLine(final Object key, V value, long loadTimeNanos, long expireTimeNanos) {
         return tryRegister(loadTimeNanos, new SoftRefCacheLine<V>(value, loadTimeNanos, expireTimeNanos) {
             @Override

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/test/UtilCacheTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/test/UtilCacheTests.java?rev=1340351&r1=1340350&r2=1340351&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/test/UtilCacheTests.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/cache/test/UtilCacheTests.java Sat May 19 06:36:41 2012
@@ -339,6 +339,20 @@ public class UtilCacheTests extends Gene
         assertEquals("listener", wantedListener, gotListener);
     }
 
+    public void testPutIfAbsentAndGet() throws Exception {
+        UtilCache<String, String> cache = createUtilCache(5, 5, 2000, false, false);
+        Listener<String, String> gotListener = createListener(cache);
+        Listener<String, String> wantedListener = new Listener<String, String>();
+        wantedListener.noteKeyAddition(cache, "key", "value");
+        assertNull("no-get", cache.get("key"));
+        assertEquals("putIfAbsentAndGet", "value", cache.putIfAbsentAndGet("key", "value"));
+        assertHasSingleKey(cache, "key", "value");
+        assertEquals("putIfAbsentAndGet", "value", cache.putIfAbsentAndGet("key", "newValue"));
+        assertHasSingleKey(cache, "key", "value");
+        cache.removeListener(gotListener);
+        assertEquals("listener", wantedListener, gotListener);
+    }
+
     public void testChangeMemSize() throws Exception {
         int size = 5;
         long ttl = 2000;