@Resource
private static StringRedisTemplate stringRedisTemplate;
// 2024年1月1日的时间戳
private static final long BEGIN_TIMESTAMP = 1704067200;
private static final int MOVE_BIT = 32;
// 由符号位 时间戳 序列号组成的唯一id
public static long generateId(String prefix){
// 1、生成时间戳
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;
// 2、生成序列号 自增长
// 设定格式
String date = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
Long serialNumber = stringRedisTemplate.opsForValue().increment("icr:" + prefix + ":" + date);
// 拼接 位运算拼接
return timestamp << MOVE_BIT | serialNumber;
}
06-03
708
01-21
1356
02-27
1318
12-20
2568