I'm using Spring Cache abstraction and I have multiple caches defined. Sometimes, when data changes, I want to evict more than one caches.
Is there away to evict multiple cache using Spring's @CacheEvict
annotation?
Asked
Active
Viewed 4.1k times
42

yglodt
- 13,807
- 14
- 91
- 127
2 Answers
81
You can do this:
@Caching(evict = {
@CacheEvict("primary"),
@CacheEvict(value = "secondary", key = "#p0")
})
Check out the Reference for details
-
How to do if 1 is @CachePut & another one is @CacheEvict? – Satish Patro May 23 '19 at 13:32
-
3The Kotlin version of this is: `@Caching(evict = [ CacheEvict("primary"), CacheEvict(value = ["secondary"], key = "#p0") ])` – Jonas Pedersen Sep 14 '22 at 12:12
53
Keep it compact: You can evict multiple caches by enumerating them inside the @CacheEvict
annotation:
@CacheEvict(value = { "cache1", "cache2" }, allEntries = true)

yglodt
- 13,807
- 14
- 91
- 127
-
1Well yeah, assuming you want the same additional arguments (`allEntries = true`) to apply to all caches you specify. In my experience that ain't often. – Madbreaks Nov 17 '17 at 21:20
-
-
3Can mention keys like this `@CacheEvict(value = { "cache1", "cache2" }, key = "key")` – YohanK Oct 21 '21 at 12:19