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

redis保存AtomicInteger對象踩坑及解決

 更新時(shí)間:2022年11月22日 17:19:40   作者:夜月如水  
這篇文章主要介紹了redis保存AtomicInteger對象踩坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

redis保存AtomicInteger對象踩坑

redisTemplate 保存AtomicInteger對象異常:

java.lang.ClassCastException: java.util.concurrent.atomic.AtomicInteger cannot be cast to java.lang.String
    at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:36)
    at org.springframework.data.redis.core.AbstractOperations.rawValue(AbstractOperations.java:127)
    at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:235)
    at com.quan.starter.service.impl.RedisServiceImpl.set(RedisServiceImpl.java:139)

跟蹤源碼發(fā)現(xiàn)其執(zhí)行的是 StringRedisSerializer 的實(shí)現(xiàn),serialize默認(rèn)接收的參數(shù)類型為String 從而拋出以上異常

經(jīng)過檢查,發(fā)現(xiàn)是RedisTemplate泛型惹的禍:

@Autowired
private RedisTemplate<String, String> redisTemplate;

解決方案

去除泛型:

@Autowired
private RedisTemplate redisTemplate;

運(yùn)行服務(wù)再次跟蹤源碼,執(zhí)行的是 DefaultValueOperations 的實(shí)現(xiàn),問題解決

RedisAtomicInteger的使用

RedisAtomicInteger 從名字上來說就是 redis 的原子Integer 數(shù)據(jù)類型,由于其原子性,可用于秒殺活動物品數(shù)量的控制。

以及保證順序生成數(shù)字。

  @Resource
    RedisTemplate<String, Object> redisTemplate;


    /**
     * RedisAtomicInteger
     *
     * @throws Exception 異常
     */
    @Test
    public void testTransaction1() throws Exception {
        RedisAtomicInteger redisCount = new RedisAtomicInteger("key1", this.redisTemplate.getConnectionFactory());
        redisCount.set(0);
        // 創(chuàng)建 100 個(gè)線程 并發(fā)執(zhí)行  increment 操作
        ExecutorService pool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 100; i++) {
            pool.submit(() -> {
                // 配額碼原子變量值增加,每次增加1
                for (int j = 0; j < 100; j++) {
                    int count = redisCount.incrementAndGet();
                    log.info(Thread.currentThread().getName() + ": " + count);
                }
            });
        }
    }

結(jié)果

.
.
.
pool-2-thread-90: 9989
pool-2-thread-61: 9987
pool-2-thread-3: 9986
pool-2-thread-12: 9990
pool-2-thread-25: 9991
pool-2-thread-90: 9992
pool-2-thread-12: 9994
pool-2-thread-61: 9993
pool-2-thread-25: 9995
pool-2-thread-61: 10000
pool-2-thread-12: 9996
pool-2-thread-61: 9997
pool-2-thread-25: 9998
pool-2-thread-12: 9999

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

相關(guān)文章

  • Redis持久化AOF示例詳解

    Redis持久化AOF示例詳解

    AOF(Append-Only?File)用于將Redis服務(wù)器收到的寫操作追加到日志文件,通過該機(jī)制可以保證服務(wù)器重啟后依然可以依靠日志文件恢復(fù)數(shù)據(jù),這篇文章主要介紹了Redis持久化AOF詳解,需要的朋友可以參考下
    2023-12-12
  • 詳解redis是如何實(shí)現(xiàn)隊(duì)列消息的ack

    詳解redis是如何實(shí)現(xiàn)隊(duì)列消息的ack

    這篇文章主要介紹了關(guān)于redis是如何實(shí)現(xiàn)隊(duì)列消息的ack的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04
  • redis鍵空間通知使用實(shí)現(xiàn)

    redis鍵空間通知使用實(shí)現(xiàn)

    這篇文章主要介紹了redis鍵空間通知使用實(shí)現(xiàn)
    2021-08-08
  • 使用Redis命令操作數(shù)據(jù)庫的常見錯誤及解決方法

    使用Redis命令操作數(shù)據(jù)庫的常見錯誤及解決方法

    由于Redis是內(nèi)存數(shù)據(jù)庫,因此可能會存在一些安全問題,下面這篇文章主要給大家介紹了關(guān)于使用Redis命令操作數(shù)據(jù)庫的常見錯誤及解決方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-02-02
  • Redis中鍵和數(shù)據(jù)庫通用指令詳解

    Redis中鍵和數(shù)據(jù)庫通用指令詳解

    這篇文章主要為大家介紹了Redis中鍵和數(shù)據(jù)庫通用指令基本操作詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Redis實(shí)現(xiàn)數(shù)據(jù)的交集、并集、補(bǔ)集的示例

    Redis實(shí)現(xiàn)數(shù)據(jù)的交集、并集、補(bǔ)集的示例

    本文主要介紹了Redis實(shí)現(xiàn)數(shù)據(jù)的交集、并集、補(bǔ)集的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Redis基本數(shù)據(jù)類型List常用操作命令

    Redis基本數(shù)據(jù)類型List常用操作命令

    這篇文章主要為大家介紹了Redis數(shù)據(jù)類型List常用命令操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 基于redis實(shí)現(xiàn)分布式鎖的原理與方法

    基于redis實(shí)現(xiàn)分布式鎖的原理與方法

    這篇文章主要給大家介紹了基于redis實(shí)現(xiàn)分布式鎖的原理與方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Redis源碼解析:集群手動故障轉(zhuǎn)移、從節(jié)點(diǎn)遷移詳解

    Redis源碼解析:集群手動故障轉(zhuǎn)移、從節(jié)點(diǎn)遷移詳解

    這篇文章主要介紹了Redis源碼解析:集群手動故障轉(zhuǎn)移、從節(jié)點(diǎn)遷移的相關(guān)內(nèi)容,涉及通過集群定時(shí)器函數(shù)clusterCron實(shí)現(xiàn)從節(jié)點(diǎn)遷移等知識,具有一定參考價(jià)值,需要的朋友可以了解。
    2017-10-10
  • redis緩存穿透解決方法

    redis緩存穿透解決方法

    在本篇文章里小編給大家分享了關(guān)于redis緩存穿透的解決方法以及相關(guān)實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-06-06

最新評論