当前位置:网站首页>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
边栏推荐
- Use of lists
- Programmers can make mistakes. Basic pointers and arrays of C language
- 共用体(union)详解【C语言】
- Basic operations of databases and tables ----- classification of data
- Arduino uno R3 register writing method (1) -- pin level state change
- Important methods of array and string
- E-commerce data analysis -- User Behavior Analysis
- VIM command line notes
- ESP8266使用arduino连接阿里云物联网
- Basic operations of databases and tables ----- view data tables
猜你喜欢

电商数据分析--用户行为分析
![[Red Treasure Book Notes simplified version] Chapter 12 BOM](/img/ff/0ad410b5b556c0e16a4076a2a0577b.jpg)
[Red Treasure Book Notes simplified version] Chapter 12 BOM

共用体(union)详解【C语言】

Kconfig Kbuild

Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries

A possible cause and solution of "stuck" main thread of RT thread

Gallery's image browsing and component learning

Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect

Principle and implementation of MySQL master-slave replication

Page performance optimization of video scene
随机推荐
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
JS变量类型以及常用类型转换
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Vscode basic configuration
Pytorch-温度预测
ToggleButton实现一个开关灯的效果
Page performance optimization of video scene
level16
Togglebutton realizes the effect of switching lights
Types de variables JS et transformations de type communes
电商数据分析--薪资预测(线性回归)
VSCode基础配置
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
ESP learning problem record
GCC compilation options
FreeRTOS 任务函数里面的死循环
Arm pc=pc+8 is the most understandable explanation
Common properties of location
Basic knowledge of lithium battery
Understanding of AMBA, AHB, APB and Axi