redis使用setnx实现分布式锁

 

package com.shanhe.service;

import com.shanhe.entity.CommodityDetails;
import com.shanhe.lock.impl.RedisLockImpl;
import com.shanhe.mapper.CommodityDetailsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;


@Slf4j
@RestController
public class SeckillRedisService {
    @Autowired
    private CommodityDetailsMapper commodityDetailsMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private String redisLockKey = "Lock";

    //    /**
//     * redis实现分布式锁  idea调试 停止 属于正常停止 idea非调试的情况下 非正常停止
//     *
//     * @return
//     * @throws Exception
////     */
    @RequestMapping("/seckilRedisLock")
    public String seckilRedisLock(Long commodityId) throws Exception {
        try {
            // setnx
            Boolean lock = stringRedisTemplate.opsForValue().setIfAbsent(redisLockKey, "1", 30L, TimeUnit.SECONDS);
            if (!lock) {
               log.info("<获取锁失败>");
                return "获取失败啦,请稍后重试!";

            }
         
            CommodityDetails commodityDetails = commodityDetailsMapper.getCommodityDetails(commodityId);
            Long stock = commodityDetails.getStock();
            if (stock > 0) {
              log.info("<开始执行扣库存..>");
                int result = commodityDetailsMapper.reduceInventory(1l);
                return result > 0 ? "扣库存成功" : "扣库存失败";
            }
            // 扣库存失败
            log.info("<扣库存失败>");

        } catch (Exception e) {
            log.error("<e:>", e);
        } finally {
            // 释放锁
            log.info("<释放锁>");
            stringRedisTemplate.delete(redisLockKey);
        }
        return "fail";
    }
}

 

posted @ 2023-02-07 21:11  山河永慕~  阅读(106)  评论(0编辑  收藏  举报