秒杀代码

2018-02-26 08:12:57来源:cnblogs.com作者:张扬个性,敢为天下先人点击

分享
    public function redis(){        $store=100;        $redis=new /Redis();        $result=$redis->connect('127.0.0.1',6379);        $res=$redis->llen('goods_store');        echo $res;        $count=$store-$res;        for($i=0;$i<$count;$i++){            $redis->lpush('goods_store',1);        }        echo $redis->llen('goods_store');    }    public function go(){        $redis=new /Redis();        $result=$redis->connect('127.0.0.1',6379);        $count=$redis->lpop('goods_store');        if(!$count){            $this->insertLog('error:no store redis');            echo "秒杀结束";            exit;        }         //库存减少                $is_true=M("Store","ih_")->where('id=1 and number>0')->setDec('number');                if($is_true){            $this->insertLog('库存减少成功'.I("user_id"),1);            echo "秒杀成功";        }else{            $this->insertLog('库存减少失败'.I("user_id"),2);            echo "秒杀失败";        }    }   public function insertLog($event,$type=0){         $model=M("Log","ih_");         $data["event"]=$event;         $data["type"]=$type;         $model->add($data);    }

  

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台