0

I am calling fetchCatchAndClear method where I am passing List and it consists of cache names. Can someone help me how to iterate over the list and clear the cache based on the Cache Name coming from List of String. Also if the list is empty I should clear all the caches present.

1 Answer 1

2

A rather simple approach which sticks to the org.springframework.cache.CacheManager could be the following:

List<String> cacheNames = List.of("aCache", "anotherCache"); // the list you are passing in
CacheManager cacheManager = new SimpleCacheManager(); // any cache manager you are injecting from anywhere

// a simple iteration, exception handling omitted for readability reasons
cacheNames.forEach(cacheName -> cacheManager.getCache(cacheName).clear());

Evicting all caches is also that simple, except that you have to query the relevant cache names from the very same cache manager:

CacheManager cacheManager = new SimpleCacheManager();
Collection<String> cacheNames = cacheManager.getCacheNames();

cacheNames.forEach(cacheName -> cacheManager.getCache(cacheName).clear());

If you just want to evict a single cache entry you could either do it programmatically like: cacheManager.getCache(cacheName).evict(cacheKey); or annotation-based like

@CacheEvict(value = "yourCacheName", key = "#cacheKey")
public void evictSingleCacheValue(String cacheKey) {
}


@CacheEvict(value = "yourCacheName", allEntries = true)
public void evictAllCacheValues() {
}
9
  • Please verify if I could write something of this type - public List<String> fetchCache(List<String cacheNames) { CacheManager cacheManager = new SimpleCacheManager(); if(!cacheNames.isEmpty()) { cacheNames.forEach(cacheName -> cacheManager.getCache(cacheName).clear()); } else{ Collection<String> cacheNames = cacheManager.getCacheNames(); cacheNames.forEach(cacheName -> cacheManager.getCache(cacheName).clear()); } return cacheNames;
    – Meenal
    Commented Mar 26, 2019 at 3:15
  • I didnot understand what difference it makes in clearing cache as a whole and clearing by specific name. Let me know if the above code will solve my purpose.
    – Meenal
    Commented Mar 26, 2019 at 3:18
  • Also how about using annotation @CacheEvict for both cases
    – Meenal
    Commented Mar 26, 2019 at 3:24
  • Yes the code in your comment is the same like the one that I provided, I just omitted the surounding method definition. You can use it this way. Remember both variants clear the whole caches. If you just want to evict certain cache entries, then use cacheManager.getCache(cacheName).evict(cacheKey). For @CacheEvict, please see my updated answer.
    – mle
    Commented Mar 26, 2019 at 6:29
  • Thankx I have added the same code but now my doubt is that .getCacheNames are coming as empty. I have a class where Iam using Cacheable annotaion giving the cache names. Is there any link between those two, I mean how will CacheManager identifies the Cacheable methods and collect info from it.
    – Meenal
    Commented Mar 26, 2019 at 10:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.