Skip to content

Commit 3096888

Browse files
committed
Better synchronization in ConcurrentMapCache
Issue: SPR-13810
1 parent 9b9f371 commit 3096888

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCache.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class ConcurrentMapCache extends AbstractValueAdaptingCache {
5959
* @param name the name of the cache
6060
*/
6161
public ConcurrentMapCache(String name) {
62-
this(name, new ConcurrentHashMap<Object, Object>(256), true);
62+
this(name, new ConcurrentHashMap<>(256), true);
6363
}
6464

6565
/**
@@ -69,7 +69,7 @@ public ConcurrentMapCache(String name) {
6969
* values for this cache
7070
*/
7171
public ConcurrentMapCache(String name, boolean allowNullValues) {
72-
this(name, new ConcurrentHashMap<Object, Object>(256), allowNullValues);
72+
this(name, new ConcurrentHashMap<>(256), allowNullValues);
7373
}
7474

7575
/**
@@ -141,20 +141,14 @@ public <T> T get(Object key, Callable<T> valueLoader) {
141141
return (T) get(key).get();
142142
}
143143
else {
144-
synchronized (this.store) {
145-
if (this.store.containsKey(key)) {
146-
return (T) get(key).get();
147-
}
148-
T value;
144+
return (T) fromStoreValue(this.store.computeIfAbsent(key, r -> {
149145
try {
150-
value = valueLoader.call();
146+
return toStoreValue(valueLoader.call());
151147
}
152148
catch (Exception ex) {
153149
throw new ValueRetrievalException(key, valueLoader, ex);
154150
}
155-
put(key, value);
156-
return value;
157-
}
151+
}));
158152
}
159153
}
160154

spring-context/src/test/java/org/springframework/cache/concurrent/ConcurrentMapCacheTests.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,9 +44,9 @@ public class ConcurrentMapCacheTests extends AbstractCacheTests<ConcurrentMapCac
4444

4545
@Before
4646
public void setUp() throws Exception {
47-
nativeCache = new ConcurrentHashMap<Object, Object>();
48-
cache = new ConcurrentMapCache(CACHE_NAME, nativeCache, true);
49-
cache.clear();
47+
this.nativeCache = new ConcurrentHashMap<>();
48+
this.cache = new ConcurrentMapCache(CACHE_NAME, this.nativeCache, true);
49+
this.cache.clear();
5050
}
5151

5252
@Override
@@ -84,9 +84,9 @@ public void testSerializer() {
8484
public void testNonSerializableContent() {
8585
ConcurrentMapCache serializeCache = createCacheWithStoreByValue();
8686

87-
thrown.expect(IllegalArgumentException.class);
88-
thrown.expectMessage("Failed to serialize");
89-
thrown.expectMessage(this.cache.getClass().getName());
87+
this.thrown.expect(IllegalArgumentException.class);
88+
this.thrown.expectMessage("Failed to serialize");
89+
this.thrown.expectMessage(this.cache.getClass().getName());
9090
serializeCache.put(createRandomKey(), this.cache);
9191
}
9292

@@ -96,15 +96,15 @@ public void testInvalidSerializedContent() {
9696

9797
String key = createRandomKey();
9898
this.nativeCache.put(key, "Some garbage");
99-
thrown.expect(IllegalArgumentException.class);
100-
thrown.expectMessage("Failed to deserialize");
101-
thrown.expectMessage("Some garbage");
99+
this.thrown.expect(IllegalArgumentException.class);
100+
this.thrown.expectMessage("Failed to deserialize");
101+
this.thrown.expectMessage("Some garbage");
102102
serializeCache.get(key);
103103
}
104104

105105

106106
private ConcurrentMapCache createCacheWithStoreByValue() {
107-
return new ConcurrentMapCache(CACHE_NAME, nativeCache, true,
107+
return new ConcurrentMapCache(CACHE_NAME, this.nativeCache, true,
108108
new SerializationDelegate(ConcurrentMapCacheTests.class.getClassLoader()));
109109
}
110110

0 commit comments

Comments
 (0)