濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > php+redis消息隊(duì)列實(shí)現(xiàn)搶購(gòu)功能

php+redis消息隊(duì)列實(shí)現(xiàn)搶購(gòu)功能

熱門(mén)標(biāo)簽:400電話申請(qǐng)方法收費(fèi) 電話機(jī)器人危險(xiǎn)嗎 深圳外呼系統(tǒng)收費(fèi) 江蘇外呼電銷機(jī)器人報(bào)價(jià) 400電話辦理福州市 南寧高頻外呼回?fù)芟到y(tǒng)哪家好 專業(yè)電話機(jī)器人批發(fā)商 長(zhǎng)沙crm外呼系統(tǒng)業(yè)務(wù) 離石地圖標(biāo)注

本文實(shí)例為大家分享了php+redis消息隊(duì)列實(shí)現(xiàn)搶購(gòu)的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)功能:

1. 基于redis隊(duì)列,防止高并發(fā)的超賣
2. 基于mysql的事務(wù)加排它鎖,防止高并發(fā)的超賣

基于redis隊(duì)列工作流程:

1. 管理員根據(jù)goods表中的庫(kù)存,創(chuàng)建redis商品庫(kù)存隊(duì)列
2. 客戶端訪問(wèn)秒殺API
3. web服務(wù)器先從redis的商品庫(kù)存隊(duì)列中查詢剩余庫(kù)存重點(diǎn)內(nèi)容
4. redis隊(duì)列中有剩余,則在mysql中創(chuàng)建訂單,去庫(kù)存,搶購(gòu)成功
5. redis隊(duì)列中沒(méi)有剩余,則提示庫(kù)存不足,搶購(gòu)失敗重點(diǎn)內(nèi)容

基于mysql事務(wù)和排它鎖工作流程:

1. 開(kāi)啟事務(wù)
2. 查詢庫(kù)存,并顯示的設(shè)置寫(xiě)鎖(排他鎖):SELECT * FROM goods WHERE id = 1 FOR UPDATE
3. 生成訂單
4. 去庫(kù)存,隱示的設(shè)置寫(xiě)鎖(排他鎖):UPDATE goods SET counts = counts – 1 WHERE id = 1
5. commit,釋放鎖

注意:第二步步可以設(shè)置共享鎖,不然有可能會(huì)造成死鎖。

代碼:

?php
/**********************************************
* 搶購(gòu)模塊
*
* @author liubin
* @date 2016-02-10
*
* ab -n 1000 -c 100 http://192.168.16.73/Seckill/buy.php
*
*/
class seckill extends common
{

 private $_orderModel = null;
 private $_goodsModel = null;
 private $_redis = null;
 /*
  * 錯(cuò)誤信息
 */
 protected $_error = '';
 /**
  * 構(gòu)造器
  *
 */
 public function __construct()
 {
  if($this->_orderModel === null){
   $this->_orderModel = new OrderModel();
  }
  if($this->_goodsModel === null){
   $this->_goodsModel = new GoodsModel();
  }
  if($this->_redis === null){
   $this->_redis = new QRedis(); 
  }
 }
 /*
  * 秒殺API
  * 
  * @author liubin
  * @date 2017-02-10
 */
 public function addQsec(){
  $gid = intval($_GET['gid']);
  $type = isset($_GET['type']) ? $_GET['type'] : 'mysql';
  switch ($type) {
   case 'mysql':
    $this->order_check_mysql($gid);
    echo $this->getError();
    break;
   case 'redis':
    $this->order_check_redis($gid);
    echo $this->getError();
    break;
   case 'transaction':
    $this->order_check_transaction($gid);
    echo $this->getError();
    break;
   default:
    echo '類型錯(cuò)誤';
    break;
  }
 }
 /*
  * 獲取錯(cuò)誤信息
  * 
  * @author liubin
  * @date 2017-02-10
 */
 public function getError(){
  return $this->_error;
 }
 /*
  * 基于mysql驗(yàn)證庫(kù)存信息
  * @desc 高并發(fā)下會(huì)導(dǎo)致超賣
  *
  * @author liubin
  * @date 2017-02-10
 */
 protected function order_check_mysql($gid){


  $model = $this->_goodsModel;
  $pdo = $model->getHandler();
  $gid = intval($gid);

  /*
   * 1:$sql_forlock如果不加事務(wù),不加寫(xiě)鎖:
   * 超賣非常嚴(yán)重,就不說(shuō)了
   * 
   * 2:$sql_forlock如果不加事務(wù),只加寫(xiě)鎖:
   * 第一個(gè)會(huì)話讀$sql_forlock時(shí)加寫(xiě)鎖,第一個(gè)會(huì)話$sql_forlock查詢結(jié)束會(huì)釋放該行鎖.
   * 第二個(gè)會(huì)話在第一個(gè)會(huì)話釋放后讀$sql_forlock的寫(xiě)鎖時(shí),會(huì)再次$sql_forlock查庫(kù)存
   * 導(dǎo)致超賣現(xiàn)象產(chǎn)生
   *
  */
  $sql_forlock = 'select * from goods where id = '.$gid .' limit 1 for update';
  //$sql_forlock = 'select * from goods where id = '.$gid .' limit 1';
  $result = $pdo->query($sql_forlock,PDO::FETCH_ASSOC);
  $goodsInfo = $result->fetch();

  if($goodsInfo['counts']>0){

   //去庫(kù)存
   $gid = $goodsInfo['id'];
   $sql_inventory = 'UPDATE goods SET counts = counts - 1 WHERE id = '.$gid;
   $result = $this->_goodsModel->exect($sql_inventory);
   if($result){
    //創(chuàng)訂單
    $data    = [];
    $data['order_id'] = $this->_orderModel->buildOrderNo();
    $data['goods_id'] = $goodsInfo['id'];
    $data['addtime'] = time();
    $data['uid']  = 1;
    $order_rs = $this->_orderModel->create_order($data);
    if($order_rs){
     $this->_error = '購(gòu)買成功';
     return true;
    }
   }
  }

  $this->_error = '庫(kù)存不足';
  return false;

 }
 /*
  * 基于redis隊(duì)列驗(yàn)證庫(kù)存信息
  * @desc Redis是底層是單線程的,命令執(zhí)行是原子操作,包括lpush,lpop等.高并發(fā)下不會(huì)導(dǎo)致超賣
  *
  * @author liubin
  * @date 2017-02-10
 */
 protected function order_check_redis($gid){
  $goodsInfo = $this->_goodsModel->getGoods($gid);
  if(!$goodsInfo){
   $this->_error = '商品不存在';
   return false;
  }
  $key = 'goods_list_'.$goodsInfo['id'];
  $count = $this->_redis->getHandel()->lpop($key);
  if(!$count){
   $this->_error = '庫(kù)存不足';
   return false;
  }
  //生成訂單
  $data    = [];
  $data['order_id'] = $this->_orderModel->buildOrderNo();
  $data['goods_id'] = $goodsInfo['id'];
  $data['addtime'] = time();
  $data['uid']  = 1;
  $order_rs = $this->_orderModel->create_order($data);

  //庫(kù)存減少
  $gid = $goodsInfo['id'];
  $sql = 'UPDATE goods SET counts = counts - 1 WHERE id = '.$gid;
  $result = $this->_goodsModel->exect($sql);
  $this->_error = '購(gòu)買成功';
  return true;
 }
 /*
  * 基于mysql事務(wù)驗(yàn)證庫(kù)存信息
  * @desc 事務(wù) 和 行鎖 模式,高并發(fā)下不會(huì)導(dǎo)致超賣,但效率會(huì)慢點(diǎn)
  * @author liubin
  * @date 2017-02-10


  說(shuō)明:
  如果$sql_forlock不加寫(xiě)鎖,并發(fā)時(shí),$sql_forlock查詢的記錄存都大于0,可以減庫(kù)存操作.
  如果$sql_forlock加了寫(xiě)鎖,并發(fā)時(shí),$sql_forlock查詢是等待第一次鏈接釋放后查詢.所以庫(kù)存最多就是5

 */
 protected function order_check_transaction($gid){

  $model = $this->_goodsModel;
  $pdo = $model->getHandler();
  $gid = intval($gid);

  try{
   $pdo->beginTransaction();//開(kāi)啟事務(wù)處理


   /*
    * 1:$sql_forlock如果只加事務(wù),不加寫(xiě)鎖:
    * 開(kāi)啟事務(wù)
    * 因?yàn)闆](méi)有加鎖,讀$sql_forlock后,并發(fā)時(shí)$sql_inventory之前還可以再讀。
    * $sql_inventory之后和commit之前才會(huì)鎖定
    * 出現(xiàn)超賣跟事務(wù)的一致性不沖突
    * 
    *
    * 2:$sql_forlock如果加了事務(wù),又加讀鎖:
    * 開(kāi)啟事務(wù)
    * 第一個(gè)會(huì)話讀$sql_forlock時(shí)加讀鎖,并發(fā)時(shí),第二個(gè)會(huì)話也允許獲得$sql_forlock的讀鎖,
    * 但是在第一個(gè)會(huì)話執(zhí)行去庫(kù)存操作時(shí)(寫(xiě)鎖),寫(xiě)鎖便會(huì)等待第二個(gè)會(huì)話的讀鎖,第二個(gè)會(huì)話執(zhí)行寫(xiě)操作時(shí),寫(xiě)鎖便會(huì)等待第一個(gè)會(huì)話的讀鎖,
    * 出現(xiàn)死鎖

    * 3:$sql_forlock如果加了事務(wù),又加寫(xiě)鎖:
    * 開(kāi)啟事務(wù)
    * 第一個(gè)會(huì)話讀$sql_forlock時(shí)加寫(xiě)鎖,直到commit才會(huì)釋放寫(xiě)鎖,并發(fā)查詢不會(huì)出現(xiàn)超賣現(xiàn)象。
    *
   */

   $sql_forlock = 'select * from goods where id = '.$gid .' limit 1 for update';
   //$sql_forlock = 'select * from goods where id = '.$gid .' limit 1 LOCK IN SHARE MODE';
   //$sql_forlock = 'select * from goods where id = '.$gid .' limit 1';
   $result = $pdo->query($sql_forlock,PDO::FETCH_ASSOC);
   $goodsInfo = $result->fetch();

   if($goodsInfo['counts']>0){

    //去庫(kù)存
    $gid = $goodsInfo['id'];
    $sql_inventory = 'UPDATE goods SET counts = counts - 1 WHERE id = '.$gid;
    $result = $this->_goodsModel->exect($sql_inventory);

    if(!$result){
     $pdo->rollBack();
     $this->_error = '庫(kù)存減少失敗';
     return false;
    }

    //創(chuàng)訂單
    $data    = [];
    $data['id']   = 'null';
    $data['order_id'] = $this->_orderModel->buildOrderNo();
    $data['goods_id'] = $goodsInfo['id'];
    $data['uid']  = 'abc';
    $data['addtime'] = time();

    $sql = 'insert into orders (id,order_id,goods_id,uid,addtime) values ('.$data['id'].',"'.$data['order_id'].'","'.$data['goods_id'].'","'.$data['uid'].'","'.$data['addtime'].'")';   
    $result = $pdo->exec($sql);
    if(!$result){
     $pdo->rollBack();
     $this->_error = '訂單創(chuàng)建失敗';
     return false;
    }
    $pdo->commit();//提交
    $this->_error = '購(gòu)買成功';
    return true;

   }else{
    $this->_error = '庫(kù)存不足';
    return false;
   }
  }catch(PDOException $e){
   echo $e->getMessage();
   $pdo->rollBack();
  }


 }
 /*
  * 創(chuàng)建訂單
  * mysql 事物處理,也可以用存儲(chǔ)過(guò)程
  *
 */
 private function create_order($goodsInfo){
  //生成訂單
  $data    = [];
  $data['order_id'] = $this->_orderModel->buildOrderNo();
  $data['goods_id'] = $goodsInfo['id'];
  $data['addtime'] = time();
  $data['uid']  = 1;
  $order_rs = $this->_orderModel->create_order($data);

  //庫(kù)存減少
  $gid = $goodsInfo['id'];
  $sql = 'UPDATE goods SET counts = counts - 1 WHERE id = '.$gid;
  $result = $this->_goodsModel->exect($sql);
  return true;
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • php使用redis的有序集合zset實(shí)現(xiàn)延遲隊(duì)列應(yīng)用示例
  • php+redis實(shí)現(xiàn)消息隊(duì)列功能示例
  • PHP實(shí)現(xiàn)基于Redis的MessageQueue隊(duì)列封裝操作示例
  • php基于Redis消息隊(duì)列實(shí)現(xiàn)的消息推送的方法
  • PHP使用Redis隊(duì)列執(zhí)行定時(shí)任務(wù)實(shí)例講解

標(biāo)簽:白酒營(yíng)銷 興安盟 濱州 南京 南昌 株洲 曲靖 太原

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php+redis消息隊(duì)列實(shí)現(xiàn)搶購(gòu)功能》,本文關(guān)鍵詞  php+redis,消息,隊(duì)列,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php+redis消息隊(duì)列實(shí)現(xiàn)搶購(gòu)功能》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于php+redis消息隊(duì)列實(shí)現(xiàn)搶購(gòu)功能的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    拉孜县| 临夏县| 竹山县| 习水县| 华阴市| 渭南市| 怀集县| 丽江市| 新野县| 神农架林区| 会昌县| 景东| 峡江县| 丰顺县| 青河县| 淄博市| 千阳县| 麟游县| 长沙县| 禄丰县| 特克斯县| 铁岭市| 开鲁县| 顺义区| 潞城市| 泰来县| 和林格尔县| 两当县| 赤城县| 陇西县| 邓州市| 准格尔旗| 临城县| 荣成市| 台东市| 宜宾县| 东宁县| 益阳市| 丁青县| 密云县| 南汇区|