springboot集合redis之模拟库存扣减1.decrby 2.lua操作

1.模拟库存扣减,扣到0后还会一直扣,不能保证原子性

package com.by;

import cn.hutool.core.util.StrUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootTest
class DecryDemoTest {

    @Resource(name="redisTemplate")
    private ValueOperations<String,Long> valueOperations;

    private final  String key = "protect#01";

    /**
     * 模拟库存扣减
     */
    @Test
    void test() {
       //设置库存为5
        valueOperations.set(key, 5l);
        //开启一个线程池
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 1; i <= 10;i++){
            //执行  每个人买一份
            executorService.execute(() -> {
                Long decrement = valueOperations.decrement(key, 1);

                if (decrement>=0){

                    System.out.println(StrUtil.format("线程{},扣减成功{}",Thread.currentThread().getName(),decrement));
                }else{
                    System.out.println(StrUtil.format("线程{},扣减失败{}",Thread.currentThread().getName(),decrement));
                }
            });
        }
    }
}


2.模拟库存扣减,扣到0之后不会一直扣,保证原子性

package com.by;

import cn.hutool.core.collection.CollectionUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;

@SpringBootTest
class LuaDemoTest {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 模拟库存扣减,扣到0之后不会一直扣,保证原子性
     */
    @Test
    void test() {

       String key = "pro#01";
       //设置商品库存量
        redisTemplate.opsForValue().set(key,"5");
       //需求量
       String qty="2";
        //lua脚本
        //我希望能扣减成功返回1,不能扣减成功返回0
        StringBuilder luasb=new StringBuilder();
        //空格不能少
        //书写lua脚本语言    local定义变量
        luasb.append(" local key=KEYS[1] ");
        luasb.append(" local qty=redis.call('get',key); ");
        //库存量大于需求量
        luasb.append(" if tonumber(qty)>=tonumber(ARGV[1])");
        luasb.append(" then ");
        luasb.append(" redis.call('decrby',KEYS[1],ARGV[1]) ");
        luasb.append(" return 1 ");
        luasb.append(" else ");
        luasb.append(" return 0 ");
        luasb.append(" end ");
        RedisScript<Long> redisScript = RedisScript.of(luasb.toString(), Long.class);
        //执行结果
        Long aLong = redisTemplate.execute(redisScript, CollectionUtil.newArrayList(key), qty);

        System.out.println(aLong);


    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

great-sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值