当前位置:网站首页>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
边栏推荐
- Characteristics, task status and startup of UCOS III
- RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
- ES6 grammar summary -- Part I (basic)
- Machine learning -- linear regression (sklearn)
- JS变量类型以及常用类型转换
- Basic use of pytest
- js 变量作用域和函数的学习笔记
- inline详细讲解【C语言】
- Use of lists
- ESP8266通过arduino IED连接巴法云(TCP创客云)
猜你喜欢
PyTorch四种常用优化器测试
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
Amba, ahb, APB, Axi Understanding
高通&MTK&麒麟 手機平臺USB3.0方案對比
机器学习--线性回归(sklearn)
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
Analysis of charging architecture of glory magic 3pro
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
Vscode basic configuration
随机推荐
AMBA、AHB、APB、AXI的理解
MySQL占用内存过大解决方案
电商数据分析--薪资预测(线性回归)
機器學習--線性回歸(sklearn)
ESP8266通过arduino IED连接巴法云(TCP创客云)
Arduino get random number
level16
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
Arm pc=pc+8 is the most understandable explanation
Reno7 60W超级闪充充电架构
Arduino JSON data information parsing
map文件粗略分析
OPPO VOOC快充电路和协议
Oppo vooc fast charging circuit and protocol
Pytoch implements simple linear regression demo
Bubble sort [C language]
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
Pat 1097 duplication on a linked list (25 points)
FreeRTOS 任务函数里面的死循环
Pytorch实现简单线性回归Demo