redisTemplate ValueOperations增量获取空值

时间:2018-08-26 14:17:03

标签: spring-boot redis spring-data-redis

首先,我必须说密钥存在于redis中, 该密钥是一个自增序列, 但有时获取此密钥为空。 我使用了spring-boot:2.0.4.0.release 我的期望是一次获得一个自增1来生成订单号 您能为我的方案提供解决方案吗?谢谢!

@Override
@Transactional(rollbackFor = Exception.class)
public boolean paymentBatchMeter(UserDescriptor user, String areaCode, String newPrice) {
    List<Meter> meters = meterService.findAllByFilterAndAreaCode(null, areaCode, user);
    if (meters != null && meters.size() > 0) {
        List<TaskValue> taskValueList = new ArrayList<>();
        meters.forEach(meter -> {
            if (meter.getState() == 1) {
                Task task = new Task();
                task.setCode(meter.getCode());

                TaskValue taskValue = new TaskValue();
                taskValue.setId(task.getId());
                taskValue.setMeterType(meter.getMeterDicCode());
                taskValue.setCode(meter.getCode());

                Long orderIndex = redisTemplate.opsForValue().increment("athena:order:index", 1);
                if (orderIndex == null) {
                    throw new IllegalStateException("error!");
                }
                taskValueList.add(taskValue);

                Order order = new Order();
                order.setCustomerId(meter.getCustomerId());
                order.setCode(orderIndex.toString());
                order.setCreateTime(new Date());
                order.setMeterId(meter.getId());
                order.setState(0);
                order.setRechargeMoney(new BigDecimal(newPrice));
                List<DicProjectItem> dicProjectItems = dicProjectItemMapper.getDicProjectItemAll("D10015", 1);
                String dicProjectItemId = dicProjectItems.stream().filter(p -> "002".equals(p.getCode())).collect(Collectors.toList()).get(0).getId();
                order.setRechargeWayDicId(dicProjectItemId);
                order.setRechargeState(1);
                order.setTaskId(task.getId());
                order.setOperatorId(user.getId());
            }
        });

        redisTemplate.setEnableTransactionSupport(true);
        taskValueList.forEach(taskValue -> {
            if ("004".equals(taskValue.getMeterType())) {
                redisTemplate.convertAndSend(TOPIC, taskValue.getCode());
                redisTemplate.opsForList().rightPush("athena:concentrator:meter:task:" + taskValue.getCode(), JSON.toJSONString(taskValue));
            } else {
                redisTemplate.opsForList().rightPush("athena:meter:task:" + taskValue.getCode(), JSON.toJSONString(taskValue));
            }
        });
        redisTemplate.setEnableTransactionSupport(false);

        return true;
    }

    return false;
}

0 个答案:

没有答案