In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章主要介绍"SpringBoot @Cacheable自定义KeyGenerator方式是什么",在日常操作中,相信很多人在SpringBoot @Cacheable自定义KeyGenerator方式是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"SpringBoot @Cacheable自定义KeyGenerator方式是什么"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
@Cacheable自定义KeyGenerator1. 概述
SpringBoot 使用 @Cacheable 可以方便的管理缓存数据,在不指定 key 属性的情况下,默认使用 SimpleKeyGenerator 生成 key。除此之外,我们也可以自定义实现 KeyGenerator 接口,生成自己的 key 名称策略。
2. MySimpleKey 类
MySimpleKey类的作用是存放参数数据,必须实现equals、hashCode。如果需要自定义key格式,同样需要实现toString接口,下面的例子是把参数用逗号分隔。
public class MySimpleKey implements Serializable { public static final MySimpleKey EMPTY = new MySimpleKey(new Object[0]); private final Object[] params; private transient int hashCode; public MySimpleKey(Object... elements) { Assert.notNull(elements, "Elements must not be null"); this.params = (Object[])elements.clone(); this.hashCode = Arrays.deepHashCode(this.params); } public boolean equals(@Nullable Object other) { return this == other || other instanceof MySimpleKey && Arrays.deepEquals(this.params, ((MySimpleKey)other).params); } public final int hashCode() { return this.hashCode; } public String toString() { return StringUtils.arrayToCommaDelimitedString(this.params); }}3. MyKeyGenerator 类
MyKeyGenerator 实现 KeyGenerator 的接口,里面只有一个 generate 方法
public class MyKeyGenerator implements KeyGenerator { @Override public Object generate(Object o, Method method, Object... objects) { if (objects.length == 0) { return MySimpleKey.EMPTY; } else { if (objects.length == 1) { Object param = objects[0]; if (param != null && !param.getClass().isArray()) { return param; } } return new MySimpleKey(objects); } }}
定义MyKeyGenerator Bean:
@Componentpublic class MyRedisConf { @Bean public MyKeyGenerator myKeyGenerator(){ return new MyKeyGenerator(); }}4. 配置keyGenerator
在 @Cacheable 配置 keyGenerator 属性,值就是前面配置的Bean名称
@Override @Cacheable(value = {"REDIS:GETSTRING3"}, keyGenerator = "myKeyGenerator") public String getString3(String tag, String name) { return tag + " " + name; }
测试结果如下,tag、name 参数确实以逗号分隔
127.0.0.1:6379[5]> KEYS *
1) "REDIS:GETSTRING3::hello,zhangsan"
Spring-Cache key设置第一种方式:手动设置
为了便于key的不重复,我们可以手动设置key有类名、方法名、参数等组合
属性名称
描述
示例
methodName
当前方法名
#root.methodName
method
当前方法
#root.method.name
target
当前被调用的对象
#root.target
targetClass
当前被调用的对象的class
#root.targetClass
args
当前方法参数组成的数组
#root.args[0]
caches
当前被调用的方法使用的Cache
#root.caches[0].name
key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#param"
如下图所示
The second way: custom keyGenerator
1, Custom CacheKeyGenerator implementation KeyGenerator
public class CacheKeyGenerator implements KeyGenerator { /** * (non-Javadoc) *
* Title: generate *
* * @param target * @param method * @param params * @return * @see org.springframework.cache.interceptor.KeyGenerator#generate(java.lang.Object, * java.lang.reflect.Method, java.lang.Object[]) */ @Override public Object generate(Object target, Method method, Object... params) { StringBuilder key = new StringBuilder(); key.append(target.getClass().getSimpleName()).append(":").append(method.getName()).append(":"); if (params.length == 0) { return key.toString(); } for (int i = 0; i
< params.length; i++) { Object param = params[i]; if (param == null || param instanceof LogableParam) { del(key); } else if (ClassUtils.isPrimitiveArray(param.getClass())) { int length = Array.getLength(param); for (int j = 0; j < length; j++) { key.append(Array.get(param, j)); key.append(','); } } else if (ClassUtils.isPrimitiveOrWrapper(param.getClass()) || param instanceof String) { key.append(param); } else { key.append(param.toString()); } key.append('-'); } del(key); return key.toString(); } private StringBuilder del(StringBuilder stringBuilder) { if (stringBuilder.toString().endsWith("-")) { stringBuilder.deleteCharAt(stringBuilder.length() - 1); } return stringBuilder; }} 2、配置xml 3、配置注解 如下图所示At this point, the study of "SpringBoot @Cacheable custom KeyGenerator mode" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.