使用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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 分析并發(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ù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12使用java的HttpClient實現(xiàn)多線程并發(fā)
這篇文章主要介紹了使用java的HttpClient實現(xiàn)多線程并發(fā)的相關(guān)資料,需要的朋友可以參考下2016-09-09使用Post方法模擬登陸爬取網(wǎng)頁的實現(xiàn)方法
下面小編就為大家?guī)硪黄褂肞ost方法模擬登陸爬取網(wǎng)頁的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03springboot中Getmapping獲取參數(shù)的實現(xiàn)方式
這篇文章主要介紹了springboot中Getmapping獲取參數(shù)的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05