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

@@ -0,0 +1,105 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* @author Stephane Nicoll
*/
public abstract class AbstractCacheTests<T extends Cache> {
protected final static String CACHE_NAME = "testCache";
protected abstract T getCache();
protected abstract Object getNativeCache();
@Test
public void testCacheName() throws Exception {
assertEquals(CACHE_NAME, getCache().getName());
}
@Test
public void testNativeCache() throws Exception {
assertSame(getNativeCache(), getCache().getNativeCache());
}
@Test
public void testCachePut() throws Exception {
T cache = getCache();
Object key = "enescu";
Object value = "george";
assertNull(cache.get(key));
assertNull(cache.get(key, String.class));
assertNull(cache.get(key, Object.class));
cache.put(key, value);
assertEquals(value, cache.get(key).get());
assertEquals(value, cache.get(key, String.class));
assertEquals(value, cache.get(key, Object.class));
assertEquals(value, cache.get(key, null));
cache.put(key, null);
assertNotNull(cache.get(key));
assertNull(cache.get(key).get());
assertNull(cache.get(key, String.class));
assertNull(cache.get(key, Object.class));
}
@Test
public void testCachePutIfAbsent() throws Exception {
T cache = getCache();
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 {
T cache = getCache();
Object key = "enescu";
Object value = "george";
assertNull(cache.get(key));
cache.put(key, value);
}
@Test
public void testCacheClear() throws Exception {
T cache = getCache();
assertNull(cache.get("enescu"));
cache.put("enescu", "george");
assertNull(cache.get("vlaicu"));
cache.put("vlaicu", "aurel");
cache.clear();
assertNull(cache.get("vlaicu"));
assertNull(cache.get("enescu"));
}
}

View File

@@ -16,56 +16,55 @@
package org.springframework.cache.ehcache;
import static org.junit.Assert.*;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.AbstractCacheTests;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import static org.junit.Assert.*;
/**
* @author Costin Leau
* @author Stephane Nicoll
* @author Juergen Hoeller
*/
public class EhCacheCacheTests {
protected final static String CACHE_NAME = "testCache";
protected Ehcache nativeCache;
protected Cache cache;
public class EhCacheCacheTests extends AbstractCacheTests<EhCacheCache> {
private CacheManager cacheManager;
private Ehcache nativeCache;
private EhCacheCache cache;
@Before
public void setUp() throws Exception {
if (CacheManager.getInstance().cacheExists(CACHE_NAME)) {
nativeCache = CacheManager.getInstance().getEhcache(CACHE_NAME);
}
else {
nativeCache = new net.sf.ehcache.Cache(new CacheConfiguration(CACHE_NAME, 100));
CacheManager.getInstance().addCache(nativeCache);
}
public void setUp() {
cacheManager = new CacheManager(new Configuration().name("EhCacheCacheTests")
.defaultCache(new CacheConfiguration("default", 100)));
nativeCache = new net.sf.ehcache.Cache(new CacheConfiguration(CACHE_NAME, 100));
cacheManager.addCache(nativeCache);
cache = new EhCacheCache(nativeCache);
cache.clear();
}
@Test
public void testCacheName() throws Exception {
assertEquals(CACHE_NAME, cache.getName());
@After
public void tearDown() {
cacheManager.shutdown();
}
@Test
public void testNativeCache() throws Exception {
assertSame(nativeCache, cache.getNativeCache());
@Override
protected EhCacheCache getCache() {
return cache;
}
@Override
protected Ehcache getNativeCache() {
return nativeCache;
}
@Test
public void testCachePut() throws Exception {
Object key = "enescu";
@@ -88,26 +87,6 @@ public class EhCacheCacheTests {
assertNull(cache.get(key, Object.class));
}
@Test
public void testCacheRemove() throws Exception {
Object key = "enescu";
Object value = "george";
assertNull(cache.get(key));
cache.put(key, value);
}
@Test
public void testCacheClear() throws Exception {
assertNull(cache.get("enescu"));
cache.put("enescu", "george");
assertNull(cache.get("vlaicu"));
cache.put("vlaicu", "aurel");
cache.clear();
assertNull(cache.get("vlaicu"));
assertNull(cache.get("enescu"));
}
@Test
public void testExpiredElements() throws Exception {
Assume.group(TestGroup.LONG_RUNNING);
@@ -123,5 +102,4 @@ public class EhCacheCacheTests {
Thread.sleep(5 * 1000);
assertNull(cache.get(key));
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cache.guava;
import static org.junit.Assert.*;
import com.google.common.cache.CacheBuilder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cache.AbstractCacheTests;
import org.springframework.cache.Cache;
/**
* @author Stephane Nicoll
*/
public class GuavaCacheTests extends AbstractCacheTests<GuavaCache> {
private com.google.common.cache.Cache<Object, Object> nativeCache;
private GuavaCache cache;
@Before
public void setUp() {
nativeCache = CacheBuilder.newBuilder().build();
cache = new GuavaCache(CACHE_NAME, nativeCache);
}
@Override
protected GuavaCache getCache() {
return cache;
}
@Override
protected Object getNativeCache() {
return nativeCache;
}
@Test
public void putIfAbsentNullValue() throws Exception {
GuavaCache cache = getCache();
Object key = new Object();
Object value = null;
assertNull(cache.get(key));
assertNull(cache.putIfAbsent(key, value));
assertEquals(value, cache.get(key).get());
Cache.ValueWrapper wrapper = cache.putIfAbsent(key, "anotherValue");
assertNotNull(wrapper); // A value is set but is 'null'
assertEquals(null, wrapper.get());
assertEquals(value, cache.get(key).get()); // not changed
}
}

View File

@@ -90,6 +90,18 @@ public class TransactionAwareCacheDecoratorTests {
assertEquals("123", target.get(key, String.class));
}
@Test
public void putIfAbsent() { // no transactional support for putIfAbsent
Cache target = new ConcurrentMapCache("testCache");
Cache cache = new TransactionAwareCacheDecorator(target);
Object key = new Object();
assertNull(cache.putIfAbsent(key, "123"));
assertEquals("123", target.get(key, String.class));
assertEquals("123", cache.putIfAbsent(key, "456").get());
assertEquals("123", target.get(key, String.class)); // unchanged
}
@Test
public void evictNonTransactional() {
Cache target = new ConcurrentMapCache("testCache");