记一次redis操作在SessionCallback中开启事务operations.opsForValue().get(key)获取不到值的问题

BUG代码场景:

String key ="ceshi1";
        String newValue = "6";
        String oldValue = "7";
        //这里是能取到值的
        String s = redisOperator.get(key);
        System.out.println(s);

        SessionCallback<List<Object>> sessionCallback = new SessionCallback() {
            @Override
            public List<Object> execute(RedisOperations operations) throws DataAccessException {
                operations.multi();
                String redisValue = (String)operations.opsForValue().get(key);
                //这里没有获取到值。
                System.out.println(redisValue + "--"+ oldValue);
                if(Objects.equals(redisValue, oldValue) || Objects.isNull(redisValue)){
                    System.out.println( " in set");
                    operations.opsForValue().set(key, newValue);

                }
                return operations.exec();
            }
        };
        List<Object> execute = redisOperator.execute(sessionCallback);
        System.out.println(JsonUtil.toJson(execute));

追踪到源码时:因为开启了事务,所以 会进入如下所示,return null; 如果是没有开启事务,就会有值。
在这里插入图片描述
解决方案:
暂时没有想到 好的方式:等我想到了在来 补充!

补充,目前想到的是 通过自己写 lua脚本 去实现,lua脚本的方式 没有代码写起来那么方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值