redis setIfAbsent返回null的问题及解决

起因

今天在写业务代码的时候,用到了StringRedisTemplate中redisTemplate.opsForValue().setIfAbsent()中并不是我期望的true或者false而是返回了null,起初我还以为是因为Returns:null when used in pipeline / transaction.这个原因,但是我并没有在业务中使用到事务。

setIfAbsent

原因

遂使用谷歌大法,才搜到原来这是因为Redisson的bug,网友给出的解决方案是去掉就好了,但是不可能因为这个问题就不使用Redisson,看来又看但一个帖子,说这是redisson的bug。

看到这里,我就想到去看看github中是不是有相关的issue呢?

解决

issue

果然,我们可以看到有两个issue是关于setIfAbsent返回null的问题,让我们去看一下回复数多的那一条

其中提到在#2811中修复

fixed

也可以明确看到关于此bug的修复代码

method

让我们一起看看这个BooleanNullSafeReplayConvertor是何方神圣,怎么解决的返回值为null的bug.

BooleanNullSafeReplayConvertor

其实解决的方式也很简单暴力,就是把之前的obj == null return null改为了obj == null return false并且去掉了泛型。

BooleanReplayConvertor

作者:LaiDeJi_原文地址:https://blog.csdn.net/LaiDeJi_/article/details/124992419

%s 个评论

要回复文章请先登录注册