使用RedisAtomicLong優(yōu)化性能問題
RedisAtomicLong優(yōu)化性能
在項(xiàng)目中許多過這樣的需求,記錄留做備忘。
需要創(chuàng)建一個(gè)遞增序列,這個(gè)序列會提供給多個(gè)應(yīng)用來使用,這樣就需要保持序列的原子遞增。
RedisAtomicLong
spring-data-redis包中提供的,可以對數(shù)據(jù)中的Long類型進(jìn)行原子性操作的類,下面是這個(gè)類的頭:
/** ?* 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包一樣進(jìn)行原子性操作,兩者不同的是:
AtomicLong
只能在一個(gè)應(yīng)用中使用RedisAtomicLong
可以在所有與Redis有連接的應(yīng)用中使用
開始優(yōu)化
應(yīng)用初始化時(shí)創(chuàng)建RedisAtomicLong實(shí)例。
?// // 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); ? ? }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 分析并發(fā)編程之LongAdder原理
- 一篇帶你解析入門LongAdder源碼
- java高并發(fā)下CopyOnWriteArrayList替代ArrayList
- java高并發(fā)ScheduledThreadPoolExecutor類深度解析
- java高并發(fā)ScheduledThreadPoolExecutor與Timer區(qū)別
- java高并發(fā)ThreadPoolExecutor類解析線程池執(zhí)行流程
- java高并發(fā)InterruptedException異常引發(fā)思考
- java高并發(fā)下解決AtomicLong性能瓶頸方案LongAdder
相關(guān)文章
java線程池合理設(shè)置最大線程數(shù)和核心線程數(shù)方式
這篇文章主要介紹了java線程池合理設(shè)置最大線程數(shù)和核心線程數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java操作solr實(shí)現(xiàn)查詢功能的實(shí)例
下面小編就為大家分享一篇java操作solr實(shí)現(xiàn)查詢功能的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11詳解Java中LinkedStack鏈棧的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java中LinkedStack鏈棧的相關(guān)知識,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下2022-11-11使用java的HttpClient實(shí)現(xiàn)多線程并發(fā)
這篇文章主要介紹了使用java的HttpClient實(shí)現(xiàn)多線程并發(fā)的相關(guān)資料,需要的朋友可以參考下2016-09-09使用Post方法模擬登陸爬取網(wǎng)頁的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄褂肞ost方法模擬登陸爬取網(wǎng)頁的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03java ClassLoader機(jī)制詳細(xì)講解
ClassLoader一個(gè)經(jīng)常出現(xiàn)又讓很多人望而卻步的詞,本文將試圖以最淺顯易懂的方式來講解 ClassLoader,希望能對不了解該機(jī)制的朋友起到一點(diǎn)點(diǎn)作用2016-07-07springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了springboot中Getmapping獲取參數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05