On May 11, 2012, at 9:51 PM, Adam Heath wrote:

> (I've wanted to add a UtilCache.getOrCreate(key, creator) method for a
> while to solve this problem, but have never gotten around to it).

Nice.
The following small utility method may also be useful:

Index: framework/base/src/org/ofbiz/base/util/cache/UtilCache.java
===================================================================
--- framework/base/src/org/ofbiz/base/util/cache/UtilCache.java (revision 
1337191)
+++ framework/base/src/org/ofbiz/base/util/cache/UtilCache.java (working copy)
@@ -289,6 +289,11 @@
         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

Jacopo

Reply via email to