当前位置: 移动技术网 > IT编程>开发语言>PHP > 秒杀代码

秒杀代码

2018年02月24日  | 移动技术网IT编程  | 我要评论
    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);
    }

  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网