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 d5b77cbc1bb..a09c12c9947 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 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. @@ -51,6 +51,7 @@ import org.springframework.util.ObjectUtils; * @author Juergen Hoeller * @author Stephane Nicoll * @author Sam Brannen + * @author Brian Clozel * @since 4.3 * @see CaffeineCache */ @@ -188,8 +189,13 @@ public class CaffeineCacheManager implements CacheManager { @Override @Nullable public Cache getCache(String name) { - return this.cacheMap.computeIfAbsent(name, cacheName -> - this.dynamic ? createCaffeineCache(cacheName) : null); + 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); + } }