Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

预减库存如果库存小于0,为什么要调用afterPropertiesSet()更新每件商品? #37

Open
nicexiaoliang opened this issue Feb 22, 2021 · 1 comment

Comments

@nicexiaoliang
Copy link

秒杀下单时的一段代码:
//预减库存 long stock = redisService.decr(GoodsKey.getGoodsStock, "" + goodsId);//10 if (stock < 0) { afterPropertiesSet(); long stock2 = redisService.decr(GoodsKey.getGoodsStock, "" + goodsId);//10 if(stock2 < 0){ localOverMap.put(goodsId, true); return Result.error(CodeMsg.SECKILL_OVER); } }

下面这个是afterPropertiesSet方法定义,每次调用都要获取所有商品并且遍历商品,这样会消耗性能吧,而且,我只是下单某一件商品,为什么在遍历中将每件商品标记为false?原本某些标记为true的商品变为false了
public void afterPropertiesSet() { List<GoodsVo> goodsVoList = goodsService.listGoodsVo(); if (goodsVoList == null) { return; } for (GoodsVo goods : goodsVoList) { redisService.set(GoodsKey.getGoodsStock, "" + goods.getId(), goods.getStockCount()); //初始化商品都是没有处理过的 localOverMap.put(goods.getId(), false); } }

@q258523454
Copy link

我也认为这段代码有问题。不应该在缓存不足的时候,直接调用 afterPropertiesSet(),这个方法不应该直接调用。建议:另外写一个方法,只刷新当前商品即可。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants