抢购

/*test*/
    public function rushToBuyAction(){
        $user_id  = rand(1,100);
        $key      = 'order_list:';
        $user_key = 'user_list:';
        $result = $this->redis->blPop($key,1);
        if(!$result){
            echo '抢购失败';
            die;
        }else{
            //判断是否已经抢购成功
            $user = $this->redis->lRange($user_key,0,-1);
            if(!empty($user) && in_array($user_id,$user)){
                $this->redis->lPush($key,1);
                echo '您已经抢购成功过了';
                die;
            }else{
                $this->redis->lPush($user_key,$user_id);
                echo '抢购成功';
                die;
            }
        }
    }

    /*test1*/
    public function createNumAction(){
        $key = 'order_list:';
        $num = 100;
        for( $i = 0; $i < $num; $i++ ){
            $this->redis->lPush($key,1);
        }
        echo 'success';
        die;
    }

猜你喜欢

转载自blog.csdn.net/samtrue/article/details/83817655