Thinkphp5+Redis實現商品秒殺
環境:wamp,redis
要求:安裝WAMP,Redis,以及為PHP安裝Redis擴充套件(怎麼安裝Redis可以看看我前面寫的文章)
秒殺功能大致思路:獲取快取列表的長度,如果長度(llen)等於0,就停止秒殺,即秒殺失敗,如果長度大於0,則繼續執行,先從快取中移除一個元素(lpop),再進行資料庫操作(新增訂單表,商品庫存數量減一),如果再進一個人秒殺,就再走一遍流程,迴圈往復。
一、安裝Redis擴充套件
1.檢視PHP版本資訊
開啟phpinfo.php,檢視PHP版本,我的是PHP7.3.4,還有一個需要注意Architecture x64
2.下載擴充套件檔案
https://pecl.php.net/package/redis
https://pecl.php.net/package/igbinary
根據自己環境,選擇合適的版本
3.解壓
解壓下載的壓縮包,並把php_redis.dll、php_redis.pdb和php_igbinary.dll、php_igbinary.pdb四個檔案,移至自己PHP版本對應目錄下的ext資料夾下E:\phpstudy_pro\Extensions\php\php7.3.4nts\ext
4.修改php.ini
新增如下程式碼:
extension=php_igbinary.dll
extension=php_redis.dll
如果有這兩句可以把前面的分號刪掉,沒有就自己新增上,要注意順序,php_igbinary.dll 要在php_redis.dll 前面
5.重啟Apache
重啟後,再執行phpinfo.php,檢視是否安裝成功
二、資料結構
一共三張表,ab_goods商品表,ab_order訂單表,ab_log日誌表
- 商品表
- 訂單表
- 日誌表 記錄秒殺資訊
三、程式碼
<?php namespace app\index\controller; use think\Controller; use think\Db; use think\cache\driver\Redis; class Miaosha extends Controller { private $redis = null; private $cachekey = null; //快取變數名 private $basket = []; //私有陣列,存放商品資訊 private $store = 50; /** * 購物車初始化,傳入使用者id */ public function __construct() { parent::__construct(); $this->redis = new \Redis(); // 例項化 $this->redis->connect('127.0.0.1','6379'); $this->redis->auth('zxf123456'); } /** * 秒殺初始化 */ public function Ms_init() { // 刪除快取列表 $this->redis->del($this->cachekey); $len = $this->redis->llen($this->cachekey); $count = $this->store - $len; for ($i=0; $i < $count; $i++) { // 向庫存列表推進50個,模擬50個商品庫存 $this->redis->lpush($this->cachekey,1); } echo "庫存初始化完成:".$this->redis->llen($this->cachekey); } /** * 秒殺入口 */ public function index() { $id = 1; //商品編號 if (empty($id)) { // 記錄失敗日誌 return $this->writeLog(0,'商品編號不存在'); } // 計算庫存列表長度 $count = $this->redis->llen($this->cachekey); // 先判斷庫存是否為0,為0秒殺失敗,不為0,則進行先移除一個元素,再進行資料庫操作 if ($count == 0) { //庫存為0 $this->writeLog(0,'庫存為0'); echo "庫存為0"; exit; }else{ // 有庫存 //先移除一個列表元素 $this->redis->lpop($this->cachekey); $ordersn = $this->build_order_no(); //生成訂單 $uid = rand(0,9999); //隨機生成使用者id $status = 1; // 再進行資料庫操作 $data = Db::table('ab_goods')->field('count,amount')->where('id',$id)->find(); //查詢商品 if (!$data) { return $this->writeLog(0,'該商品不存在'); } $insert_data = [ 'order_sn' => $ordersn, 'user_id' => $uid, 'goods_id' => $id, 'price' => $data['amount'], 'status' => $status, 'addtime' => date('Y-m-d H:i:s') ]; // 訂單入庫 $result = Db::table('ab_order')->insert($insert_data); // 自動減少一個庫存 $res = Db::table('ab_goods')->where('id',$id)->setDec('count'); if ($res) { echo "第".$count."件秒殺成功"; $this->writeLog(1,'秒殺成功'); }else{ echo "第".$count."件秒殺失敗"; $this->writeLog(0,'秒殺失敗'); } } } /** * 生成訂單號 */ public function build_order_no() { return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); } /** * 生成日誌 1成功 0失敗 */ public function writeLog($status = 1,$msg) { $data['count'] = 1; $data['status'] = $status; $data['addtime'] = date('Y-m-d H:i:s'); $data['msg'] = $msg; return Db::table('ab_log')->insertGetId($data); } }
四、壓力測試
使用apache壓力測試工具 AB 測試,模擬多使用者秒殺商品,模擬60秒內發起3000個請求,併發600次,秒殺50個庫存商品
AB測試相關引數說明
1 2 3 4 |
-r 指定接收到錯誤資訊時不退出程式
-t 等待響應的最大時間
-n 指定壓力測試總共的執行次數
-c 用於指定壓力測試的併發數
|
1.初始化50個庫存,執行ms_init方法
2.測試 命令列:E:\phpstudy_pro\Extensions\Apache2.4.39\bin>ab -r -t 60 -n 3000 -c 1000 http://gouwuche.zxf/index/miaosha/index
需要先進入apache的bin目錄下執行命令
3.檢測資料庫資料
日誌表狀態為1(秒殺成功)的資料有50人,訂單表裡的訂單數也是50條,商品表裡的商品數量變成了0(測試之前是50),商品秒殺成功完成!
如果不用redis而是直接用mysql的話,商品表訂單的數量count會變成負數,而秒殺成功的人數也多餘50人,訂單表裡的訂單數量也多餘50條(新測),下面是直接用Mysql的例子;
public function sqlMs() { $id = 1; //商品編號 $count = 50; $ordersn = $this->build_order_no(); //生成訂單 $uid = rand(0,9999); //隨機生成使用者id $status = 1; // 再進行資料庫操作 $data = Db::table('ab_goods')->field('count,amount')->where('id',$id)->find(); //查詢商品 // 查詢還剩多少庫存 $rs = Db::table('ab_goods')->where('id',$id)->value('count'); if ($rs <= 0) { $this->writeLog(0,'庫存為0'); }else{ $insert_data = [ 'order_sn' => $ordersn, 'user_id' => $uid, 'goods_id' => $id, 'price' => $data['amount'], 'status' => $status, 'addtime' => date('Y-m-d H:i:s') ]; // 訂單入庫 $result = Db::table('ab_order')->insert($insert_data); // 自動減少一個庫存 $res = Db::table('ab_goods')->where('id',$id)->setDec('count'); if ($res) { echo "第".$data['count']."件秒殺成功"; $this->writeLog(1,'秒殺成功'); }else{ echo "第".$data['count']."件秒殺失敗"; $this->writeLog(0,'秒殺失敗'); } } }