From 022fdcd67ee4811a5e73e5495280f26149b7446f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 29 Oct 2024 23:02:48 +0100 Subject: [PATCH] Provide removeCache method on Caffeine/ConcurrentMapCacheManager Closes gh-33813 --- .../cache/caffeine/CaffeineCacheManager.java | 13 ++++++++++++- .../cache/concurrent/ConcurrentMapCacheManager.java | 11 ++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) 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 2518f3a57a..2ad316114b 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-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -303,6 +303,17 @@ public class CaffeineCacheManager implements CacheManager { this.cacheMap.put(name, adaptCaffeineCache(name, cache)); } + /** + * Remove the specified cache from this cache manager, applying to + * custom caches as well as dynamically registered caches at runtime. + * @param name the name of the cache + * @since 6.1.15 + */ + public void removeCache(String name) { + this.customCacheNames.remove(name); + this.cacheMap.remove(name); + } + /** * Adapt the given new native Caffeine Cache instance to Spring's {@link Cache} * abstraction for the specified cache name. 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 585d8b2059..6dfa5a4eda 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-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -175,6 +175,15 @@ public class ConcurrentMapCacheManager implements CacheManager, BeanClassLoaderA return cache; } + /** + * Remove the specified cache from this cache manager. + * @param name the name of the cache + * @since 6.1.15 + */ + public void removeCache(String name) { + this.cacheMap.remove(name); + } + private void recreateCaches() { for (Map.Entry entry : this.cacheMap.entrySet()) { entry.setValue(createConcurrentMapCache(entry.getKey()));