有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring cache@cacheexecute匹配列表中的键?

我正在使用Spring缓存,并试图通过一个键(id)列表逐出缓存

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

我怎样才能做到这一点


共 (1) 个答案

  1. # 1 楼答案

    • @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.

    • 钥匙

    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
    }