Null key returned for cache operation(maybe you are using named params on classes without debug info

在使用@Cacheable注解的时候遇到的这个问题

@Cacheable(value = RedisConstant.USER, key = "#id")
UserShow userShow(String id);

奇怪的是本地从没遇到过,生产上也是偶尔遇到,而@Cacheable注解肯定支持key属性,查看日志也没找到原因,最终在网上的一篇文章里面看到和debug模式有关,反射无法获取到参数值。

进入日志打印的类里面看一下:

private Object generateKey(CacheOperationContext context, @Nullable Object result) {
	Object key = context.generateKey(result);
	if (key == null) {
		throw new IllegalArgumentException("Null key returned for cache operation (maybe you are " +
				"using named params on classes without debug info?) " + context.metadata.operation);
	}
	if (logger.isTraceEnabled()) {
		logger.trace("Computed cache key '" + key + "' for operation " + context.metadata.operation);
	}
	return key;
}

好吧不管怎样,是由于context.generateKey(result);取不到值造成的,然而本地可以debug就不会遇到这个问题,生产上无法debug就会遇到???

两种解决方式:
  • 使用#p0
    在我理解中和MyBatisparam0类似?不使用反射查找出参数名,直接指定第一个参数,反正我一般不使用这个方法。

  • 自定义keyGenerator
    https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#cache-annotations-cacheable
    Redis配置类中注入bean

    @Bean(name = "idGenerator")
    public KeyGenerator idGenerator() {
    	return new KeyGenerator() {
    		@Override
    		public Object generate(Object target, Method method, Object... params) {
    			StringBuilder sb = new StringBuilder();
    			sb.append(params[0].toString());
    			return sb.toString();
    		}
    	};
    }
    

    注意我这里使用的是idGenerator,而我在使用的时候接口参数中一般只有id字段,如果有其他的可以参考上面的链接自行配置。
    接口注解重写:

    @Cacheable(value = RedisConstant.USER, keyGenerator = "idGenerator")
    UserShow userShow(String id);
    

问题解决,目前没再遇到过这个问题。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值