当前位置:网站首页>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
边栏推荐
- Arm pc=pc+8 is the most understandable explanation
- Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
- Analysis of charging architecture of glory magic 3pro
- Use of lists
- js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- Basic knowledge of lithium battery
- Arduino get random number
- 电商数据分析--薪资预测(线性回归)
- level16
- 几个关于指针的声明【C语言】
猜你喜欢

Reno7 60W super flash charging architecture

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

Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer

ES6语法总结--下篇(进阶篇 ES6~ES11)
![[esp32 learning-2] esp32 address mapping](/img/ee/c4aa0f7aed7543bb6807d7fd852c88.png)
[esp32 learning-2] esp32 address mapping

Oppo vooc fast charging circuit and protocol

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

arduino JSON数据信息解析

Amba, ahb, APB, Axi Understanding

RT thread API reference manual
随机推荐
Unit test - unittest framework
Pat 1097 duplication on a linked list (25 points)
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
【ESP32学习-2】esp32地址映射
Detailed explanation of Union [C language]
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
open-mmlab labelImg mmdetection
E-commerce data analysis -- salary prediction (linear regression)
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Keyword inline (inline function) usage analysis [C language]
电商数据分析--薪资预测(线性回归)
选择法排序与冒泡法排序【C语言】
Bubble sort [C language]
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
Reno7 60W超级闪充充电架构
Dead loop in FreeRTOS task function
Raspberry pie tap switch button to use
JS變量類型以及常用類型轉換
Arduino JSON data information parsing