@Override
public String getGood(Long id) {
redisLock.tryLock(id+"userLock", 30, 30, TimeUnit.SECONDS);
Suser suserCache=redisCache.getCacheObject("suser:"+id);
if(suserCache==null){
Suser suser=suserMapper.selectById(id);
redisCache.setCacheObject("suser:"+id,suser,300, TimeUnit.SECONDS);
}
Suser suser=redisCache.getCacheObject("suser:"+id);
if(suser.getNum()<=0){
redisLock.unlock(id+"userLock");
return "商品数量不足";
}
suser.setNum(suser.getNum()-1);
redisCache.setCacheObject("suser:"+id,suser,300, TimeUnit.SECONDS);
suserMapper.updateById(suser);
redisLock.unlock(id+"userLock");
return "抢购成功";
}
|