当前位置:网站首页>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
边栏推荐
- arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
- ARM PC=PC+8 最便于理解的阐述
- arduino获取数组的长度
- OPPO VOOC快充电路和协议
- Characteristics, task status and startup of UCOS III
- 高通&MTK&麒麟 手機平臺USB3.0方案對比
- ESP8266通过arduino IED连接巴法云(TCP创客云)
- Pytorch four commonly used optimizer tests
- Arduino get random number
- Selective sorting and bubble sorting [C language]
猜你喜欢

物联网系统框架学习

Mysql database interview questions

Kconfig Kbuild

JS正则表达式基础知识学习

Pat 1097 duplication on a linked list (25 points)

Time slice polling scheduling of RT thread threads

Basic operations of databases and tables ----- view data tables

E-commerce data analysis -- User Behavior Analysis

Missing value filling in data analysis (focus on multiple interpolation method, miseforest)

Arduino uno R3 register writing method (1) -- pin level state change
随机推荐
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
History object
选择法排序与冒泡法排序【C语言】
ARM PC=PC+8 最便于理解的阐述
Vscode basic configuration
优先级反转与死锁
【ESP32学习-2】esp32地址映射
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
C语言,log打印文件名、函数名、行号、日期时间
E-commerce data analysis -- salary prediction (linear regression)
STM32 how to locate the code segment that causes hard fault
imgcat使用心得
Important methods of array and string
MySQL占用内存过大解决方案
程序员老鸟都会搞错的问题 C语言基础 指针和数组
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
JS数组常用方法的分类、理解和运用
Keyword inline (inline function) usage analysis [C language]
GCC compilation options
Several declarations about pointers [C language]