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

标签 java spring spring-cache

我正在使用 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/

相关文章:

Java 具有优先级队列的可比较对象

java - 创建新的随机音频文件时,播放随机音频onClick不会重置-Android

java - @Async 在升级到 Spring Boot 1.3.3 后无法正常工作

java - 无法保存实体可嵌入 ID

java - 使用自定义刷新和驱逐 java 在 DAO 上实现缓存

spring - javanica @HystrixCommand 和 spring @Cacheable 执行顺序

java - 如何在不覆盖默认 spring-cache 的情况下创建辅助 CacheManager

java - 使用 Apache HTTP 服务器提供错误页面

java - 如何在 Java 中使用泛型来使用两个接口(interface)作为参数?

java - Spring JPA 规范 API 的 Mockito 不完全执行动态查询