redis计数器与数量控制

2023-12-13 22:11:29

命令行命令:

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

数量控制器应用场景:

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

改进:


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发送请求:


视频学习地址

文章来源:https://blog.csdn.net/qq_44114187/article/details/134826526
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。