java商城的面试题,商城项目秒杀模块问题,面试时被问到的

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

@Override//保存秒杀订单

public void saveSeckillOrder(Long seckillGoodsId, String userId) {

//有请求下单的任务,就把排队人数加一

redisTemplate.boundValueOps("SECKILL_COUNT_ORDER_QUEUE").increment(1);

//判断该用户是否已经秒杀过啦

Boolean member = redisTemplate.boundSetOps("SECKILL_PAY_LOG" + seckillGoodsId).isMember(userId);

if (member) {

throw new RuntimeException("请先支付,不允许重复秒杀");

}

//解决超卖问题 task里leftpush 这里rightpop [3,2,1]

Object o = redisTemplate.boundListOps("SECKILL_GOODS_QUEUE" + seckillGoodsId).rightPop();

if (o == null) {

throw new RuntimeException("被抢光啦");

}

TbSeckillGoods seckill_goods = (TbSeckillGoods) redisTemplate.boundHashOps("SECKILL_GOODS").get(seckillGoodsId);

//判断是否有库存

if (seckill_goods.getStockCount() <= 0 || seckill_goods == null) {

throw new RuntimeException("被抢光啦");

}

//当排队人数过多时,提示

Long count_order_queue = redisTemplate.boundValueOps("SECKILL_COUNT_ORDER_QUEUE").size();

if (count_order_queue > (seckill_goods.getStockCount() + 10)) {

throw new RuntimeException("当前人数过多");

}

//将多线程操作mysql任务放入redis

redisTemplate.boundListOps("SECKILL_ORDER_QUEUE").leftPush(new UserIdAndSeckillGoodsId(userId, seckillGoodsId));

executor.execute(createOrder);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值