Redis

关注公众号 jb51net

关闭
首页 > 数据库 > Redis > redis计数器与数量控制

redis计数器与数量控制的实现

作者:愚人钊呀

使用Redis计数器可以轻松地解决数量控制的问题,同时还能有效地提高应用的性能,本文主要介绍了redis计数器与数量控制的实现,具有一定的参考价值,感兴趣的可以了解一下

命令行命令:

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

视频学习地址

到此这篇关于redis计数器与数量控制的实现的文章就介绍到这了,更多相关redis计数器与数量控制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文