431330
模拟了一个秒杀的场景,发现count为0,但是有多个用户买了,麻烦大佬看看,谢谢! 代码如下:
async buy(ctx) {
try {
await redis.watch("goods_count");
const count = await redis.get("goods_count");
if (parseInt(count) > 0) {
console.log(count);
const time = new Date();
const at = Math.round(time / 1000);
const data = await redis.multi().zadd("user_goods_list", at, uuid.v4()).set("goods_count", count - 1).exec();
console.log(data);
ctx.body = {
message: "抢到了"
}
} else {
ctx.body = {
message: "goods is empty"
}
}
} catch (error) {
console.log(error)
ctx.body = {
message: "fail"
}
}
}