diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java
index c730c637e8..31bfd2f530 100644
--- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java
+++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheCache.java
@@ -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);
+ }
+
}
diff --git a/spring-context-support/src/main/java/org/springframework/cache/guava/GuavaCache.java b/spring-context-support/src/main/java/org/springframework/cache/guava/GuavaCache.java
index 6a9cf4cbe7..6acc2aad0b 100644
--- a/spring-context-support/src/main/java/org/springframework/cache/guava/GuavaCache.java
+++ b/spring-context-support/src/main/java/org/springframework/cache/guava/GuavaCache.java
@@ -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;
*
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