首先,生成庫存的計數(shù)量
?public?function?kucun() ????{ ????????//有十個庫存 ????????$count=10; ????????//添加到redis?list中 ????????for($i=0;$ilpush('kucun',111111111); ????????} ????????self::dd(Predis::getInstance()->lrange('kucun',0,-1)); ????}
完后利用redis的lpop或rpop對list進行裁剪,之前采用llen或incr的方式對數(shù)據(jù)進行判斷,都會出現(xiàn)超賣的現(xiàn)象,所以這里使用lpop的邏輯解決了超賣的問題
?public?function?ru() ????{??? ????????//判斷計數(shù)器 ????????if?(Predis::getInstance()->lpop('kucun'))?{ ????????????$user=User::where('user_id',1082)->find(); ????????????//存入會員id ????????????Predis::getInstance()->lpush('user',$user['user_id']); ????????????//計數(shù)器累計加1 ????????????//?Predis::getInstance()->incr('number'); ????????????echo?'加入秒殺成功';exit(); ????????}else{ ????????????echo?'活動截至'; ????????????exit(); ????????} ????}
測試:
ab?-r?-n?1000?-c?1000??http://149.28.16.212/index/index/ru
更多Redis相關(guān)知識,請訪問Redis使用教程欄目!
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載。
THE END