亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用RedisAtomicLong優(yōu)化性能問題

 更新時間:2022年11月23日 08:35:07   作者:飯團小哥哥iop  
這篇文章主要介紹了使用RedisAtomicLong優(yōu)化性能問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

RedisAtomicLong優(yōu)化性能

在項目中許多過這樣的需求,記錄留做備忘。

需要創(chuàng)建一個遞增序列,這個序列會提供給多個應(yīng)用來使用,這樣就需要保持序列的原子遞增。

RedisAtomicLong

spring-data-redis包中提供的,可以對數(shù)據(jù)中的Long類型進行原子性操作的類,下面是這個類的頭:

/**
?* Atomic long backed by Redis. Uses Redis atomic increment/decrement and watch/multi/exec operations for CAS
?* operations.
?* ?* @see java.util.concurrent.atomic.AtomicLong
?* @author Costin Leau
?* @author Thomas Darimont
?* @author Christoph Strobl
?* @author Mark Paluch
?*/
public class RedisAtomicLong extends Number implements Serializable, BoundKeyOperations<String> {

我們可以看到j(luò)ava.util.concurrent.atomic.AtomicLong,和java自帶的atomic包一樣進行原子性操作,兩者不同的是:

  • AtomicLong只能在一個應(yīng)用中使用
  • RedisAtomicLong可以在所有與Redis有連接的應(yīng)用中使用

開始優(yōu)化

應(yīng)用初始化時創(chuàng)建RedisAtomicLong實例。

?//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jiu.common.redis;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;

public class RedisSequenceFactory {
? ? private static final Logger log = LoggerFactory.getLogger(RedisSequenceFactory.class);
? ? @Autowired
? ? private ObjectRedisTemplate<Integer> redisTemplate;

? ? public RedisSequenceFactory() {
? ? }

? ? public void set(String key, long value) {
? ? ? ? RedisAtomicLong counter = new RedisAtomicLong(key, this.redisTemplate.getConnectionFactory());
? ? ? ? counter.set(value);
? ? }

? ? public long generate(String key, int increment) {
? ? ? ? RedisAtomicLong counter = new RedisAtomicLong(key, this.redisTemplate.getConnectionFactory());
? ? ? ? return counter.addAndGet((long)increment);
? ? }

? ? public List<Long> generateBatch(String key, int increment, int size) {
? ? ? ? RedisAtomicLong counter = new RedisAtomicLong(key, this.redisTemplate.getConnectionFactory());
? ? ? ? long max = counter.addAndGet((long)(increment * size));
? ? ? ? long min = max - (long)(increment * (size - 1));
? ? ? ? List<Long> list = new ArrayList();
? ? ? ? list.add(min);

? ? ? ? for(int i = 1; i < size; ++i) {
? ? ? ? ? ? list.add(min + (long)(increment * i));
? ? ? ? }

? ? ? ? return list;
? ? }

? ? public long queryValue(String key) {
? ? ? ? Integer val = (Integer)this.redisTemplate.get(key);
? ? ? ? return val == null ? 0L : (long)val.intValue();
? ? }
}

測試代碼

public static String tradeIdIncRedisKey = "order:orderid_inc";
? ? ?@Test
? ? public long generateId(){
? ? ? ? return redisSequenceFactory.generate(tradeIdIncRedisKey,1);
? ? }

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

相關(guān)文章

  • SpringBoot中生成二維碼的案例分享

    SpringBoot中生成二維碼的案例分享

    在Spring?Boot項目中整合ZXing庫來生成二維碼是一個常見的需求,zxing是一個功能強大的開源Java庫,專門用于二維碼的生成與解析,它支持Android、iOS、Java等多個平臺,本文小編將給大家分享SpringBoot中生成二維碼的案例,需要的朋友可以參考下
    2024-08-08
  • java線程池合理設(shè)置最大線程數(shù)和核心線程數(shù)方式

    java線程池合理設(shè)置最大線程數(shù)和核心線程數(shù)方式

    這篇文章主要介紹了java線程池合理設(shè)置最大線程數(shù)和核心線程數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java操作solr實現(xiàn)查詢功能的實例

    java操作solr實現(xiàn)查詢功能的實例

    下面小編就為大家分享一篇java操作solr實現(xiàn)查詢功能的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • 詳解Java中LinkedStack鏈棧的實現(xiàn)

    詳解Java中LinkedStack鏈棧的實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Java中LinkedStack鏈棧的相關(guān)知識,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-11-11
  • java單鏈表使用總結(jié)

    java單鏈表使用總結(jié)

    這篇文章主要為大家詳細(xì)介紹了java單鏈表使用總結(jié),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 使用java的HttpClient實現(xiàn)多線程并發(fā)

    使用java的HttpClient實現(xiàn)多線程并發(fā)

    這篇文章主要介紹了使用java的HttpClient實現(xiàn)多線程并發(fā)的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Java中的Vector和Stack底層源碼分析

    Java中的Vector和Stack底層源碼分析

    這篇文章主要介紹了Java中的Vector和Stack底層源碼分析,Stack繼承了Vector,Vector底層還是一個List,也就是基于數(shù)組來實現(xiàn)的,所以ArrayList有的優(yōu)點,比如獲取元素的速度快,隨機讀,它都有,需要的朋友可以參考下
    2023-12-12
  • 使用Post方法模擬登陸爬取網(wǎng)頁的實現(xiàn)方法

    使用Post方法模擬登陸爬取網(wǎng)頁的實現(xiàn)方法

    下面小編就為大家?guī)硪黄褂肞ost方法模擬登陸爬取網(wǎng)頁的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • java ClassLoader機制詳細(xì)講解

    java ClassLoader機制詳細(xì)講解

    ClassLoader一個經(jīng)常出現(xiàn)又讓很多人望而卻步的詞,本文將試圖以最淺顯易懂的方式來講解 ClassLoader,希望能對不了解該機制的朋友起到一點點作用
    2016-07-07
  • springboot中Getmapping獲取參數(shù)的實現(xiàn)方式

    springboot中Getmapping獲取參數(shù)的實現(xiàn)方式

    這篇文章主要介紹了springboot中Getmapping獲取參數(shù)的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評論