基于redis自增 id 编号生成器
2023-09-03 本文已影响0人
川流不息attitude
基于redis id 自增 序列化生成器
业务编号+yyMMdd+6位流水号
/**
* 工单格式化
*/
private final String WORK_ORDER_FORMAT = "000000";
/**
* 工单序列过期时间
*/
private final Integer WORK_ORDER_SN_EXPIRE = 24*60+5;
private String getWorkOrderSn(String bizCode) {
RedisService redisService = SpringContextHolder.getBean(RedisService.class);
String dateToStr = DateUtils.parseDateToStr(DateUtils.YYMMDD, new Date());
StringBuilder workOrderSn = new StringBuilder(bizCode).append(dateToStr);
String redisKey = new StringBuilder(REDIS_PREFIX + bizCode).append(StringPool.COLON).append(workOrderSn).toString();
Boolean exists = redisService.exists(redisKey);
if (exists) {
Long id = redisService.incr(redisKey);
String decimalFormat = NumberUtil.decimalFormat(WORK_ORDER_FORMAT, id);
return workOrderSn.append(decimalFormat).toString();
}
Long id = redisService.incr(redisKey);
// 第一次 redisKey 过期时间一天+5分钟
redisService.expire(redisKey, Duration.ofMinutes(WORK_ORDER_SN_EXPIRE));
String decimalFormat = NumberUtil.decimalFormat(WORK_ORDER_FORMAT, id);
return workOrderSn.append(decimalFormat).toString();
}