Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cache: Evict multiple caches

People also ask

How do I evict multiple caches in spring?

Spring provides two ways to evict a cache, either by using the @CacheEvict annotation on a method, or by auto-wiring the CacheManger and clearing it by calling the clear() method.

How does cache evict work?

Cache eviction is a feature where file data blocks in the cache are released when fileset usage exceeds the fileset soft quota, and space is created for new files. The process of releasing blocks is called eviction. However, file data is not evicted if the file data is dirty.

What is the use of @EnableCaching?

This allows for customizing the strategy for cache key generation, per Spring's KeyGenerator SPI. Normally, @EnableCaching will configure Spring's SimpleKeyGenerator for this purpose, but when implementing CachingConfigurer , a key generator must be provided explicitly.


You can do this:

@Caching(evict = {
    @CacheEvict("primary"),
    @CacheEvict(value = "secondary", key = "#p0")
})

Check out the Reference for details


Keep it compact: You can evict multiple caches by enumerating them inside the @CacheEvict annotation:

@CacheEvict(value = { "cache1", "cache2" }, allEntries = true)