欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

秒杀代码

程序员文章站 2022-07-10 23:10:38
public function redis(){ $store=100; $redis=new \Redis(); $result=$redis->connect('127.0.0.1',6379); $res=$redis->llen('goods_store'); echo $res; $... ......
    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);
    }