使用@CacheEvict 多参数如何匹配删除

网友投稿 836 2022-11-11

使用@CacheEvict 多参数如何匹配删除

使用@CacheEvict 多参数如何匹配删除

目录@CacheEvict 多参数匹配删除解决思路方案一:单独写一个自定义的KeyGenerator方案二:@Cacheable(value=“XXX”, key=“xxxx”)

@CacheEvict 多参数匹配删除

如果@Cacheable(“XXX”)

Object getXXX(String a, String b, String c);

spring的缓存使用的key是ESPL表达式,然后翻看源码key默认用的生成方式是org.springframework.cache.interceptor.SimpleKeyGenerator

大于1个参数走的是最后一个方法

/**

* Generate a key based on the specified parameters.

*/

public static Object generateKey(Object... params) {

if (params.length == 0) {

return SimpleKey.EMPTY;

}

if (params.length == 1) {

Object param = params[0];

if (param != null && !param.getClass().isArray()) {

return param;

}

}

return new SimpleKey(params);

}

然后查看org.springframework.cache.interceptor.SimpleKey对应代码,发现返回的其实是SimpleKey

/**

* Create a new {@lhttp://ink SimpleKey} instance.

* @param elements the elements of the key

*/

public SimpleKey(Object... elements) {

Assert.notNull(elements, "Elements must not be null");

this.params = new Object[elements.length];

System.arraycopy(elements, 0, this.params, 0, elements.length);

this.hashCode = Arrays.deepHashCode(this.params);

}

解决思路

方案一:单独写一个自定义的KeyGenerator

处理对应的key。(之前的redis的文章已写过,所以不重复写了)

下面博文的 MyKeyGenerator 这个类

sprintboot使用spring-security包,缓存内存与redis共存

方案二:@Cacheable(value=“XXX”, key=“xxxx”)

@CacheEvict(value=“XXX”, key=“xxxx”)

做相应的key配置

数组的话可以使用 key = “#root.args[0]”

参数参考如下:

名字

位置

描述

示例

methodName

root object

当前被调用的方法名

#root.methodName

method

root object

当前被调用的方法

#root.method .name

target

root object

当前被调用的目标对象

#root.target

targetClass

root object

当前被调用的目标对象类

#root.targetClass

args

root object

当前被调用的方法的参数列表

#root.args[0]

caches

root object

当前方法调用使用的缓存列表

#root.caches[0].name

argument name

evaluation context

方法参数的名字,可以直接#参数名,也可以使用#p0或#a0的形式,0代表参数的索引

#iban、#a0、#p0

result

evaluation context

方法执行后的返回值

#result

以上为个人经验,希望能给大家一个参考,也希望大家

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:(转)使用HttpClient发送HTTPS请求以及配置Tomcat支持SSL
下一篇:后端框架学习-----mybatis(使用mybatis框架遇到的问题)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~