3

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?

piet.t
  • 11,718
  • 21
  • 43
  • 52
NanoNova
  • 829
  • 10
  • 19
  • Please have a look into https://stackoverflow.com/questions/32460606/how-do-i-use-the-key-in-a-condition-in-cacheable-annotation. it will help you. – Raheela Aslam Nov 14 '18 at 05:31
  • To be specific: a) `foreach` is not an elegant solution; b) I want to evict certain ids of cache under the namespace, so `startWith` spEl cannot suitable for this. – NanoNova Jan 29 '19 at 06:55

1 Answers1

5
  • @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
}
RazvanParautiu
  • 2,805
  • 2
  • 18
  • 21