最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Redis唯一ID生成器的實現(xiàn)

 更新時間:2022年07月05日 16:28:36   作者:Chen陳c  
本文主要介紹了Redis唯一ID生成器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

ID的組成部分:

  • 符號位:1bit,永遠(yuǎn)為0
  • 時間戳:31bit,以秒為單位,可以使用69年
  • 序列號:32bit,秒內(nèi)的計數(shù)器,支持每秒產(chǎn)生2^32個不同ID

生成代碼:

public class RedisIdWorker {

? ? /**
? ? ?* 開始時間戳
? ? ?*/
? ? private static final long BEGIN_TIMESTAMP = 1640995200L;
? ? /**
? ? ?* 序列號的位數(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. 生成時間戳
? ? ? ? LocalDateTime now = LocalDateTime.now();
? ? ? ? long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
? ? ? ? long timestamp = nowSecond - BEGIN_TIMESTAMP;
? ? ? ? //2.生成序列號
? ? ? ? // 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實現(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"));
? ? ? ? //自定義編號規(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ù)
? ? ?* @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生成器的實現(xiàn)的文章就介紹到這了,更多相關(guān)Redis唯一ID生成器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 為何Redis使用跳表而非紅黑樹實現(xiàn)SortedSet

    為何Redis使用跳表而非紅黑樹實現(xiàn)SortedSet

    本篇文章主要介紹了為何Redis使用跳表而非紅黑樹實現(xiàn)SortedSet,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 基于redis樂觀鎖實現(xiàn)并發(fā)排隊

    基于redis樂觀鎖實現(xiàn)并發(fā)排隊

    這篇文章主要介紹了基于redis樂觀鎖實現(xiàn)并發(fā)排隊的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Redis中ziplist壓縮列表的實現(xiàn)

    Redis中ziplist壓縮列表的實現(xiàn)

    本文主要介紹了Redis中ziplist壓縮列表的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Redis的9種數(shù)據(jù)類型用法解讀

    Redis的9種數(shù)據(jù)類型用法解讀

    這篇文章主要介紹了Redis的9種數(shù)據(jù)類型用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • redis服務(wù)啟動與停止方式

    redis服務(wù)啟動與停止方式

    這篇文章主要介紹了redis服務(wù)啟動與停止方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Redis消息隊列實現(xiàn)異步秒殺功能

    Redis消息隊列實現(xiàn)異步秒殺功能

    在高并發(fā)場景下,為了提高秒殺業(yè)務(wù)的性能,可將部分工作交給 Redis 處理,并通過異步方式執(zhí)行,Redis 提供了多種數(shù)據(jù)結(jié)構(gòu)來實現(xiàn)消息隊列,總結(jié)三種,本文詳細(xì)介紹Redis消息隊列實現(xiàn)異步秒殺功能,感興趣的朋友一起看看吧
    2025-04-04
  • redis快照模式_動力節(jié)點Java學(xué)院整理

    redis快照模式_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了redis快照模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Redis內(nèi)存碎片原理深入分析

    Redis內(nèi)存碎片原理深入分析

    這篇文章主要為大家介紹了Redis內(nèi)存碎片原理深入分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Redis簡介

    Redis簡介

    Redis是一個開源,高級的鍵值存儲和一個適用的解決方案,用于構(gòu)建高性能,可擴展的Web應(yīng)用程序。關(guān)于redis的相關(guān)知識大家可以通過本教程學(xué)習(xí)
    2017-05-05
  • 在不重啟的情況下熱更新Redis集群密碼的流程步驟

    在不重啟的情況下熱更新Redis集群密碼的流程步驟

    當(dāng)我們需要在運行中的 Redis 集群中修改密碼時,可以通過 Redis 的配置命令 CONFIG SET 實現(xiàn)即時修改,并使用 CONFIG REWRITE 將更改持久化到配置文件中,在本文中,我們將詳細(xì)介紹如何安全地更新你的 Redis 集群密碼,需要的朋友可以參考下
    2024-05-05

最新評論

临夏市| 东丽区| 祁门县| 高阳县| 西充县| 景德镇市| 哈密市| 萨嘎县| 修武县| 西昌市| 肥东县| 雷波县| 咸宁市| 峨眉山市| 沧源| 林口县| 尚义县| 观塘区| 杂多县| 浦北县| 通许县| 湾仔区| 全椒县| 甘泉县| 嘉黎县| 绥宁县| 沭阳县| 西乡县| 铁岭市| 邵阳县| 石楼县| 夏邑县| 德格县| 洞头县| 高青县| 大渡口区| 永济市| 右玉县| 焦作市| 邛崃市| 搜索|