springboot执行redis事务、流水线、订阅和lua脚本

项目地址:

https://github.com/HouChenggong/springboot_redis_transaction.git

package com.pf.org.cms.hcg.system.miaosha;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Long flashSellByLuaScript(String skuCode,int num) {
        String luaScript ="local buyNum = ARGV[1]\n" +
                "local goodsKey = KEYS[1]  \n" +
                "local goodsNum = redis.call('get',goodsKey) \n" +
                "if goodsNum >= buyNum \n" +
                "then redis.call('decrby',goodsKey,buyNum) \n" +
                "return buyNum \n" +
                "else \n" +
                "return '0'\n" +
                "end\n" +
                "\n" ;

        DefaultRedisScript<String> re = new DefaultRedisScript<String>();
        //设置脚本
        re.setScriptText(luaScript);
        //定义返回值类型,注意,如果没有这个定义,Spring不会返回结果
        re.setResultType(String.class);
        RedisSerializer<String> stringRedisSerializer = stringRedisTemplate.getStringSerializer();
        //执行LUA脚本
        String result = (String) stringRedisTemplate.execute(re, stringRedisSerializer, stringRedisSerializer, null);
        return Long.valueOf(result);
    }

    @Override
    public Long flashSellByRedisWatch(String skuCode,int num){

        SessionCallback<Long> sessionCallback = new SessionCallback<Long>() {
            @Override
            public Long execute(RedisOperations operations) throws DataAccessException {
                int result = num;
                //redis 乐观锁
                operations.watch(skuCode);
                ValueOperations<String, String> valueOperations = operations.opsForValue();
                String goodsNumStr = valueOperations.get(skuCode);
                Integer goodsNum = Integer.valueOf(goodsNumStr);
                //标记一个事务块的开始。
                //事务块内的多条命令会按照先后顺序被放进一个队列当中,
                //最后由 EXEC 命令原子性(atomic)地执行。
                operations.multi();
                if (goodsNum >= num) {
                    valueOperations.increment(skuCode, 0 - num);
                } else {
                    result = 0;
                }
                //多条命令执行的结果集合
                List exec = operations.exec();
                if(exec.size()>0){
                    System.out.println(exec);
                }
                return (long) result;
            }
        };
        return stringRedisTemplate.execute(sessionCallback);
    }
//省略 其他的方法


}

    @ApiOperation(value = "用事务秒杀测试接口", notes = "用事务秒杀测试接口")
    @RequestMapping(value = "/miaoTransaction", method = RequestMethod.GET)
    @ResponseBody
    public Long miaoTransaction() {

        Long res = goodsService.flashSellByRedisWatch("xiaomi", 1);
        return res;
    }


    @ApiOperation(value = " 秒杀Lua测试接口", notes = "秒杀Lua测试接口")
    @RequestMapping(value = "/miaoLua", method = RequestMethod.GET)
    @ResponseBody
    public Long miaoLua() {

        Long res = goodsService.flashSellByRedisWatch("xiaomi", 1);
        System.out.println(res.toString());
        return res;
    }
其实是带项目地址的:
https://github.com/HouChenggong/springboot_redis_transaction.git
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值