Spring缓存@CacheEvict匹配列表中的键?

Ale*_*lex 3 java spring spring-cache

我正在使用 Spring 缓存并尝试通过键(id)列表逐出缓存。

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

我怎样才能做到这一点?

Rzv*_*van 5

  • @CacheEvict

指示某个方法(或类上的所有方法)触发缓存逐出操作的注释。

  • 缓存名称或值

存储方法调用结果的缓存的名称。

  • 健康)状况

用于使方法缓存有条件的表达式。

  • 钥匙

root.method、root.target 和 root.caches 分别用于对方法、目标对象和受影响的缓存的引用。

问题的解决方案:假设列表中的每个对象都被缓存到,例如cacheName =“entities”,并且对于键,您可以使用实体ID(它是整数值的字符串表示形式),您应该编写第二个驱逐缓存的方法。

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
}
Run Code Online (Sandbox Code Playgroud)