Reputation: 182
I developed a method that use @Cacheable
annotation. The code is:
@Cacheable(value="reporties" , key="{#root.methodName,#manager.name}")
public List<Employee> getReportiesForManager(Employee manager){
// code to fetch reporties its a Spring JDBC call
}
Now, I want to evict this cache after some events:
After that, the cache related with the manager should be evicted, in that way, the application will get new data instead of using the existing one in that cache. I developed the following method for that:
@CacheEvict(value="reporties",key="{#name}")
public void evictReportiesCache(String name){}
I call inside the method which updates the relations of the Manager and its reporties. However, this one works intermittently and I am not sure if that's the correct way to evict cache. Also the Cacheable uses #root.methodName
as part of key.
Can someone please help me with this cache eviction?
Upvotes: 5
Views: 35762
Reputation: 10727
You can think of the cache as a Map<key, value>
.
Every time you invoke a method with the @Cacheable
annotation you write in that Map
a value associated to what you define as they key.
Every time you invoke a method with the @CacheEvict
annotation you delete the value associated with the key. You can also delete all the entries in the Map.
Upvotes: 5
Reputation: 1306
The keys passed to Cacheable
and CacheEvict
annotations must be the same if they are identifying the same data.
If you want to evict your reports cache by the manager's name, you need to both cache and evict solely based on the manager's name.
Upvotes: 3