Author: doogie
Date: Wed May 26 06:04:59 2010 New Revision: 948333 URL: http://svn.apache.org/viewvc?rev=948333&view=rev Log: Add putIfAbsent method; disk-based variant of this method is not yet tested. 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=948333&r1=948332&r2=948333&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 Wed May 26 06:04:59 2010 @@ -291,6 +291,10 @@ public class UtilCache<K, V> implements return putInternal(key, value, expireTimeNanos); } + public V putIfAbsent(K key, V value) { + return putIfAbsentInternal(key, value, expireTimeNanos); + } + CacheLine<V> createSoftRefCacheLine(final Object key, V value, long loadTimeNanos, long expireTimeNanos) { return tryRegister(loadTimeNanos, new SoftRefCacheLine<V>(value, loadTimeNanos, expireTimeNanos) { @Override @@ -368,6 +372,10 @@ public class UtilCache<K, V> implements return putInternal(key, value, TimeUnit.NANOSECONDS.convert(expireTimeMillis, TimeUnit.MILLISECONDS)); } + public V putIfAbsent(K key, V value, long expireTimeMillis) { + return putIfAbsentInternal(key, value, TimeUnit.NANOSECONDS.convert(expireTimeMillis, TimeUnit.MILLISECONDS)); + } + V putInternal(K key, V value, long expireTimeNanos) { Object nulledKey = fromKey(key); CacheLine<V> oldCacheLine = memoryTable.put(nulledKey, createCacheLine(key, value, expireTimeNanos)); @@ -392,6 +400,41 @@ public class UtilCache<K, V> implements } } + V putIfAbsentInternal(K key, V value, long expireTimeNanos) { + Object nulledKey = fromKey(key); + V oldValue; + if (fileTable != null) { + try { + synchronized (this) { + oldValue = fileTable.get(nulledKey); + if (oldValue == null) { + memoryTable.put(nulledKey, createCacheLine(key, value, expireTimeNanos)); + fileTable.put(nulledKey, value); + jdbmMgr.commit(); + } + } + } catch (IOException e) { + Debug.logError(e, module); + oldValue = null; + } + } else { + CacheLine<V> newCacheLine = createCacheLine(key, value, expireTimeNanos); + CacheLine<V> oldCacheLine = memoryTable.putIfAbsent(nulledKey, newCacheLine); + if (oldCacheLine == null) { + oldValue = null; + } else { + oldValue = oldCacheLine.getValue(); + cancel(newCacheLine); + } + } + if (oldValue == null) { + noteAddition(key, value); + return null; + } else { + return oldValue; + } + } + /** Gets an element from the cache according to the specified key. * @param key The key for the element, used to reference it in the hastables and LRU linked list * @return The value of the element specified by the key 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=948333&r1=948332&r2=948333&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 Wed May 26 06:04:59 2010 @@ -323,6 +323,19 @@ public class UtilCacheTests extends Gene basicTest(cache); } + public void testPutIfAbsent() 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, "two", "dos"); + assertNull("putIfAbsent", cache.putIfAbsent("two", "dos")); + assertHasSingleKey(cache, "two", "dos"); + assertEquals("putIfAbsent", "dos", cache.putIfAbsent("two", "double")); + assertHasSingleKey(cache, "two", "dos"); + cache.removeListener(gotListener); + assertEquals("listener", wantedListener, gotListener); + } + public void testChangeMemSize() throws Exception { int size = 5; long ttl = 2000; |
Free forum by Nabble | Edit this page |