这篇“Redis唯一ID生成器如何实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Redis唯一ID生成器如何实现”文章吧。
ID的组成部分:
符号位:1bit,永远为0
时间戳:31bit,以秒为单位,可以使用69年
序列号:32bit,秒内的计数器,支持每秒产生2^32个不同ID
生成代码:
public class RedisIdWorker {
/**
* 开始时间戳
*/
private static final long BEGIN_TIMESTAMP = 1640995200L;
/**
* 序列号的位数
*/
private static final int COUNT_BITS = 32;
private StringRedisTemplate stringRedisTemplate;
//构造方法形式注入
public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
public long nextId(String keyPrefix){
//1. 生成时间戳
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;
//2.生成序列号
// 2.1 获取当前日期,精确到天
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);
//3.拼接并返回
return timestamp << COUNT_BITS | count;
}
}
PS:Redis实现全局唯一id生成
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
/**
* 描述:
* 唯一ID生成器
* @author jimmy
* @create 2020-11-06 16:06
*/
@Component
public class GenerateIDUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* 生成每天的初始Id
* @param key
* @return
*/ public String initPrimaryId(String key) {
Assert.hasLength(key, "hashName不能为空");
String hashCol = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
//自定义编号规则
String hashColVal = hashCol + "00001";
// redisTemplate.opsForHash().putIfAbsent(hashName, hashCol, hashColVal);
Long expiresTime = getSecondsNextEarlyMorning();
redisTemplate.opsForValue().set(key, Long.valueOf(hashColVal), expiresTime, TimeUnit.SECONDS);
return hashColVal;
}
/**
* 获取分布式Id
* @param key
* @return
*/
public String getPrimaryId(String key) {
String id = "";
if(redisTemplate.hasKey(key)){
// redisTemplate.opsForValue().get(key);
// redisTemplate.delete(key);
id = String.valueOf(redisTemplate.opsForValue().increment(key, 1));
} else {
id = initPrimaryId(key);
}
return id;
}
/**
* 判断当前时间距离第二天凌晨的秒数
* @return 返回值单位为[s:秒]
*/
public Long getSecondsNextEarlyMorning() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
}
}
以上就是关于“Redis唯一ID生成器如何实现”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。