Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring cache @CacheEvict matches key in a list?

I'm using Spring cache and trying to evict cache by a list of key(id).

@CacheEvict(value="cacheName",key=?, condition=? )
public void deleteByIds(List<Integer> ids){...}

How can I manage to do that?

like image 951
NanoNova Avatar asked Oct 13 '25 05:10

NanoNova


1 Answers

  • @CacheEvict

Annotation indicating that a method (or all methods on a class) triggers a cache evict operation.

  • The cachaName or value

Names of the caches in which method invocation results are stored.

  • Condition

Expression used for making the method caching conditional.

  • Key

root.method, root.target, and root.caches for references to the method, target object, and affected cache(s) respectively.

Solution for your problem: Assuming that every object from the List it is cached into, for example cacheName = "entities" and for the key you can use the entity ID (which is the String representation of the Integer value) you should write a second method to evict the cache.

public void deleteByIds(List<Intiger> intigers){
 for(Intigier i : intigers){
  deleteEntity(i.toString());
 }
}

@CacheEvict(cacheName = "entities", key="entityId", condition="entityId!=null")
private void deleteEntity(String entityId){
 //processing : for ex delete from the database and also remove from cache
}
like image 81
Rzv Razvan Avatar answered Oct 14 '25 21:10

Rzv Razvan