redis計(jì)數(shù)器與數(shù)量控制的實(shí)現(xiàn)
命令行命令:
127.0.0.1:6379> exists mycounter (integer) 0 127.0.0.1:6379> set mycounter 99 //設(shè)置一個(gè)值 OK 127.0.0.1:6379> get mycounter //獲得一個(gè)值 "99" 127.0.0.1:6379> incr mycounter //對計(jì)數(shù)器進(jìn)行增加操作 (integer) 100 127.0.0.1:6379> get mycounter "100" 127.0.0.1:6379> incrby mycounter 2 //對計(jì)數(shù)器進(jìn)行+2操作 (integer) 102 127.0.0.1:6379> get mycounter "102" 127.0.0.1:6379> incrby mycounter -2 //對計(jì)數(shù)器進(jìn)行-2操作 (integer) 100 127.0.0.1:6379> get mycounter "100" 127.0.0.1:6379> setnx mycounter 99 //當(dāng)Key不存在時(shí),設(shè)置這個(gè)值 (integer) 0 127.0.0.1:6379> setnx mycounter1 99 //當(dāng)Key不存在時(shí),設(shè)置這個(gè)值 (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的生存時(shí)間 (integer) 1 127.0.0.1:6379> ttl mycounter //獲得key的生存時(shí)間 (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ù)量控制器應(yīng)用場景:
- 商品搶購
- 抽獎(jiǎng)限量
- 搶紅包


改進(jìn):


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("記個(gè)日志:{}", 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("記個(gè)日志:{}", 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計(jì)數(shù)器與數(shù)量控制的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)redis計(jì)數(shù)器與數(shù)量控制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決redis-cli報(bào)錯(cuò)Could not connect to Redis&
這篇文章主要介紹了解決redis-cli報(bào)錯(cuò)Could not connect to Redis at 127.0.0.1:6379: Connection refused,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
基于redis實(shí)現(xiàn)分布式鎖的原理與方法
這篇文章主要給大家介紹了基于redis實(shí)現(xiàn)分布式鎖的原理與方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用redis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Redis 事務(wù)知識點(diǎn)相關(guān)總結(jié)
這篇文章主要介紹了Redis 事務(wù)相關(guān)總結(jié),幫助大家更好的理解和學(xué)習(xí)使用Redis,感興趣的朋友可以了解下2021-03-03
Redis統(tǒng)計(jì)訪問量的3種實(shí)現(xiàn)方式
這篇文章主要介紹了Redis統(tǒng)計(jì)訪問量的3種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
redis中5種數(shù)據(jù)基礎(chǔ)查詢命令
本文主要介紹了redis中5種數(shù)據(jù)基礎(chǔ)查詢命令,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Redis中5種BitMap應(yīng)用場景及實(shí)現(xiàn)介紹
Redis BitMap是一種高效的位操作數(shù)據(jù)結(jié)構(gòu),這種結(jié)構(gòu)在處理海量數(shù)據(jù)的布爾型狀態(tài)時(shí)尤其高效,下面小編就來和大家簡單介紹一下5種它的應(yīng)用場景及實(shí)現(xiàn)方法吧2025-04-04

