当前位置:网站首页>Redis based distributed ID generator
Redis based distributed ID generator
2022-07-06 12:13:00 【A pole】
be based on Redis Distributed ID generator
ID Self increasing strategy
- One a day key, It is convenient to count the order quantity
- ID The structure is Time stamp + Counter
ID Component part
- Sign bit :1bit, For ever 0
- Time stamp :31bit, In seconds , from 2022 year 1 The month begins to count , have access to 68 year , Can also be based on demand , Change to every minute 、 Hourly or daily counter , Available time can be increased .
- Serial number :32bit, Daily counter , Support daily production 2^32 Different ID, Can also be based on demand , Change to hourly 、 Counter per minute or per second , But it needs to match with the timestamp , Can increase ID Number .
Here is a timestamp in seconds and a serialization counter in days ID generator .
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 A pole * @version 1.0 * @date 2022/6/20 20:29 */
@Component
public class RedisIdGenerator {
/** * Start timestamp */
private static final long BEGIN_TIMESTAMP = 1640995200L;
/** * Number of digits of serial number */
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. Generate timestamps
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;
// 2. Generate serial number
// 2.1 Get current date , Accurate to the sky
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
// 2.2 obtain redis Self growth value
long increment = redisTemplate.opsForValue().increment("id:" + keyPrefix + ":" + date);
// 3. Splice and return
return increment << COUNT_BITS | timestamp;
}
}
Other globally unique ID Generation strategy
- UUID
- Redis Self increasing
- snowflake Algorithm
- Database autoincrement
边栏推荐
- RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
- ES6 grammar summary -- Part I (basic)
- JS object and event learning notes
- VSCode基础配置
- JS 函数提升和var变量的声明提升
- Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
- vim命令行笔记
- OSPF message details - LSA overview
- Imgcat usage experience
- Embedded startup process
猜你喜欢
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
Basic use of pytest
机器学习--线性回归(sklearn)
【ESP32学习-2】esp32地址映射
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
ESP学习问题记录
R & D thinking 01 ----- classic of embedded intelligent product development process
Types de variables JS et transformations de type communes
高通&MTK&麒麟 手机平台USB3.0方案对比
MySQL realizes read-write separation
随机推荐
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
level16
1081 rational sum (20 points) points add up to total points
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
Oppo vooc fast charging circuit and protocol
I2C总线时序详解
Pytoch temperature prediction
C语言函数之可变参数原理:va_start、va_arg及va_end
Arduino gets the length of the array
MySQL realizes read-write separation
js 变量作用域和函数的学习笔记
物联网系统框架学习
Characteristics, task status and startup of UCOS III
ES6 grammar summary -- Part I (basic)
There are three iPhone se 2022 models in the Eurasian Economic Commission database
Several declarations about pointers [C language]
JS 函数提升和var变量的声明提升
VIM command line notes
E-commerce data analysis -- salary prediction (linear regression)