Skip to content

Commit 96aadc2

Browse files
committed
Add resetCaches() method to general CacheManager interface
Closes gh-35845 See gh-35840
1 parent 2c6ccae commit 96aadc2

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,17 @@ protected Collection<Cache> loadCaches() {
130130
return null;
131131
}
132132

133+
@Override
134+
public void resetCaches() {
135+
CacheManager cacheManager = getCacheManager();
136+
if (cacheManager != null && !cacheManager.isClosed()) {
137+
for (String cacheName : cacheManager.getCacheNames()) {
138+
javax.cache.Cache<Object, Object> jcache = cacheManager.getCache(cacheName);
139+
if (jcache != null && !jcache.isClosed()) {
140+
jcache.clear();
141+
}
142+
}
143+
}
144+
}
145+
133146
}

spring-context-support/src/test/java/org/springframework/cache/jcache/JCacheEhCacheApiTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ void setup() {
5151
this.cacheManager.createCache(CACHE_NAME_NO_NULL, new MutableConfiguration<>());
5252
this.nativeCache = this.cacheManager.getCache(CACHE_NAME);
5353
this.cache = new JCacheCache(this.nativeCache);
54-
Cache<Object, Object> nativeCacheNoNull =
55-
this.cacheManager.getCache(CACHE_NAME_NO_NULL);
54+
Cache<Object, Object> nativeCacheNoNull = this.cacheManager.getCache(CACHE_NAME_NO_NULL);
5655
this.cacheNoNull = new JCacheCache(nativeCacheNoNull, false);
5756
}
5857

@@ -100,4 +99,18 @@ void testPutIfAbsentNullValue() {
10099
assertThat(cache.get(key).get()).isEqualTo(value);
101100
}
102101

102+
@Test
103+
void resetCaches() {
104+
JCacheCacheManager cm = new JCacheCacheManager(cacheManager);
105+
org.springframework.cache.Cache cache = cm.getCache(CACHE_NAME);
106+
cache.put("key", "value");
107+
assertThat(cm.getCacheNames()).contains(CACHE_NAME);
108+
assertThat(cm.getCache(CACHE_NAME)).isNotNull().isSameAs(cache);
109+
assertThat(cacheManager.getCache(CACHE_NAME).iterator()).hasNext();
110+
cm.resetCaches();
111+
assertThat(cm.getCacheNames()).contains(CACHE_NAME);
112+
assertThat(cm.getCache(CACHE_NAME)).isNotNull().isSameAs(cache);
113+
assertThat(cacheManager.getCache(CACHE_NAME).iterator()).isExhausted();
114+
}
115+
103116
}

spring-context/src/main/java/org/springframework/cache/CacheManager.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*
2828
* @author Costin Leau
2929
* @author Sam Brannen
30+
* @author Juergen Hoeller
3031
* @since 3.1
3132
*/
3233
public interface CacheManager {
@@ -47,4 +48,31 @@ public interface CacheManager {
4748
*/
4849
Collection<String> getCacheNames();
4950

51+
/**
52+
* Remove all registered caches from this cache manager if possible,
53+
* re-creating them on demand. After this call, {@link #getCacheNames()}
54+
* will possibly be empty and the cache provider will have dropped all
55+
* cache management state.
56+
* <p>Alternatively, an implementation may perform an equivalent reset
57+
* on fixed existing cache regions without actually dropping the cache.
58+
* This behavior will be indicated by {@link #getCacheNames()} still
59+
* exposing a non-empty set of names, whereas the corresponding cache
60+
* regions will not contain cache entries anymore.
61+
* <p>The default implementation calls {@link Cache#clear} on all
62+
* registered caches, retaining all caches as registered, satisfying
63+
* the alternative implementation path above. Custom implementations
64+
* may either drop the actual caches (re-creating them on demand) or
65+
* perform a more exhaustive reset at the actual cache provider level.
66+
* @since 7.0.2
67+
* @see Cache#clear()
68+
*/
69+
default void resetCaches() {
70+
for (String cacheName : getCacheNames()) {
71+
Cache cache = getCache(cacheName);
72+
if (cache != null) {
73+
cache.clear();
74+
}
75+
}
76+
}
77+
5078
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ public Collection<String> getCacheNames() {
115115
return this.cacheNames;
116116
}
117117

118+
@Override
119+
public void resetCaches() {
120+
synchronized (this.cacheMap) {
121+
this.cacheMap.values().forEach(Cache::clear);
122+
}
123+
}
124+
118125

119126
// Common cache initialization delegates for subclasses
120127

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,11 @@ public Collection<String> getCacheNames() {
119119
return Collections.unmodifiableSet(names);
120120
}
121121

122+
@Override
123+
public void resetCaches() {
124+
for (CacheManager manager : this.cacheManagers) {
125+
manager.resetCaches();
126+
}
127+
}
128+
122129
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ public Collection<String> getCacheNames() {
5555
return Collections.unmodifiableSet(this.cacheMap.keySet());
5656
}
5757

58+
@Override
59+
public void resetCaches() {
60+
this.cacheMap.clear();
61+
}
62+
5863
}

0 commit comments

Comments
 (0)