Home > Database > Redis > body text

How does redis solve the flash sale and overselling problem?

Release: 2019-07-05 16:16:12
Original
9576 people have browsed it

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(&#39;kucun&#39;,111111111);
        }
        self::dd(Predis::getInstance()->lrange(&#39;kucun&#39;,0,-1));
    }
Copy after login

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(&#39;kucun&#39;)) {
            $user=User::where(&#39;user_id&#39;,1082)->find();
            //存入会员id
            Predis::getInstance()->lpush(&#39;user&#39;,$user[&#39;user_id&#39;]);
            //计数器累计加1
            // Predis::getInstance()->incr(&#39;number&#39;);
            echo &#39;加入秒杀成功&#39;;exit();
        }else{
            echo &#39;活动截至&#39;;
            exit();
        }
    }
Copy after login

Test:

ab -r -n 1000 -c 1000  http://149.28.16.212/index/index/ru
Copy after login

How does redis solve the flash sale and overselling problem?

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template