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?
2 Answers
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? Commented May 23, 2019 at 13:32
-
4The Kotlin version of this is:
@Caching(evict = [ CacheEvict("primary"), CacheEvict(value = ["secondary"], key = "#p0") ])
Commented Sep 14, 2022 at 12:12
Keep it compact: You can evict multiple caches by enumerating them inside the @CacheEvict
annotation:
@CacheEvict(value = { "cache1", "cache2" }, allEntries = true)
-
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. Commented Nov 17, 2017 at 21:20 -
-
3Can mention keys like this
@CacheEvict(value = { "cache1", "cache2" }, key = "key")
– YohanKCommented Oct 21, 2021 at 12:19