Thinkphp redis 并发

Thinkphp5.0

数据库

tp5_goods字段:id,stock

tp5_goods_user字段:id,pid,name

 

php代码如下

redis = new \Redis();
        $this->redis->connect('192.168.70.161',6379);
        $this->redis->auth('admin999');
        //$this->stock = '1';
    }

    public function index(){
        $num = 10;
        $len = $this->redis->lLen('redis');
        if($len<10){
            $id = $this->push();
            $data['pid'] = 1;
            $data['name'] = $id;
            $res = db('goods_user')->insert($data);
            if($res ==1){
                exit('抢购成功');
            }
        }else{
            //echo '已抢光';
            exit('已抢光');
        }

    }

    // 追加队列
    public function push(){
        $str = $this->str();
        $this->redis->lpush('redis',$str);
        return $str;
    }

    // 生成随机字符串
    public function str(){
        return  rand(1000,9999);
    }

    // 剩余库存
    public function stock(){
        $res = db('goods')->field('stock')->find($this->id);
        return $res['stock'];
    }

}

 

你可能感兴趣的:(Thinkphp5)