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"; } }
早年同窗始相知,三载瞬逝情却萌。年少不知愁滋味,犹读红豆生南国。别离方知相思苦,心田红豆根以生。