当前位置:网站首页>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
边栏推荐
猜你喜欢

JS变量类型以及常用类型转换

open-mmlab labelImg mmdetection

Cannot change version of project facet Dynamic Web Module to 2.3.

js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。

【ESP32学习-1】Arduino ESP32开发环境搭建

ESP8266使用arduino连接阿里云物联网

MySQL realizes read-write separation

Types de variables JS et transformations de type communes

Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0

ESP learning problem record
随机推荐
Machine learning -- decision tree (sklearn)
Vscode basic configuration
Variable star user module
Togglebutton realizes the effect of switching lights
List and set
Reno7 60W超级闪充充电架构
Several declarations about pointers [C language]
Reading notes of difficult career creation
列表的使用
Principle and implementation of MySQL master-slave replication
level16
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
Programmers can make mistakes. Basic pointers and arrays of C language
Inline detailed explanation [C language]
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
关键字 inline (内联函数)用法解析【C语言】
ESP8266使用arduino连接阿里云物联网
Rough analysis of map file
C语言,log打印文件名、函数名、行号、日期时间
[Red Treasure Book Notes simplified version] Chapter 12 BOM