First, generate the inventory count
public function kucun() { //有十个库存 $count=10; //添加到redis list中 for($i=0;$i<$count;$i++){ Predis::getInstance()->lpush('kucun',111111111); } self::dd(Predis::getInstance()->lrange('kucun',0,-1)); }
After completion, use redis's lpop or rpop to cut the list. Previously, llen or incr was used to judge the data. , there will be oversold phenomenon, so the logic of lpop is used here to solve the oversold problem
public function ru() { //判断计数器 if (Predis::getInstance()->lpop('kucun')) { $user=User::where('user_id',1082)->find(); //存入会员id Predis::getInstance()->lpush('user',$user['user_id']); //计数器累计加1 // Predis::getInstance()->incr('number'); echo '加入秒杀成功';exit(); }else{ echo '活动截至'; exit(); } }
Test:
ab -r -n 1000 -c 1000 http://149.28.16.212/index/index/ru
More Redis related knowledge , please visit the Redis usage tutorial column!
The above is the detailed content of How does redis solve the flash sale and overselling problem?. For more information, please follow other related articles on the PHP Chinese website!