Skip to content

Commit 57a1d40

Browse files
committed
Fix getCacheNames() concurrent access in NoOpCacheManager
Closes gh-35842
1 parent bc3431f commit 57a1d40

File tree

1 file changed

+6
-24
lines changed

1 file changed

+6
-24
lines changed

spring-context/src/main/java/org/springframework/cache/support/NoOpCacheManager.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.util.Collection;
2020
import java.util.Collections;
21-
import java.util.LinkedHashSet;
22-
import java.util.Set;
2321
import java.util.concurrent.ConcurrentHashMap;
2422
import java.util.concurrent.ConcurrentMap;
2523

@@ -33,44 +31,28 @@
3331
* for disabling caching, typically used for backing cache declarations
3432
* without an actual backing store.
3533
*
36-
* <p>Will simply accept any items into the cache not actually storing them.
34+
* <p>This implementation will simply accept any items into the cache,
35+
* not actually storing them.
3736
*
3837
* @author Costin Leau
3938
* @author Stephane Nicoll
39+
* @author Juergen Hoeller
4040
* @since 3.1
4141
* @see NoOpCache
4242
*/
4343
public class NoOpCacheManager implements CacheManager {
4444

45-
private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<>(16);
45+
private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<>(16);
4646

47-
private final Set<String> cacheNames = new LinkedHashSet<>(16);
4847

49-
50-
/**
51-
* This implementation always returns a {@link Cache} implementation that will not store items.
52-
* Additionally, the request cache will be remembered by the manager for consistency.
53-
*/
5448
@Override
5549
public @Nullable Cache getCache(String name) {
56-
Cache cache = this.caches.get(name);
57-
if (cache == null) {
58-
this.caches.computeIfAbsent(name, NoOpCache::new);
59-
synchronized (this.cacheNames) {
60-
this.cacheNames.add(name);
61-
}
62-
}
63-
return this.caches.get(name);
50+
return this.cacheMap.computeIfAbsent(name, NoOpCache::new);
6451
}
6552

66-
/**
67-
* This implementation returns the name of the caches previously requested.
68-
*/
6953
@Override
7054
public Collection<String> getCacheNames() {
71-
synchronized (this.cacheNames) {
72-
return Collections.unmodifiableSet(this.cacheNames);
73-
}
55+
return Collections.unmodifiableSet(this.cacheMap.keySet());
7456
}
7557

7658
}

0 commit comments

Comments
 (0)