首页 > 数据库 >Redis实现全局唯一id

Redis实现全局唯一id

时间:2023-01-26 22:23:04浏览次数:39  
标签:format Redis springframework final org import 全局 id

全局Id生成器

为了增加id的安全性,我们可以不直接使用redis自增生成的数值,可以拼接一些其他的数值

id的组成部分

  • 符号位:1bit,永远为0
  • 时间戳:31bit,以秒为单位,可以使用69年
  • 序列号:32bit,秒内的计数器,支持每秒产生2^32个不同的id

代码具体实现

点击查看代码
package com.waa.gulimall.order.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class RedisIdWoker {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static  final    long BEGIN_TIMESTAMP = 1674220484L;
private static  final  int COUNT_BIT = 32;
public long nextId(String keyPrefix){
    //1.生成时间戳
    final LocalDateTime localDateTime = LocalDateTime.now();
    final long nowTime = localDateTime.toEpochSecond(ZoneOffset.UTC);
    long timestamp = nowTime - BEGIN_TIMESTAMP;
    final String format = localDateTime.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
    //2.生成序列号
    final Long increment = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + format);
    return timestamp << COUNT_BIT | increment;
}

    public static void main(String[] args) {
        final LocalDateTime now = LocalDateTime.now();
        System.out.println(now.toEpochSecond(ZoneOffset.UTC));
    }

}

标签:format,Redis,springframework,final,org,import,全局,id
From: https://www.cnblogs.com/waacode/p/17068331.html

相关文章

  • m基于可见光通信系统的RFID接口过程以及ALOHA防碰撞算法的matlab仿真
    1.算法描述       射频识别技术(RadioFrequencyIdentification,RFID)是一种非接触式自动识别技术,与传统的识别方式相比,它无需直接接触、无需光学可视、无需人工干......
  • WPF的APP生命周期以及全局异常捕获
    WPF的APP生命周期以及全局异常捕获APP生命周期wpf项目目录中有一个App.xaml.cs文件,该文件中App是一个partical类,与之对应的另一partical部分在App.g.i.cs文件中,该文件是......
  • Cesium 椭球大地测量EllipsoidGeodesic(十三)
    首先发现一个问题,这段代码会报错,原因是"DeveloperError:Expectedvaluetobegreaterthanorequalto0.0125,actualvaluewas0,不知道为什么经度不能为0newCesium......
  • WPF的APP生命周期以及全局异常捕获
    WPF的APP生命周期以及全局异常捕获APP生命周期wpf项目目录中有一个App.xaml.cs文件,该文件中App是一个partical类,与之对应的另一partical部分在App.g.i.cs文件中,该文件是在......
  • 【推荐系统论文精读系列】(十)--Wide&Deep Learning for Recommender Systems
    文章目录​​一、摘要​​​​二、介绍​​​​三、推荐系统综述​​​​四、Wide&Deep学习​​​​4.1Wide部分​​​​4.2Deep部分​​​​4.3联合训练Wide&DeepMode......
  • 解决ios h5下video点击播放自动全屏视频问题
    部署上线webrtc项目发现iosh5video默认情况下设置静音实现自动播放后手动点击播放时候自动全屏播放了,交互不好,如图:打印查看video所有自带的dom属性constvideoDo......
  • Redis学习笔记
    1.简介概述Redis是基于内存的key-value数据库基于内存存储,独写性能高,所有Redis很多时候会作为缓存来使用适合存储热点数据:短时间有大量用户访问MySQL则是存在磁盘......
  • go 使用redis
       import(redigo"github.com/gomodule/redigo/redis")  funcNewPool()*redigo.Pool{//return&redigo.Pool{//MaxIdle:3,//IdleTimeou......
  • Android时间与服务器同步方案
    前言在部分场景里,应用对时间的要求比较严苛,比如金融类app,股票类、期货等,对交易区间的判断是非常重要的。这就需要客户端的时间与服务器时间或者是世界标准时间同步,而不能简......
  • 微服务 Spring Boot 整合 Redis BitMap 实现 签到与统计
    文章目录​​⛄引言​​​​一、RedisBitMap基本用法​​​​⛅BitMap基本语法、指令​​​​⚡使用BitMap完成功能实现​​​​二、SpringBoot整合Redis实现签到......