[英]Spring evict cache from method with another signature
I cache some queries in one method我用一种方法缓存一些查询
@Override
@Cacheable(cacheNames = "user-actions")
public UserAction getUserAction(UUID userId) {
...
}
I'd like to evict cache in another method.我想用另一种方法驱逐缓存。 It works if method has the same signature, for example
例如,如果方法具有相同的签名,它就可以工作
@CacheEvict(cacheNames = "user-actions")
public void evictUserLevel(UUID userId) {
log.info("Cache user-actions has been evicted");
}
But is there ways to evict cache if I don't pass userId
to method in which cache will be evicted, or if it has more than one parameter?但是,如果我不将
userId
传递给将驱逐缓存的方法,或者如果它有多个参数,是否有方法驱逐缓存? This doesn't work:这不起作用:
@CacheEvict(cacheNames = "user-actions")
public void processEvent(UserEvent event, UUID userId) {
...
}
This works for me这对我有用
@CacheEvict(cacheNames = "user-events", key = "#root.args[1]")
public void processEvent(UserEvent event, UUID userId) {
...
}
root.args
- means method arguments and [1]
- is an index of an argument root.args
- 表示方法 arguments 和[1]
- 是参数的索引
Quoting from the documentation从文档中引用
Since caches are essentially key-value stores, each invocation of a cached method needs to be translated into a suitable key for cache access.由于缓存本质上是键值存储,因此缓存方法的每次调用都需要转换为适合缓存访问的键。 Out of the box, the caching abstraction uses a simple
KeyGenerator
based on the following algorithm:开箱即用,缓存抽象使用基于以下算法的简单
KeyGenerator
:
If no params are given, return 0.如果没有给出参数,则返回 0。
If only one param is given, return that instance.如果只给出一个参数,则返回该实例。
If more the one param is given, return a key computed from the hashes of all parameters.如果给出了多个参数,则返回从所有参数的哈希计算的键。
Hence below signature does not work as key is computed from event
and userId
.因此,下面的签名不起作用,因为密钥是从
event
和userId
计算出来的。
@CacheEvict(cacheNames = "user-actions")
public void processEvent(UserEvent event, UUID userId) {
...
}
But is there ways to evict cache if I don't pass userId to method in which cache will be evicted, or if it has more than one parameter?
但是,如果我不将 userId 传递给将驱逐缓存的方法,或者如果它有多个参数,是否有方法驱逐缓存?
set allEntries=true
, which will clear all entries.设置
allEntries=true
,这将清除所有条目。
@CacheEvict(cacheNames = "user-actions", allEntries = true)
public void evictAll() {
}
specify the parameter for the key
, refer to Custom Key Generation Declaration for details.指定
key
的参数,详见自定义密钥生成声明。
@CacheEvict(cacheNames = "user-actions", key = "#userId")
public void processEvent(UserEvent event, UUID userId) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.