private static boolean SP = true;
public ResponseDTO testMS(String userId){
if(userId==null){
return ResponseDTO.succMsg("未登录");
}
//商品模拟写死为100
String productID = "100";
if(SP) {
Boolean ifAbsent = redisTemplate.opsForValue().setIfAbsent(userId, productID,60, TimeUnit.SECONDS);
if(ifAbsent) {
//库存减1
long decr = redisService.decr(productID, 1);
if (decr >= 0) {
//用户查询是否成功订单查询
//redisService.set(userId, productID);
//发送到 MQ 对数据库订单表进行写入操作 不放在主逻辑进行
//.......
System.out.println(userId+"抢购成功!!");
return ResponseDTO.succData("抢购成功 前台启动查询订单接口");
} else {
redisService.del(userId);
SP = false;
}
}
}else{
return ResponseDTO.succMsg("商品已枪空!");
}
return ResponseDTO.succData("失败");
}
高并发秒杀伪代码实现逻辑
最新推荐文章于 2022-12-12 13:16:33 发布