当前位置:网站首页>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
边栏推荐
- 【ESP32学习-2】esp32地址映射
- List and set
- Unit test - unittest framework
- Arduino gets the length of the array
- arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
- [golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
- Cannot change version of project facet Dynamic Web Module to 2.3.
- The dolphin scheduler remotely executes shell scripts through the expect command
- Pytorch four commonly used optimizer tests
- 列表的使用
猜你喜欢
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
E-commerce data analysis -- salary prediction (linear regression)
Detailed explanation of Union [C language]
Priority inversion and deadlock
ES6语法总结--上篇(基础篇)
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
荣耀Magic 3Pro 充电架构分析
Basic operations of databases and tables ----- modifying data tables
Cannot change version of project facet Dynamic Web Module to 2.3.
随机推荐
Unit test - unittest framework
Embedded startup process
列表的使用
[template] KMP string matching
arduino JSON数据信息解析
JS regular expression basic knowledge learning
电商数据分析--用户行为分析
Variable star user module
List and set
[esp32 learning-1] construction of Arduino esp32 development environment
A possible cause and solution of "stuck" main thread of RT thread
程序员老鸟都会搞错的问题 C语言基础 指针和数组
Mysqldump error1066 error solution
E-commerce data analysis -- User Behavior Analysis
Analysis of charging architecture of glory magic 3pro
AMBA、AHB、APB、AXI的理解
关键字 inline (内联函数)用法解析【C语言】
ES6语法总结--下篇(进阶篇 ES6~ES11)
Symbolic representation of functions in deep learning papers
Several declarations about pointers [C language]