Add putIfAbsent on Cache abstraction

This commit adds a putIfAbsent method to the Cache interface. This
method offers an atomic put if the key is not already associated in
the cache.

Issue: SPR-11400
This commit is contained in:
Stephane Nicoll
2014-02-12 14:41:34 +01:00
parent 8ed490c4d7
commit 3e74d3b2fb
12 changed files with 332 additions and 54 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010-2014 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@ import static org.junit.Assert.*;
/**
* @author Costin Leau
* @author Juergen Hoeller
* @author Stephane Nicoll
*/
public class ConcurrentCacheTests {
@@ -79,6 +80,18 @@ public class ConcurrentCacheTests {
assertNull(cache.get(key, Object.class));
}
@Test
public void testCachePutIfAbsent() throws Exception {
Object key = new Object();
Object value = "initialValue";
assertNull(cache.get(key));
assertNull(cache.putIfAbsent(key, value));
assertEquals(value, cache.get(key).get());
assertEquals("initialValue", cache.putIfAbsent(key, "anotherValue").get());
assertEquals(value, cache.get(key).get()); // not changed
}
@Test
public void testCacheRemove() throws Exception {
Object key = "enescu";