Skip to content

Commit ef699b6

Browse files
committed
Align ConcurrentMapCacheManager locking behavior with CaffeineCacheManager
Closes gh-30780 (cherry picked from commit 60865ea)
1 parent e440eb8 commit ef699b6

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,11 @@ public Collection<String> getCacheNames() {
189189
@Override
190190
@Nullable
191191
public Cache getCache(String name) {
192-
if (this.dynamic) {
193-
Cache cache = this.cacheMap.get(name);
194-
return (cache != null) ? cache : this.cacheMap.computeIfAbsent(name, this::createCaffeineCache);
195-
}
196-
else {
197-
return this.cacheMap.get(name);
192+
Cache cache = this.cacheMap.get(name);
193+
if (cache == null && this.dynamic) {
194+
cache = this.cacheMap.computeIfAbsent(name, this::createCaffeineCache);
198195
}
196+
return cache;
199197
}
200198

201199

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -166,13 +166,7 @@ public Collection<String> getCacheNames() {
166166
public Cache getCache(String name) {
167167
Cache cache = this.cacheMap.get(name);
168168
if (cache == null && this.dynamic) {
169-
synchronized (this.cacheMap) {
170-
cache = this.cacheMap.get(name);
171-
if (cache == null) {
172-
cache = createConcurrentMapCache(name);
173-
this.cacheMap.put(name, cache);
174-
}
175-
}
169+
cache = this.cacheMap.computeIfAbsent(name, this::createConcurrentMapCache);
176170
}
177171
return cache;
178172
}

0 commit comments

Comments
 (0)