您好,登錄后才能下訂單哦!
如何在php中利用redis消息隊(duì)列實(shí)現(xiàn)一個(gè)搶購功能?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
實(shí)現(xiàn)功能:
1. 基于redis隊(duì)列,防止高并發(fā)的超賣
2. 基于mysql的事務(wù)加排它鎖,防止高并發(fā)的超賣
基于redis隊(duì)列工作流程:
1. 管理員根據(jù)goods表中的庫存,創(chuàng)建redis商品庫存隊(duì)列
2. 客戶端訪問秒殺API
3. web服務(wù)器先從redis的商品庫存隊(duì)列中查詢剩余庫存重點(diǎn)內(nèi)容
4. redis隊(duì)列中有剩余,則在mysql中創(chuàng)建訂單,去庫存,搶購成功
5. redis隊(duì)列中沒有剩余,則提示庫存不足,搶購失敗重點(diǎn)內(nèi)容
基于mysql事務(wù)和排它鎖工作流程:
1. 開啟事務(wù)
2. 查詢庫存,并顯示的設(shè)置寫鎖(排他鎖):SELECT * FROM goods WHERE id = 1 FOR UPDATE
3. 生成訂單
4. 去庫存,隱示的設(shè)置寫鎖(排他鎖):UPDATE goods SET counts = counts – 1 WHERE id = 1
5. commit,釋放鎖
注意:第二步步可以設(shè)置共享鎖,不然有可能會(huì)造成死鎖。
代碼:
<?php /********************************************** * 搶購模塊 * * @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)證庫存信息 * @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ù),不加寫鎖: * 超賣非常嚴(yán)重,就不說了 * * 2:$sql_forlock如果不加事務(wù),只加寫鎖: * 第一個(gè)會(huì)話讀$sql_forlock時(shí)加寫鎖,第一個(gè)會(huì)話$sql_forlock查詢結(jié)束會(huì)釋放該行鎖. * 第二個(gè)會(huì)話在第一個(gè)會(huì)話釋放后讀$sql_forlock的寫鎖時(shí),會(huì)再次$sql_forlock查庫存 * 導(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){ //去庫存 $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 = '購買成功'; return true; } } } $this->_error = '庫存不足'; return false; } /* * 基于redis隊(duì)列驗(yàn)證庫存信息 * @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 = '庫存不足'; 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); //庫存減少 $gid = $goodsInfo['id']; $sql = 'UPDATE goods SET counts = counts - 1 WHERE id = '.$gid; $result = $this->_goodsModel->exect($sql); $this->_error = '購買成功'; return true; } /* * 基于mysql事務(wù)驗(yàn)證庫存信息 * @desc 事務(wù) 和 行鎖 模式,高并發(fā)下不會(huì)導(dǎo)致超賣,但效率會(huì)慢點(diǎn) * @author liubin * @date 2017-02-10 說明: 如果$sql_forlock不加寫鎖,并發(fā)時(shí),$sql_forlock查詢的記錄存都大于0,可以減庫存操作. 如果$sql_forlock加了寫鎖,并發(fā)時(shí),$sql_forlock查詢是等待第一次鏈接釋放后查詢.所以庫存最多就是5 */ protected function order_check_transaction($gid){ $model = $this->_goodsModel; $pdo = $model->getHandler(); $gid = intval($gid); try{ $pdo->beginTransaction();//開啟事務(wù)處理 /* * 1:$sql_forlock如果只加事務(wù),不加寫鎖: * 開啟事務(wù) * 因?yàn)闆]有加鎖,讀$sql_forlock后,并發(fā)時(shí)$sql_inventory之前還可以再讀。 * $sql_inventory之后和commit之前才會(huì)鎖定 * 出現(xiàn)超賣跟事務(wù)的一致性不沖突 * * * 2:$sql_forlock如果加了事務(wù),又加讀鎖: * 開啟事務(wù) * 第一個(gè)會(huì)話讀$sql_forlock時(shí)加讀鎖,并發(fā)時(shí),第二個(gè)會(huì)話也允許獲得$sql_forlock的讀鎖, * 但是在第一個(gè)會(huì)話執(zhí)行去庫存操作時(shí)(寫鎖),寫鎖便會(huì)等待第二個(gè)會(huì)話的讀鎖,第二個(gè)會(huì)話執(zhí)行寫操作時(shí),寫鎖便會(huì)等待第一個(gè)會(huì)話的讀鎖, * 出現(xiàn)死鎖 * 3:$sql_forlock如果加了事務(wù),又加寫鎖: * 開啟事務(wù) * 第一個(gè)會(huì)話讀$sql_forlock時(shí)加寫鎖,直到commit才會(huì)釋放寫鎖,并發(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){ //去庫存 $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 = '庫存減少失敗'; 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 = '購買成功'; return true; }else{ $this->_error = '庫存不足'; return false; } }catch(PDOException $e){ echo $e->getMessage(); $pdo->rollBack(); } } /* * 創(chuàng)建訂單 * mysql 事物處理,也可以用存儲(chǔ)過程 * */ 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); //庫存減少 $gid = $goodsInfo['id']; $sql = 'UPDATE goods SET counts = counts - 1 WHERE id = '.$gid; $result = $this->_goodsModel->exect($sql); return true; } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。