当前位置:网站首页>基於Redis的分布式ID生成器
基於Redis的分布式ID生成器
2022-07-06 12:12:00 【阿杆.】
基於Redis的分布式ID生成器
ID自增策略
- 每天一個key,方便統計訂單量
- ID構造是 時間戳 + 計數器
ID的組成部分
- 符號比特:1bit,永遠為0
- 時間戳:31bit,以秒為單比特,從2022年1月開始計數,可以使用68年,也可以根據需求,修改為每分鐘、每小時或每天的計數器,可以增大可用時間。
- 序列號:32bit,每天的計數器,支持每天產生2^32個不同ID,也可以根據需求,修改為每小時、每分鐘或每秒的計數器,但需要和時間戳相配合,可以增大ID數量。
這裏是以秒為單比特的時間戳和以天為單比特的序列化計數器組成的ID生成器。
package cn.sticki.common.redis.utils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
/** * @author 阿杆 * @version 1.0 * @date 2022/6/20 20:29 */
@Component
public class RedisIdGenerator {
/** * 開始時間戳 */
private static final long BEGIN_TIMESTAMP = 1640995200L;
/** * 序列號的比特數 */
private static final int COUNT_BITS = 32;
private final RedisTemplate<String, Long> redisTemplate;
public RedisIdGenerator(RedisTemplate<String, Long> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@SuppressWarnings("ConstantConditions")
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"));
// 2.2 獲取redis自增長值
long increment = redisTemplate.opsForValue().increment("id:" + keyPrefix + ":" + date);
// 3. 拼接並返回
return increment << COUNT_BITS | timestamp;
}
}
其他全局唯一ID生成策略
- UUID
- Redis自增
- snowflake算法
- 數據庫自增
边栏推荐
- Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
- 荣耀Magic 3Pro 充电架构分析
- GCC compilation options
- I2C bus timing explanation
- 小天才电话手表 Z3工作原理
- Raspberry pie tap switch button to use
- FreeRTOS 任务函数里面的死循环
- Cannot change version of project facet Dynamic Web Module to 2.3.
- Pytorch实现简单线性回归Demo
- Vscode basic configuration
猜你喜欢
随机推荐
OPPO VOOC快充电路和协议
Bubble sort [C language]
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
VSCode基础配置
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
【ESP32学习-1】Arduino ESP32开发环境搭建
map文件粗略分析
Machine learning -- linear regression (sklearn)
Basic use of pytest
锂电池基础知识
Arduino gets the length of the array
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Arduino uno R3 register writing method (1) -- pin level state change
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
1081 rational sum (20 points) points add up to total points
Working principle of genius telephone watch Z3
RT thread API reference manual
vim命令行笔记
ES6语法总结--下篇(进阶篇 ES6~ES11)
Variable star user module