先使用System.currentTimeMillis()获取时间戳 再通过redis存自增key结合
@Resource
private StringRedisTemplate stringRedisTemplate;
String no = stringRedisTemplate.opsForValue().get("id_");
if (StringUtils.isEmpty(no)) {
stringRedisTemplate.opsForValue().set("id_", "1", 60 * 60 * 24, TimeUnit.SECONDS);
System.out.println(System.currentTimeMillis()+1);
} else {
stringRedisTemplate.boundValueOps("id_").increment(1);
System.out.println(System.currentTimeMillis()+no);
}
本文介绍了一种使用时间戳和Redis自增键来生成不重复ID的方法,通过结合System.currentTimeMillis()获取时间戳和Redis的原子自增操作,确保ID的唯一性。
779

被折叠的 条评论
为什么被折叠?



