Skip to content

Commit 6a08bfd

Browse files
committed
Avoid unnecessary synchronization for non-existent missing caches
Closes gh-23635
1 parent da44a24 commit 6a08bfd

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -87,25 +87,26 @@ public void initializeCaches() {
8787
@Override
8888
@Nullable
8989
public Cache getCache(String name) {
90+
// Quick check for existing cache...
9091
Cache cache = this.cacheMap.get(name);
9192
if (cache != null) {
9293
return cache;
9394
}
94-
else {
95-
// Fully synchronize now for missing cache creation...
95+
96+
// The provider may support on-demand cache creation...
97+
Cache missingCache = getMissingCache(name);
98+
if (missingCache != null) {
99+
// Fully synchronize now for missing cache registration
96100
synchronized (this.cacheMap) {
97101
cache = this.cacheMap.get(name);
98102
if (cache == null) {
99-
cache = getMissingCache(name);
100-
if (cache != null) {
101-
cache = decorateCache(cache);
102-
this.cacheMap.put(name, cache);
103-
updateCacheNames(name);
104-
}
103+
cache = decorateCache(missingCache);
104+
this.cacheMap.put(name, cache);
105+
updateCacheNames(name);
105106
}
106-
return cache;
107107
}
108108
}
109+
return cache;
109110
}
110111

111112
@Override

0 commit comments

Comments
 (0)