mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 08:24:13 +00:00
Add resetCaches() method to general CacheManager interface
Closes gh-35845 See gh-35840
This commit is contained in:
Vendored
+13
@@ -130,4 +130,17 @@ public class JCacheCacheManager extends AbstractTransactionSupportingCacheManage
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetCaches() {
|
||||
CacheManager cacheManager = getCacheManager();
|
||||
if (cacheManager != null && !cacheManager.isClosed()) {
|
||||
for (String cacheName : cacheManager.getCacheNames()) {
|
||||
javax.cache.Cache<Object, Object> jcache = cacheManager.getCache(cacheName);
|
||||
if (jcache != null && !jcache.isClosed()) {
|
||||
jcache.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Vendored
+15
-2
@@ -51,8 +51,7 @@ class JCacheEhCacheApiTests extends AbstractValueAdaptingCacheTests<JCacheCache>
|
||||
this.cacheManager.createCache(CACHE_NAME_NO_NULL, new MutableConfiguration<>());
|
||||
this.nativeCache = this.cacheManager.getCache(CACHE_NAME);
|
||||
this.cache = new JCacheCache(this.nativeCache);
|
||||
Cache<Object, Object> nativeCacheNoNull =
|
||||
this.cacheManager.getCache(CACHE_NAME_NO_NULL);
|
||||
Cache<Object, Object> nativeCacheNoNull = this.cacheManager.getCache(CACHE_NAME_NO_NULL);
|
||||
this.cacheNoNull = new JCacheCache(nativeCacheNoNull, false);
|
||||
}
|
||||
|
||||
@@ -100,4 +99,18 @@ class JCacheEhCacheApiTests extends AbstractValueAdaptingCacheTests<JCacheCache>
|
||||
assertThat(cache.get(key).get()).isEqualTo(value);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resetCaches() {
|
||||
JCacheCacheManager cm = new JCacheCacheManager(cacheManager);
|
||||
org.springframework.cache.Cache cache = cm.getCache(CACHE_NAME);
|
||||
cache.put("key", "value");
|
||||
assertThat(cm.getCacheNames()).contains(CACHE_NAME);
|
||||
assertThat(cm.getCache(CACHE_NAME)).isNotNull().isSameAs(cache);
|
||||
assertThat(cacheManager.getCache(CACHE_NAME).iterator()).hasNext();
|
||||
cm.resetCaches();
|
||||
assertThat(cm.getCacheNames()).contains(CACHE_NAME);
|
||||
assertThat(cm.getCache(CACHE_NAME)).isNotNull().isSameAs(cache);
|
||||
assertThat(cacheManager.getCache(CACHE_NAME).iterator()).isExhausted();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.jspecify.annotations.Nullable;
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.1
|
||||
*/
|
||||
public interface CacheManager {
|
||||
@@ -47,4 +48,31 @@ public interface CacheManager {
|
||||
*/
|
||||
Collection<String> getCacheNames();
|
||||
|
||||
/**
|
||||
* Remove all registered caches from this cache manager if possible,
|
||||
* re-creating them on demand. After this call, {@link #getCacheNames()}
|
||||
* will possibly be empty and the cache provider will have dropped all
|
||||
* cache management state.
|
||||
* <p>Alternatively, an implementation may perform an equivalent reset
|
||||
* on fixed existing cache regions without actually dropping the cache.
|
||||
* This behavior will be indicated by {@link #getCacheNames()} still
|
||||
* exposing a non-empty set of names, whereas the corresponding cache
|
||||
* regions will not contain cache entries anymore.
|
||||
* <p>The default implementation calls {@link Cache#clear} on all
|
||||
* registered caches, retaining all caches as registered, satisfying
|
||||
* the alternative implementation path above. Custom implementations
|
||||
* may either drop the actual caches (re-creating them on demand) or
|
||||
* perform a more exhaustive reset at the actual cache provider level.
|
||||
* @since 7.0.2
|
||||
* @see Cache#clear()
|
||||
*/
|
||||
default void resetCaches() {
|
||||
for (String cacheName : getCacheNames()) {
|
||||
Cache cache = getCache(cacheName);
|
||||
if (cache != null) {
|
||||
cache.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+7
@@ -115,6 +115,13 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
|
||||
return this.cacheNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetCaches() {
|
||||
synchronized (this.cacheMap) {
|
||||
this.cacheMap.values().forEach(Cache::clear);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Common cache initialization delegates for subclasses
|
||||
|
||||
|
||||
+7
@@ -119,4 +119,11 @@ public class CompositeCacheManager implements CacheManager, InitializingBean {
|
||||
return Collections.unmodifiableSet(names);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetCaches() {
|
||||
for (CacheManager manager : this.cacheManagers) {
|
||||
manager.resetCaches();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
@@ -55,4 +55,9 @@ public class NoOpCacheManager implements CacheManager {
|
||||
return Collections.unmodifiableSet(this.cacheMap.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resetCaches() {
|
||||
this.cacheMap.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user