当前位置:网站首页>Générateur d'identification distribué basé sur redis
Générateur d'identification distribué basé sur redis
2022-07-06 12:12:00 【Un bâton.】
Basé surRedisDistribution deIDGénérateur
IDStratégie d'auto - augmentation
- Un par jourkey,Statistiques pratiques sur le volume des commandes
- IDLa construction est Horodatage + Compteur
IDLes composantes de
- Bits de symbole:1bit,Pour toujours0
- Horodatage:31bit,En secondes,De2022Année1Début du mois,Peut être utilisé68Année,Ou sur demande,Modifier pour chaque minute、Compteurs horaires ou quotidiens,Peut augmenter le temps disponible.
- Numéro de série:32bit,Compteurs quotidiens,Soutenir la production quotidienne2^32C'est différent.ID,Ou sur demande,Modifier à l'heure、Compteurs par minute ou par seconde,Mais il doit correspondre à l'horodatage,Peut être agrandiIDNombre.
Voici un horodatage en secondes et un compteur de sérialisation en joursIDGénérateur.
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 Un poteau. * @version 1.0 * @date 2022/6/20 20:29 */
@Component
public class RedisIdGenerator {
/** * Horodatage de démarrage */
private static final long BEGIN_TIMESTAMP = 1640995200L;
/** * Nombre de chiffres du numéro de série */
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. Générer un TIMESTAMP
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;
// 2. Générer un numéro de série
// 2.1 Obtenir la date actuelle,Au jour le jour
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
// 2.2 Accèsredis Valeur d'auto - croissance
long increment = redisTemplate.opsForValue().increment("id:" + keyPrefix + ":" + date);
// 3. Attelage et retour
return increment << COUNT_BITS | timestamp;
}
}
Autre unique au niveau mondial IDGénérer une politique
- UUID
- RedisAuto - augmentation
- snowflakeAlgorithmes
- Base de données auto - incrémentale
边栏推荐
- STM32 how to locate the code segment that causes hard fault
- Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
- Basic operations of databases and tables ----- modifying data tables
- level16
- MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
- Kconfig Kbuild
- Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries
- arduino获取随机数
- 列表的使用
- 程序员老鸟都会搞错的问题 C语言基础 指针和数组
猜你喜欢
Oppo vooc fast charging circuit and protocol
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
Common properties of location
Implementation scheme of distributed transaction
锂电池基础知识
Detailed explanation of Union [C language]
Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)
ES6 grammar summary -- Part I (basic)
Gallery's image browsing and component learning
随机推荐
Raspberry pie tap switch button to use
Vscode basic configuration
JS正则表达式基础知识学习
Dead loop in FreeRTOS task function
MySQL占用内存过大解决方案
Unit test - unittest framework
ESP8266通过arduino IED连接巴法云(TCP创客云)
VSCode基础配置
Machine learning -- linear regression (sklearn)
gcc 编译选项
Rough analysis of map file
AMBA、AHB、APB、AXI的理解
I2C bus timing explanation
C语言回调函数【C语言】
Inline detailed explanation [C language]
列表的使用
ESP8266通过Arduino IDE连接Onenet云平台(MQTT)
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
荣耀Magic 3Pro 充电架构分析
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题