Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring使用另一个签名从方法中逐出缓存_Java_Spring_Spring Cache - Fatal编程技术网

Java Spring使用另一个签名从方法中逐出缓存

Java Spring使用另一个签名从方法中逐出缓存,java,spring,spring-cache,Java,Spring,Spring Cache,我将一些查询缓存在一个方法中 @Override @Cacheable(cacheNames = "user-actions") public UserAction getUserAction(UUID userId) { ... } 我想用另一种方法清除缓存。例如,如果方法具有相同的签名,它就可以工作 @CacheEvict(cacheNames = "user-actions") pub

我将一些查询缓存在一个方法中

    @Override
    @Cacheable(cacheNames = "user-actions")
    public UserAction getUserAction(UUID userId) {
        ...
    }
我想用另一种方法清除缓存。例如,如果方法具有相同的签名,它就可以工作

    @CacheEvict(cacheNames = "user-actions")
    public void evictUserLevel(UUID userId) {
        log.info("Cache user-actions has been evicted");
    }
但是,如果我不将
userId
传递给要退出缓存的方法,或者如果它有多个参数,是否有办法退出缓存?这不起作用:

    @CacheEvict(cacheNames = "user-actions")
    public void processEvent(UserEvent event, UUID userId) {
        ...
    }
这对我有用

    @CacheEvict(cacheNames = "user-events", key = "#root.args[1]")
    public void processEvent(UserEvent event, UUID userId) {
        ...
    }
root.args
-表示方法参数,而
[1]
-是从

默认密钥生成 由于缓存本质上是键值存储,缓存方法的每次调用都需要转换为适合缓存访问的键。开箱即用的缓存抽象使用一个基于以下算法的简单的
KeyGenerator

  • 如果未给出参数,则返回0

  • 如果只给出一个参数,则返回该实例

  • 如果给定了多个参数,则返回从所有参数的散列计算出的键


因此,下面的签名不起作用,因为密钥是根据
事件
用户ID
计算的

@cacheexecute(cacheNames=“用户操作”)
public void processEvent(UserEvent事件,UUID userId){
...
}

但若我并没有将userId传递给要退出缓存的方法,或者若它有多个参数,那个么有办法退出缓存吗

无参数 设置
allEntries=true
,这将清除所有条目

@cacheexecute(cacheNames=“用户操作”,allEntries=true)
公共资源(全部){
}
用于多个参数 指定
键的参数,有关详细信息,请参阅

@cacheexecute(cacheNames=“用户操作”,key=“#userId”)
public void processEvent(UserEvent事件,UUID userId){
...
}

指定要用作缓存键的方法参数(或将缓存设置为全部退出)。