ALF-19779: Stack specific: Caches are no synchronized between the nodes

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@54391 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Matt Ward
2013-08-22 09:21:17 +00:00
parent eab5cddc79
commit 6ee97b2625
2 changed files with 52 additions and 2 deletions

View File

@@ -95,9 +95,20 @@ public final class DefaultSimpleCache<K extends Serializable, V extends Object>
@Override @Override
public void put(K key, V value) public void put(K key, V value)
{
putAndCheckUpdate(key, value);
}
/**
* <code>put</code> method that may be used to check for updates in a thread-safe manner.
*
* @return <code>true</code> if the put resulted in a change in value, <code>false</code> otherwise.
*/
public boolean putAndCheckUpdate(K key, V value)
{ {
AbstractMap.SimpleImmutableEntry<K, V> kvp = new AbstractMap.SimpleImmutableEntry<K, V>(key, value); AbstractMap.SimpleImmutableEntry<K, V> kvp = new AbstractMap.SimpleImmutableEntry<K, V>(key, value);
map.put(key, kvp); AbstractMap.SimpleImmutableEntry<K, V> priorKVP = map.put(key, kvp);
return priorKVP != null && (! priorKVP.equals(kvp));
} }
@Override @Override

View File

@@ -62,4 +62,43 @@ public class DefaultSimpleCacheTest extends SimpleCacheTestBase<DefaultSimpleCac
assertEquals("4", cache.get(4)); assertEquals("4", cache.get(4));
assertEquals("5", cache.get(5)); assertEquals("5", cache.get(5));
} }
@Test
public void putAndCheckUpdate()
{
// Put an initial value
cache.put(101, "101");
// Update it
assertEquals(true, cache.putAndCheckUpdate(101, "99101"));
// Check the value really was updated
assertEquals("99101", cache.get(101));
// Precondition: no value for key 102
assertFalse(cache.contains(102));
// Put a value - and test the return
assertEquals(false, cache.putAndCheckUpdate(102, "102"));
assertEquals("102", cache.get(102));
cache.put(103, null);
assertEquals(true, cache.putAndCheckUpdate(103, "103"));
// Repeat the put, this should not be an update
assertEquals(false, cache.putAndCheckUpdate(103, "103"));
assertFalse(cache.contains(104));
assertEquals(false, cache.putAndCheckUpdate(104, null));
// Repeat putting null - still not an update, as we had that value a moment ago.
assertEquals(false, cache.putAndCheckUpdate(104, null));
// Now an update
assertEquals(true, cache.putAndCheckUpdate(104, "104"));
// Another update
assertEquals(true, cache.putAndCheckUpdate(104, "99104"));
// Another update, back to null
assertEquals(true, cache.putAndCheckUpdate(104, null));
// Not an update - still null
assertEquals(false, cache.putAndCheckUpdate(104, null));
cache.remove(104);
assertEquals(false, cache.putAndCheckUpdate(104, "104"));
assertEquals(true, cache.putAndCheckUpdate(104, null));
}
} }