Ayushi
Ayushi

Reputation: 425

Dynamic argument for @CacheEvict

I am implementing EHcache in my project and i have written a method that will remove all entries from the cache. The method works fine, here's the code snippet:

 public void removeEntriesFromCache(String cacheName){
    CacheManager.getInstance.getCache(cacheName).removeAll();
}

I am eventually going to expose this method as a rest service so that user could call the service along with cache name that needs to be invalidated. However, i would want to be able to do the same using @CacheEvict. I know i can do this:

 @CacheEvict(name ="myCache" , allEntries=true)
public void removeEntriesFromCache(){

}

But, this method will remove entries from just one cache , in this case it will remove all cache entries from "myCache". I want to be able to resolve the cache name dynamically at runtime as specified by the user.

Is it possible to achieve something like this:

@CacheEvict(name ="${cacheName}" , allEntries=true)
public void removeEntriesFromCache(String cacheName){

}

OR

@CacheEvict(name ="#cacheName" , allEntries=true)
public void removeEntriesFromCache(String cacheName){

}

Your suggestions would be welcome.

Thanks

Upvotes: 2

Views: 3306

Answers (1)

Ákos Ratku
Ákos Ratku

Reputation: 1421

@CacheEvict documentation clearly states where you can use SpEl. You can't use SpEl in the name attribute, but you can inject CacheManager to the bean you are annotating now, get the Cache by name and clear it.

cacheManager.getCache(cacheName).clear()

Upvotes: 3

Related Questions