diff --git a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java index a09c12c99473..239a7350cdc5 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java +++ b/spring-context-support/src/main/java/org/springframework/cache/caffeine/CaffeineCacheManager.java @@ -189,13 +189,11 @@ public Collection getCacheNames() { @Override @Nullable public Cache getCache(String name) { - if (this.dynamic) { - Cache cache = this.cacheMap.get(name); - return (cache != null) ? cache : this.cacheMap.computeIfAbsent(name, this::createCaffeineCache); - } - else { - return this.cacheMap.get(name); + Cache cache = this.cacheMap.get(name); + if (cache == null && this.dynamic) { + cache = this.cacheMap.computeIfAbsent(name, this::createCaffeineCache); } + return cache; } diff --git a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java index 8a29d1fd2900..2d993db5ba66 100644 --- a/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java +++ b/spring-context/src/main/java/org/springframework/cache/concurrent/ConcurrentMapCacheManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -165,13 +165,7 @@ public Collection getCacheNames() { public Cache getCache(String name) { Cache cache = this.cacheMap.get(name); if (cache == null && this.dynamic) { - synchronized (this.cacheMap) { - cache = this.cacheMap.get(name); - if (cache == null) { - cache = createConcurrentMapCache(name); - this.cacheMap.put(name, cache); - } - } + cache = this.cacheMap.computeIfAbsent(name, this::createConcurrentMapCache); } return cache; }