最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

redis計數(shù)器與數(shù)量控制的實現(xiàn)

 更新時間:2023年12月19日 10:12:13   作者:愚人釗呀  
使用Redis計數(shù)器可以輕松地解決數(shù)量控制的問題,同時還能有效地提高應用的性能,本文主要介紹了redis計數(shù)器與數(shù)量控制的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下

命令行命令:

127.0.0.1:6379> exists mycounter
(integer) 0
127.0.0.1:6379> set mycounter 99 //設(shè)置一個值
OK
127.0.0.1:6379> get mycounter  //獲得一個值
"99"
127.0.0.1:6379> incr mycounter //對計數(shù)器進行增加操作
(integer) 100
127.0.0.1:6379> get mycounter
"100"
127.0.0.1:6379> incrby mycounter 2 //對計數(shù)器進行+2操作
(integer) 102
127.0.0.1:6379> get mycounter
"102"
127.0.0.1:6379> incrby mycounter -2 //對計數(shù)器進行-2操作
(integer) 100
127.0.0.1:6379> get mycounter
"100"
127.0.0.1:6379> setnx mycounter 99 //當Key不存在時,設(shè)置這個值
(integer) 0
127.0.0.1:6379> setnx mycounter1 99  //當Key不存在時,設(shè)置這個值
(integer) 1
127.0.0.1:6379> get mycounter1 
"99"
127.0.0.1:6379> get mycounter
"100"
127.0.0.1:6379> expire mycounter 30 //設(shè)置key的生存時間
(integer) 1
127.0.0.1:6379> ttl mycounter //獲得key的生存時間
(integer) 19
127.0.0.1:6379> ttl mycounter
(integer) -1
127.0.0.1:6379> exists mycounter
(integer) 1
127.0.0.1:6379> ttl mycounter
(integer) -1

數(shù)量控制器應用場景:

  • 商品搶購
  • 抽獎限量
  • 搶紅包

改進:

package com.example.demo.controller;

import com.example.demo.util.ResponseUtils;
import com.example.demo.util.dto.Data;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping(value = "/demo")
@Slf4j
public class DemoController {

    @Autowired
    private ValueOperations<String, String> strOperations;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ValueOperations<String, Integer> intOperations;
    @Autowired
    private RedisTemplate<String, Integer> intRedisTemplate;

    public static final String PREFIX = "mycounter_";


    @GetMapping("/v2")
    @ApiOperation(value = "方法v2")
    public ResponseEntity<Data<String>> demoV2() {


        // 2.保存數(shù)據(jù)
        strOperations.set("name", "imooc2");
        // 3. 獲取數(shù)據(jù)
        String value = strOperations.get("name");


        log.info("記個日志:{}", value);
        return ResponseUtils.res(value);
    }

    @GetMapping("/v3")
    @ApiOperation(value = "方法v3")
    public ResponseEntity<Data<String>> demoV3() {


        // 2.保存數(shù)據(jù)
        stringRedisTemplate.opsForValue().set("name", "imooc3");
        // 3. 獲取數(shù)據(jù)
        String value = stringRedisTemplate.opsForValue().get("name");


        log.info("記個日志:{}", value);
        return ResponseUtils.res(value);
    }

    /**
     * 數(shù)量控制器v1
     *
     * @return
     */
    @GetMapping("/count/v1")
    @ApiOperation(value = "數(shù)量控制器v1")
    public ResponseEntity<Data<String>> countV1() {
        String key = PREFIX + "v1";
        int amountLimit = 100;
        int incrAmount = 1;

        if (Objects.isNull(intOperations.get(key))) {
            intOperations.set(key, 95);
        }

        Integer currAmount = intOperations.get(key);

        if (currAmount + incrAmount > amountLimit) {
            log.info(" Bad Luck :{},{},currAmount + incrAmount > amountLimit={}", key, amountLimit,currAmount + incrAmount > amountLimit);

        } else {
            intOperations.set(key, currAmount + incrAmount);
            log.info(" Good Luck :{},{},currAmount + incrAmount > amountLimit={}", key, amountLimit,currAmount + incrAmount > amountLimit);
        }

        return ResponseUtils.res(currAmount.toString());
    }

    /**
     * 數(shù)量控制器v2
     *
     * @return
     */
    @GetMapping("/count/v2")
    @ApiOperation(value = "數(shù)量控制器v2")
    public ResponseEntity<Data<String>> countV2() {
        String key = PREFIX + "v2";
        int amountLimit = 100;
        Long incrAmount = 1L;
        int startValue = 95;
        if (!intRedisTemplate.hasKey(key)) {
            intRedisTemplate.opsForValue().setIfAbsent(key, startValue);
        }

        Integer currAmount = intRedisTemplate.opsForValue().get(key);
        Long increment = intRedisTemplate.opsForValue().increment(key, incrAmount);
        if (increment.intValue() > amountLimit) {
            log.info(" Bad Luck :{},{}", key, amountLimit);
        } else {
            log.info(" Good Luck :{},{}", key, amountLimit);
        }

        return ResponseUtils.res(currAmount.toString());
    }

}

利用apipost向v1發(fā)送請求:

向v2發(fā)送請求:

視頻學習地址

到此這篇關(guān)于redis計數(shù)器與數(shù)量控制的實現(xiàn)的文章就介紹到這了,更多相關(guān)redis計數(shù)器與數(shù)量控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Redis常見性能問題及解決方案

    Redis常見性能問題及解決方案

    本文給大家介紹Redis常見性能問題及解決方案,本文結(jié)合實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • 解決redis-cli報錯Could not connect to Redis at 127.0.0.1:6379: Connection refused

    解決redis-cli報錯Could not connect to Redis&

    這篇文章主要介紹了解決redis-cli報錯Could not connect to Redis at 127.0.0.1:6379: Connection refused,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 基于redis實現(xiàn)分布式鎖的原理與方法

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

    這篇文章主要給大家介紹了基于redis實現(xiàn)分布式鎖的原理與方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用redis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2020-06-06
  • Redis分布式鎖的7種實現(xiàn)

    Redis分布式鎖的7種實現(xiàn)

    這篇文章主要介紹了Redis分布式鎖的實現(xiàn)
    2022-04-04
  • Redis中Hash類型相關(guān)命令介紹

    Redis中Hash類型相關(guān)命令介紹

    Redis?hash是一個String類型的field和value的映射表,hash特別適合用于存儲對象,這篇文章主要介紹了Redis中Hash類型相關(guān)命令的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-12-12
  • Redis 事務知識點相關(guān)總結(jié)

    Redis 事務知識點相關(guān)總結(jié)

    這篇文章主要介紹了Redis 事務相關(guān)總結(jié),幫助大家更好的理解和學習使用Redis,感興趣的朋友可以了解下
    2021-03-03
  • Redis統(tǒng)計訪問量的3種實現(xiàn)方式

    Redis統(tǒng)計訪問量的3種實現(xiàn)方式

    這篇文章主要介紹了Redis統(tǒng)計訪問量的3種實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • redis中的事務操作案例分析

    redis中的事務操作案例分析

    這篇文章主要介紹了redis中的事務操作案例,結(jié)合具體實例形式詳細分析了redis事務操作的概念、原理、使用技巧與相關(guān)注意事項,需要的朋友可以參考下
    2019-07-07
  • redis中5種數(shù)據(jù)基礎(chǔ)查詢命令

    redis中5種數(shù)據(jù)基礎(chǔ)查詢命令

    本文主要介紹了redis中5種數(shù)據(jù)基礎(chǔ)查詢命令,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Redis中5種BitMap應用場景及實現(xiàn)介紹

    Redis中5種BitMap應用場景及實現(xiàn)介紹

    Redis BitMap是一種高效的位操作數(shù)據(jù)結(jié)構(gòu),這種結(jié)構(gòu)在處理海量數(shù)據(jù)的布爾型狀態(tài)時尤其高效,下面小編就來和大家簡單介紹一下5種它的應用場景及實現(xiàn)方法吧
    2025-04-04

最新評論

类乌齐县| 新疆| 尼勒克县| 鸡泽县| 聊城市| 万源市| 余姚市| 海兴县| 英吉沙县| 大港区| 龙胜| 兰州市| 泰顺县| 云霄县| 东山县| 石景山区| 黔江区| 灵山县| 宜春市| 四平市| 垣曲县| 鹤壁市| 彭水| 祁东县| 蒙城县| 彭阳县| 田东县| 司法| 五华县| 台南市| 绍兴县| 蒙阴县| 甘肃省| 井陉县| 弋阳县| 雷波县| 靖江市| 黔西县| 榆树市| 集贤县| 上高县|