Home>Article>Database> How does redis solve the flash sale and overselling problem?

How does redis solve the flash sale and overselling problem?

尚
Original
2019-07-05 16:16:12 9458browse

How does redis solve the flash sale and overselling problem?

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

How does redis solve the flash sale and overselling problem?

More Redis related knowledge , please visit theRedis usage tutorialcolumn!

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn