redis计数器与数量控制

命令行命令:

java 复制代码
127.0.0.1:6379> exists mycounter
(integer) 0
127.0.0.1:6379> set mycounter 99 //设置一个值
OK
127.0.0.1:6379> get mycounter  //获得一个值
"99"
127.0.0.1:6379> incr mycounter //对计数器进行增加操作
(integer) 100
127.0.0.1:6379> get mycounter
"100"
127.0.0.1:6379> incrby mycounter 2 //对计数器进行+2操作
(integer) 102
127.0.0.1:6379> get mycounter
"102"
127.0.0.1:6379> incrby mycounter -2 //对计数器进行-2操作
(integer) 100
127.0.0.1:6379> get mycounter
"100"
127.0.0.1:6379> setnx mycounter 99 //当Key不存在时,设置这个值
(integer) 0
127.0.0.1:6379> setnx mycounter1 99  //当Key不存在时,设置这个值
(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 //设置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

数量控制器应用场景:

  • 商品抢购
  • 抽奖限量
  • 抢红包

++改进:++


java 复制代码
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.保存数据
        strOperations.set("name", "imooc2");
        // 3. 获取数据
        String value = strOperations.get("name");


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

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


        // 2.保存数据
        stringRedisTemplate.opsForValue().set("name", "imooc3");
        // 3. 获取数据
        String value = stringRedisTemplate.opsForValue().get("name");


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

    /**
     * 数量控制器v1
     *
     * @return
     */
    @GetMapping("/count/v1")
    @ApiOperation(value = "数量控制器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());
    }

    /**
     * 数量控制器v2
     *
     * @return
     */
    @GetMapping("/count/v2")
    @ApiOperation(value = "数量控制器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发送请求:

向v2发送请求:


视频学习地址

相关推荐
(⊙o⊙)~哦16 分钟前
JavaScript substring() 方法
前端
无心使然云中漫步39 分钟前
GIS OGC之WMTS地图服务,通过Capabilities XML描述文档,获取matrixIds,origin,计算resolutions
前端·javascript
Bug缔造者1 小时前
Element-ui el-table 全局表格排序
前端·javascript·vue.js
xnian_1 小时前
解决ruoyi-vue-pro-master框架引入报错,启动报错问题
前端·javascript·vue.js
码农郁郁久居人下2 小时前
Redis的配置与优化
数据库·redis·缓存
麒麟而非淇淋2 小时前
AJAX 入门 day1
前端·javascript·ajax
2401_858120532 小时前
深入理解MATLAB中的事件处理机制
前端·javascript·matlab
阿树梢2 小时前
【Vue】VueRouter路由
前端·javascript·vue.js
随笔写4 小时前
vue使用关于speak-tss插件的详细介绍
前端·javascript·vue.js
Hsu_kk4 小时前
Redis 主从复制配置教程
数据库·redis·缓存