简单理解自定义 KeyGenerator
一般情况我们在 spring boot 中会使用 redis 作为缓存 但我们是需要自定义 cache key 的生成方式
1.为什么不使用 spring 默认的 生成策略?
先看看源码:
public class DefaultKeyGenerator implements KeyGenerator {
public static final int NO_PARAM_KEY = 0;
public static final int NULL_PARAM_KEY = 53;
public DefaultKeyGenerator() {
}
public Object generate(Object target, Method method, Object... params) {
if (params.length == 0) {
//没有参数直接返回 0
return 0;
} else {
if (params.length == 1) {
Object param = params[0];
if (param == null) {
//有一个参数 并且为 null 返回 53
return 53;
}
//参数不是数组 直接返回参数
if (!param.getClass().isArray()) {