import org.springframework.data.redis.core.StringRedisTemplate;
private final StringRedisTemplate redisTemplate;
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
private String createBusinessNo() { //根据日期格式生成规则单号 String key = DATE_FORMAT.format(new Date()); String cacheNo = redisTemplate.opsForValue().get("plan:xxxTask:batchNo:" + key); Integer value = 1; if (Func.isNotEmpty(cacheNo)) { value = Integer.parseInt(cacheNo); } NumberFormat numberFormat = NumberFormat.getNumberInstance(); //自增3位数格式 numberFormat.setMinimumIntegerDigits(3); numberFormat.setGroupingUsed(false); String batchNo = key + "-" + numberFormat.format(value); value++; //第一个参数:redis的key值 //第二个参数:需要缓存的自增数字 //第三个参数:时间 //第四个参数:时间单位 redisTemplate.opsForValue().set("plan:xxxTask:batchNo:" + key, String.valueOf(value), 1, TimeUnit.DAYS); return batchNo; }