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:
@@ -29,6 +29,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Juergen Hoeller
|
||||
* @author Stephane Nicoll
|
||||
* @since 3.1
|
||||
*/
|
||||
public class EhCacheCache implements Cache {
|
||||
@@ -62,7 +63,7 @@ public class EhCacheCache implements Cache {
|
||||
@Override
|
||||
public ValueWrapper get(Object key) {
|
||||
Element element = this.cache.get(key);
|
||||
return (element != null ? new SimpleValueWrapper(element.getObjectValue()) : null);
|
||||
return toWrapper(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -81,6 +82,12 @@ public class EhCacheCache implements Cache {
|
||||
this.cache.put(new Element(key, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueWrapper putIfAbsent(Object key, Object value) {
|
||||
Element existingElement = this.cache.putIfAbsent(new Element(key, value));
|
||||
return toWrapper(existingElement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evict(Object key) {
|
||||
this.cache.remove(key);
|
||||
@@ -91,4 +98,8 @@ public class EhCacheCache implements Cache {
|
||||
this.cache.removeAll();
|
||||
}
|
||||
|
||||
private ValueWrapper toWrapper(Element element) {
|
||||
return (element != null ? new SimpleValueWrapper(element.getObjectValue()) : null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.springframework.cache.guava;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.support.SimpleValueWrapper;
|
||||
@@ -29,6 +31,7 @@ import org.springframework.util.Assert;
|
||||
* <p>Requires Google Guava 12.0 or higher.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.0
|
||||
*/
|
||||
public class GuavaCache implements Cache {
|
||||
@@ -83,7 +86,7 @@ public class GuavaCache implements Cache {
|
||||
@Override
|
||||
public ValueWrapper get(Object key) {
|
||||
Object value = this.cache.getIfPresent(key);
|
||||
return (value != null ? new SimpleValueWrapper(fromStoreValue(value)) : null);
|
||||
return toWrapper(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -101,6 +104,17 @@ public class GuavaCache implements Cache {
|
||||
this.cache.put(key, toStoreValue(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueWrapper putIfAbsent(Object key, final Object value) {
|
||||
try {
|
||||
PutIfAbsentCallable callable = new PutIfAbsentCallable(value);
|
||||
Object result = this.cache.get(key, callable);
|
||||
return (callable.called ? null : toWrapper(result));
|
||||
} catch (ExecutionException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evict(Object key) {
|
||||
this.cache.invalidate(key);
|
||||
@@ -138,9 +152,29 @@ public class GuavaCache implements Cache {
|
||||
return userValue;
|
||||
}
|
||||
|
||||
private ValueWrapper toWrapper(Object value) {
|
||||
return (value != null ? new SimpleValueWrapper(fromStoreValue(value)) : null);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class NullHolder implements Serializable {
|
||||
}
|
||||
|
||||
private class PutIfAbsentCallable implements Callable<Object> {
|
||||
private boolean called;
|
||||
|
||||
private final Object value;
|
||||
|
||||
private PutIfAbsentCallable(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
called = true;
|
||||
return toStoreValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.util.Assert;
|
||||
* <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Stephane Nicoll
|
||||
* @since 3.2
|
||||
*/
|
||||
public class JCacheCache implements Cache {
|
||||
@@ -95,6 +96,12 @@ public class JCacheCache implements Cache {
|
||||
this.cache.put(key, toStoreValue(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueWrapper putIfAbsent(Object key, Object value) {
|
||||
boolean set = this.cache.putIfAbsent(key, toStoreValue(value));
|
||||
return (set ? null : get(key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evict(Object key) {
|
||||
this.cache.remove(key);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -28,7 +28,11 @@ import org.springframework.util.Assert;
|
||||
* successful transaction. If no transaction is active, {@link #put} and {@link #evict}
|
||||
* operations will be performed immediately, as usual.
|
||||
*
|
||||
* <p>Use of more aggressive operations such as {@link #putIfAbsent} cannot be deferred
|
||||
* to the after-commit phase of a running transaction. Use these with care.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Stephane Nicoll
|
||||
* @since 3.2
|
||||
* @see TransactionAwareCacheManagerProxy
|
||||
*/
|
||||
@@ -82,6 +86,11 @@ public class TransactionAwareCacheDecorator implements Cache {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValueWrapper putIfAbsent(final Object key, final Object value) {
|
||||
return this.targetCache.putIfAbsent(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evict(final Object key) {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
|
||||
Reference in New Issue
Block a user