> 文章列表 > 【通过redis生成编码】生成不重复的序列号

【通过redis生成编码】生成不重复的序列号

【通过redis生成编码】生成不重复的序列号

/*** 生成一个序列号,每天从0开始自增* yyyyMMdd0001* @Param leftCode编号特定前缀* */public String getSequence(String leftCode){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");String dateTime = simpleDateFormat.format(new Date());String key = MessageFormat.format("{0}:{1}","dmv:carapplicationcode"+":"+leftCode,dateTime);Long autoID = redisTemplate.opsForValue().increment(key,1);if(autoID == 1){//设置 hash 值在凌晨23;59:59 清理Date date = Date.from(LocalDateTime.of(LocalDate.now(), LocalTime.MAX).toInstant(ZoneOffset.of("+8")));redisTemplate.expireAt(key,date);}String value = org.apache.commons.lang.StringUtils.leftPad(String.valueOf(autoID),3,"0");String code = MessageFormat.format("{0}{1}",dateTime,value);return leftCode+code;}

redis中的存储
【通过redis生成编码】生成不重复的序列号

实际的生成效果
【通过redis生成编码】生成不重复的序列号