Redis唯一ID生成器的實(shí)現(xiàn)
發(fā)布日期:2022-07-15 19:33 | 文章來源:源碼之家
ID的組成部分:
- 符號(hào)位:1bit,永遠(yuǎn)為0
- 時(shí)間戳:31bit,以秒為單位,可以使用69年
- 序列號(hào):32bit,秒內(nèi)的計(jì)數(shù)器,支持每秒產(chǎn)生2^32個(gè)不同ID
生成代碼:
public class RedisIdWorker { ? ? /** ? ? ?* 開始時(shí)間戳 ? ? ?*/ ? ? private static final long BEGIN_TIMESTAMP = 1640995200L; ? ? /** ? ? ?* 序列號(hào)的位數(shù) ? ? ?*/ ? ? private static final int COUNT_BITS = 32; ? ? private StringRedisTemplate stringRedisTemplate; ?? ??? ?//構(gòu)造方法形式注入 ? ? public RedisIdWorker(StringRedisTemplate stringRedisTemplate) { ? ? ? ? this.stringRedisTemplate = stringRedisTemplate; ? ? } ? ? public long nextId(String keyPrefix){ ? ? ? ? //1. 生成時(shí)間戳 ? ? ? ? LocalDateTime now = LocalDateTime.now(); ? ? ? ? long nowSecond = now.toEpochSecond(ZoneOffset.UTC); ? ? ? ? long timestamp = nowSecond - BEGIN_TIMESTAMP; ? ? ? ? //2.生成序列號(hào) ? ? ? ? // 2.1 獲取當(dāng)前日期,精確到天 ? ? ? ? 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實(shí)現(xiàn)全局唯一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")); ? ? ? ? //自定義編號(hào)規(guī)則 ? ? ? ? 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; ? ? } ? ? /** ? ? ?* 判斷當(dāng)前時(shí)間距離第二天凌晨的秒數(shù) ? ? ?* @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; ? ? } }
到此這篇關(guān)于Redis唯一ID生成器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Redis唯一ID生成器內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章