利用redis生成订单id

书中人 2022年12月28日 691次浏览

订单id生成,每小时最大6位数订单。

public String getCode() {
        String  key  = "DT" + DateUtils.parseDateToStr("yyyyMMddHH", new Date());
        BoundValueOperations<String, Integer> opsForValue = redisTemplate.boundValueOps(key);
        Long code = opsForValue.increment(1);
        redisTemplate.expire(key, 1L, TimeUnit.HOURS);
        return key + String.format("%1$06d", code);
    }