1. 程式人生 > >php結合redis秒殺

php結合redis秒殺

public function _initialize(){         parent::_initialize();         $goods_id = I("goods_id",'0','intval');               if($goods_id){             $this->goods_id = $goods_id;             $this->user_queue_key = "goods_".$goods_id."_user";//當前商品佇列的使用者情況             $this->goods_number_key = "goods".$goods_id;//當前商品的庫存佇列         }         $this->user_id = $this->user_id ? $this->user_id : $_SESSION['uid'];           }      public function _before_detail(){         $where['goods_id'] = $this->goods_id;         $where['start_time'] = array("lt",time());         $where['end_time'] =  array("gt",time());         $goods = M("goods")->where($where)->field('goods_num,start_time,end_time')->find();         !$goods && $this->error("當前秒殺已結束!");         if($goods['goods_num'] > $goods['order_num']){             $redis = $this->connectRedis();             $getUserRedis = $redis->hGetAll("{$this->user_queue_key}");//得到所有的使用者             $gnRedis = $redis->llen("{$this->goods_number_key}");//獲取庫存的數量             /* 如果沒有會員進來佇列庫存 */             if(!count($getUserRedis) && !$gnRedis){                             for ($i = 0; $i < $goods['goods_num']; $i ++) {                     $redis->lpush("{$this->goods_number_key}", 1);//將庫存存到redis中                 }             }             $resetRedis = $redis->llen("{$this->goods_number_key}");//獲取庫存的數量             if(!$resetRedis){                 $this->error("系統繁忙,請稍後搶購!");             }         }else{             $this->error("當前產品已經秒殺完!");         }               }      public function goods_number_queue(){         !$this->user_id && $this->ajaxReturn(array("status" => "-1","msg" => "請先登入"));         $model = M("flash_sale");         $where['goods_id'] = $this->goods_id;         $goods_info = $model->where($where)->find();         !$goods_info && $this->error("對不起當前商品不存在或已下架!");          /* redis 佇列 */           $redis = $this->connectRedis();         /* 進入佇列  */         $goods_number_key = $redis->llen("{$this->goods_number_key}");         if (!$redis->hGet("{$this->user_queue_key}", $this->user_id)) {             $goods_number_key = $redis->lpop("{$this->goods_number_key}");//刪除元素返回第一個元素         }                   if($goods_number_key){             // 判斷使用者是否已在佇列             if (!$redis->hGet("{$this->user_queue_key}", $this->user_id)) {                 // 插入搶購使用者資訊                 $userinfo = array(                     "user_id" => $this->user_id,                     "create_time" => time()                 );                                $redis->hSet("{$this->user_queue_key}", $this->user_id, serialize($userinfo));                 $this->ajaxReturn(array("status" => "1"));             }else{                 $modelCart = M("cart");                 $condition['user_id'] = $this->user_id;                 $condition['goods_id'] = $this->goods_id;                 $condition['prom_type'] = 1;         $cartlist = $modelCart->where($condition)->count();                 if($cartlist > 0){                     $this->ajaxReturn(array("status" => "2"));                 }else{                                       $this->ajaxReturn(array("status" => "1"));                                    }                               }                       }else{             $this->ajaxReturn(array("status" => "-1","msg" => "系統繁忙,請重試!"));         }     }