我正在使用 Spring 缓存并尝试通过键(id)列表逐出缓存。
@CacheEvict(value="cacheName",key=?, condition=? )
public void deleteByIds(List<Integer> ids){...}
我怎样才能做到这一点?
最佳答案
@CacheEvict
Annotation indicating that a method (or all methods on a class) triggers a cache evict operation.
- 缓存名称或值
Names of the caches in which method invocation results are stored.
- 条件
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.
您的问题的解决方案: 假设列表中的每个对象都被缓存到,例如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
}
关于java - Spring缓存@CacheEvict匹配列表中的键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53293043/