欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用redis如何生成自增序列號碼

 更新時間:2022年11月23日 08:26:23   作者:飯團小哥哥iop  
這篇文章主要介紹了使用redis如何生成自增序列號碼,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

redis生成自增序列號碼

導入依賴

<!--redis-->
<dependency>
?? ?<groupId>org.springframework.boot</groupId>
?? ??? ?<artifactId>spring-boot-starter-data-redis</artifactId>
?? ??? ?<exclusion>
?? ?<groupId>org.springframework.boot</groupId>
?? ?<artifactId>spring-boot-starter-logging</artifactId>
?? ?</exclusion>
?? ?</exclusions>
</dependency>

yml 配置

? redis:
? ? ? database: 10
? ? ? host: localhost
? ? ? port: 7701
? ? ? password: root

工具方法

package com.sd.sdactivity.controller;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**

?* 使用redis生成自增序列

?*/
@RestController
public class testController {
@Autowired
private StringRedisTemplate redisTemplate;
? ? /**
? ? ?* 自增序列號
? ? ?* @param prefix ?前綴
? ? ?* @param numLength ?要生成多少位數(shù)字
? ? ?* @return
? ? ?*/?
@RequestMapping("/SeqGenerator")
? ? public String SeqGenerator(String prefix,int numLength){
? ? ? ? String upperCode =""; ??
? ?Long size=redisTemplate.opsForList().size(prefix);//查找 prefix 的key值的數(shù)據(jù)長度
? ? ? ? if(size>0){//有數(shù)據(jù)
List leve =redisTemplate.opsForList().range(prefix,0,-1);//獲取該key下面的所有值(-1 所有的值,;1下一個值
? upperCode=leve.get(leve.size()-1).toString();//返回最后一個值
? ? ? ? }
? ? ? ? ? ?String returnCode="";
? ? ? ? ? int Suffix; ?//后綴數(shù)字 if (!StringUtil.isNullOrEmpty(upperCode)){ //有數(shù)據(jù)
? ? ? ? ? ? String sequence =upperCode.substring(prefix.length());//截取前綴開始的后面的數(shù)字
? ? ? ? ? ? Suffix=Integer.parseInt(sequence);?
? ? ? ? ? ? ?Suffix++;//最后的序號加一
? ? ? ? ?}else{
? ? ? ? ? ? Suffix=1;//沒有數(shù)據(jù)
? ? ? ? }
? ? ? ? ? ?returnCode=prefix+String.format("%0"+numLength+"d",Suffix);//后綴不夠numLength長,前面補充0
? ? ? ? redisTemplate.opsForList().rightPush(prefix,returnCode);//存入Redis
? ? ? ? System.out.println(returnCode+"%%%");
? ? ? ? return ?returnCode;
? }
}

測試

2020-05-13 11:43:31.230  INFO 39268 --- [           main] c.f.SpringbootRedisApplicationTests      : seq00000002

redis生成唯一編號

在系統(tǒng)開發(fā)中,保證數(shù)據(jù)的唯一性是至關(guān)重要的一件事,目前開發(fā)中常用的方式有使用數(shù)據(jù)庫的自增序列、UUID、時間戳或者時間戳+隨機數(shù)等。

因為Redis是原子性的,所以我們可以用redis生成一個唯一的號碼,記錄一個編號,我們用這個編號可以配合時間戳生成一個唯一的key

接下來是上代碼

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
/**
 * 使用redis生成唯一key
 */
@Service("reportNumberUtils")
public class ReportNumberUtils {
 
    private static Logger logger = LoggerFactory.getLogger(ReportNumberUtils.class);
 
    @Resource
    private RedisTemplate redisTemplate;
 
    //傳入制定的key和prefix
    public String getSeqNo(String key, String prefix)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        //設置過期時間,這里設置為當天的23:59:59
        Date expireDate = calendar.getTime();
        //返回當前redis中的key的最大值
        Long seq = generate(redisTemplate, key, expireDate);
        //獲取當天的日期,格式為yyyyMMdd
        String date = new SimpleDateFormat("yyyyMMdd").format(expireDate);
        //生成八為的序列號,如果seq不夠八位,seq前面補0,
        //如果seq位數(shù)超過了八位,那么無需補0直接返回當前的seq
        String sequence = StringUtils.leftPad(seq.toString(), 8, "0");
        if (prefix == null)
        {
            prefix = "";
        }
        //拼接業(yè)務編號
        String seqNo = prefix + date + sequence;
        logger.info("KEY:{}, 序列號生成:{}, 過期時間:{}", key, seqNo, String.format("%tF %tT ", expireDate, expireDate));
        return seqNo;
    }
 
    /**
     * @param key
     * @param expireTime <i>過期時間</i>
     * @return
     */
    public static long generate(RedisTemplate<?,?> redisTemplate,String key,Date expireTime) {
        //RedisAtomicLong為原子類,根據(jù)傳入的key和redis鏈接工廠創(chuàng)建原子類
        RedisAtomicLong counter = new RedisAtomicLong(key,redisTemplate.getConnectionFactory());
        //設置過期時間
        counter.expireAt(expireTime);
        //返回redis中key的值,內(nèi)部實現(xiàn)下面詳細說明
        return counter.incrementAndGet();
    }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Redis從單點到集群部署模式(單機模式?主從模式?哨兵模式)

    Redis從單點到集群部署模式(單機模式?主從模式?哨兵模式)

    這篇文章主要為大家介紹了Redis從單點集群部署模式(單機模式?主從模式?哨兵模式)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • 基于Redis過期事件實現(xiàn)訂單超時取消

    基于Redis過期事件實現(xiàn)訂單超時取消

    這篇文章主要介紹了基于Redis過期事件實現(xiàn)訂單超時取消,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • redis哈希類型_動力節(jié)點Java學院整理

    redis哈希類型_動力節(jié)點Java學院整理

    這篇文章主要介紹了redis哈希類型的常用方法及原理淺析,感興趣的朋友一起看看吧
    2017-08-08
  • 淺談redis采用不同內(nèi)存分配器tcmalloc和jemalloc

    淺談redis采用不同內(nèi)存分配器tcmalloc和jemalloc

    下面小編就為大家?guī)硪黄獪\談redis采用不同內(nèi)存分配器tcmalloc和jemalloc。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • Redis的字符串是如何實現(xiàn)的

    Redis的字符串是如何實現(xiàn)的

    本文主要介紹了Redis的字符串是如何實現(xiàn)的,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 一文詳解Redis為什么一定要設置密碼原理

    一文詳解Redis為什么一定要設置密碼原理

    這篇文章主要為大家介紹了Redis為什么一定要設置密碼原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 用Redis實現(xiàn)微博關(guān)注關(guān)系

    用Redis實現(xiàn)微博關(guān)注關(guān)系

    在微博中,每一個用戶都會有一個關(guān)注列表,一個粉絲列表。用戶可以查看自己的關(guān)注,粉絲列表,也可以查看別人的關(guān)注,粉絲列表。并且,要展示列表里每個人與當前查看者的關(guān)注狀態(tài)。
    2015-09-09
  • 為什么RedisCluster設計成16384個槽

    為什么RedisCluster設計成16384個槽

    本文主要介紹了為什么RedisCluster設計成16384個槽,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 一篇文章帶你徹底搞懂Redis?事務

    一篇文章帶你徹底搞懂Redis?事務

    這篇文章主要介紹了一篇文章帶你徹底搞懂Redis?事務的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Redisson實現(xiàn)分布式鎖、鎖續(xù)約的案例

    Redisson實現(xiàn)分布式鎖、鎖續(xù)約的案例

    這篇文章主要介紹了Redisson如何實現(xiàn)分布式鎖、鎖續(xù)約,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03

最新評論