欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • redis遠(yuǎn)程連接不上的解決辦法

    redis遠(yuǎn)程連接不上的解決辦法

    本文主要介紹了redis遠(yuǎn)程連接不上的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Redis 持久化 RDB 與 AOF的執(zhí)行過程

    Redis 持久化 RDB 與 AOF的執(zhí)行過程

    本文給大家記錄Redis 持久化RDB 與 AOF的執(zhí)行過程與配置,通過內(nèi)部觸發(fā) RDB 場(chǎng)景分析Redis 持久化 RDB的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • redis服務(wù)如何啟動(dòng)

    redis服務(wù)如何啟動(dòng)

    這篇文章主要介紹了redis服務(wù)如何啟動(dòng)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Redis不使用 keys 命令獲取鍵值信息的方法

    Redis不使用 keys 命令獲取鍵值信息的方法

    這篇文章主要介紹了Redis 不使用 keys 命令獲取鍵值信息的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-08-08
  • 深入了解Redis的性能

    深入了解Redis的性能

    這篇文章主要介紹了Redis的性能,作者通過Visual Studio上的C#程序?qū)ζ溥M(jìn)行了分析,需要的朋友可以參考下
    2015-06-06
  • 一起raid數(shù)據(jù)恢復(fù)及回遷成功的案例

    一起raid數(shù)據(jù)恢復(fù)及回遷成功的案例

    這篇文章主要介紹了一起raid數(shù)據(jù)恢復(fù)及回遷成功的案例,需要的朋友可以參考下
    2017-04-04
  • redis中刪除操作命令

    redis中刪除操作命令

    這篇文章主要介紹了redis中刪除操作命令,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 關(guān)于Redis解決Session共享問題

    關(guān)于Redis解決Session共享問題

    這篇文章主要介紹了Redis解決Session共享問題,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Redis內(nèi)存滿了的幾種原因和最佳解決方案

    Redis內(nèi)存滿了的幾種原因和最佳解決方案

    Redis是一款高性能的內(nèi)存數(shù)據(jù)庫(kù),被廣泛應(yīng)用于緩存、消息隊(duì)列、計(jì)數(shù)器等場(chǎng)景,然而,由于Redis是基于內(nèi)存的數(shù)據(jù)庫(kù),當(dāng)數(shù)據(jù)量過大或者配置不合理時(shí),就有可能導(dǎo)致Redis的內(nèi)存滿,本文將介紹Redis內(nèi)存滿的幾種原因,并提供相應(yīng)的解決方案,需要的朋友可以參考下
    2023-11-11
  • redis底層數(shù)據(jù)結(jié)構(gòu)之ziplist實(shí)現(xiàn)詳解

    redis底層數(shù)據(jù)結(jié)構(gòu)之ziplist實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了redis底層數(shù)據(jù)結(jié)構(gòu)之ziplist實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12

最新評(píng)論