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

 更新时间:2023年12月19日 10:12:13   作者:愚人钊呀  
使用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计数器与数量控制内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Redis内存满了的几种原因和最佳解决方案

    Redis内存满了的几种原因和最佳解决方案

    Redis是一款高性能的内存数据库,被广泛应用于缓存、消息队列、计数器等场景,然而,由于Redis是基于内存的数据库,当数据量过大或者配置不合理时,就有可能导致Redis的内存满,本文将介绍Redis内存满的几种原因,并提供相应的解决方案,需要的朋友可以参考下
    2023-11-11
  • Redis 缓存满了如何解决

    Redis 缓存满了如何解决

    Redis 缓存使用内存来保存数据,随着需要缓存的数据量越来越大,有限的缓存空间不可避免地会被写满,本文主要介绍了Redis 缓存满了如何解决,感兴趣的可以了解一下
    2023-08-08
  • Redis高效率原因及数据结构分析

    Redis高效率原因及数据结构分析

    这篇文章主要为大家详细的介绍了Redis高效的原因以及分析了Redis高效的数据结构,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2021-09-09
  • 还不懂Redis?看完这个趣味小故事就明白了!

    还不懂Redis?看完这个趣味小故事就明白了!

    这篇文章主要用趣味性的方法讲解了redis是什么?并且和MYSQL的区别是什么,有对redis不太懂的小伙伴可以来看一下吧
    2020-12-12
  • 银河麒麟V10sp1服务器系统安装redis不能使用的快速解决办法

    银河麒麟V10sp1服务器系统安装redis不能使用的快速解决办法

    这篇文章主要介绍了银河麒麟V10sp1服务器系统安装redis不能使用的快速解决办法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • Win10配置redis服务实现过程详解

    Win10配置redis服务实现过程详解

    这篇文章主要介绍了Win10配置redis服务实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • 在K8s上部署Redis集群的方法步骤

    在K8s上部署Redis集群的方法步骤

    这篇文章主要介绍了在K8s上部署Redis集群的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Redis遍历所有key的两个命令(KEYS 和 SCAN)

    Redis遍历所有key的两个命令(KEYS 和 SCAN)

    这篇文章主要介绍了Redis遍历所有key的两个命令(KEYS 和 SCAN),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Spring Boot 中的 Redis 分布式锁

    Spring Boot 中的 Redis 分布式锁

    这篇文章主要介绍了Spring Boot 中的 Redis 分布式锁及,Redis分布式锁的优化需要的朋友可以参考下
    2023-10-10
  • windows上修改redis端口号的操作步骤

    windows上修改redis端口号的操作步骤

    redis是一个开源的内存数据结构存储系统,常用做数据库、缓存和消息代理,默认的端口号为6379,那么如何在windows上修改redis端口号,接下来本文给大家详细介绍了windows上修改redis端口号的操作方法,需要的朋友可以参考下
    2024-02-02

最新评论