一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

2021-11-02 15:15下頁、再停留 PHP教程

這篇文章主要介紹了Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解,代碼和步驟講解的很清楚,有需要的同學(xué)可以借鑒參考下

環(huán)境:wamp,redis

要求:安裝WAMP,Redis,以及為PHP安裝Redis擴(kuò)展

秒殺功能大致思路:獲取緩存列表的長度,如果長度(llen)等于0,就停止秒殺,即秒殺失敗,如果長度大于0,則繼續(xù)運(yùn)行,先從緩存中移除一個元素(lpop),再進(jìn)行數(shù)據(jù)庫操作(添加訂單表,商品庫存數(shù)量減一),如果再進(jìn)一個人秒殺,就再走一遍流程,循環(huán)往復(fù)。

一、安裝Redis擴(kuò)展

1.查看PHP版本信息

打開phpinfo.php,查看PHP版本,我的是PHP7.3.4,還有一個需要注意Architecture x64

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

2.下載擴(kuò)展文件

https://pecl.php.net/package/redis

https://pecl.php.net/package/igbinary

根據(jù)自己環(huán)境,選擇合適的版本

3.解壓

解壓下載的壓縮包,并把php_redis.dll、php_redis.pdb和php_igbinary.dll、php_igbinary.pdb四個文件,移至自己PHP版本對應(yīng)目錄下的ext文件夾下E:phpstudy_proExtensionsphpphp7.3.4ntsext

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

4.修改php.ini

添加如下代碼:

extension=php_igbinary.dll

extension=php_redis.dll

如果有這兩句可以把前面的分號刪掉,沒有就自己添加上,要注意順序,php_igbinary.dll 要在php_redis.dll 前面

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

5.重啟Apache

重啟后,再運(yùn)行phpinfo.php,查看是否安裝成功

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

二、數(shù)據(jù)結(jié)構(gòu)

一共三張表,ab_goods商品表,ab_order訂單表,ab_log日志表

商品表

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

訂單表

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

日志表 記錄秒殺信息

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

三、代碼

<?php
namespace appindexcontroller;
use thinkController;
use thinkDb;
use thinkcachedriverRedis;

class Miaosha extends Controller
{

 private $redis = null;
 private $cachekey = null; //緩存變量名
 private $basket = []; //私有數(shù)組,存放商品信息

 private $store = 50;

 /**
 * 購物車初始化,傳入用戶id
 */
 public function __construct()
 {
 parent::__construct();

 $this->redis = new Redis(); // 實(shí)例化
 $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++) { 

  // 向庫存列表推進(jìn)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,則進(jìn)行先移除一個元素,再進(jìn)行數(shù)據(jù)庫操作
 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); //隨機(jī)生成用戶id
  $status = 1;
  // 再進(jìn)行數(shù)據(jù)庫操作
  $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秒內(nèi)發(fā)起3000個請求,并發(fā)600次,秒殺50個庫存商品

AB測試相關(guān)參數(shù)說明

  • -r 指定接收到錯誤信息時不退出程序
  • -t 等待響應(yīng)的最大時間
  • -n 指定壓力測試總共的執(zhí)行次數(shù)
  • -c 用于指定壓力測試的并發(fā)數(shù)

1.初始化50個庫存,運(yùn)行ms_init方法

2.測試   命令行:

E:phpstudy_proExtensionsApache2.4.39in>ab -r -t 60 -n 3000 -c 1000 http://gouwuche.zxf/index/miaosha/index  

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

  3.檢測數(shù)據(jù)庫數(shù)據(jù)

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

Thinkphp5+Redis實(shí)現(xiàn)商品秒殺代碼實(shí)例講解

日志表狀態(tài)為1(秒殺成功)的數(shù)據(jù)有50人,訂單表里的訂單數(shù)也是50條,商品表里的商品數(shù)量變成了0(測試之前是50),商品秒殺成功完成!

如果不用redis而是直接用mysql的話,商品表訂單的數(shù)量count會變成負(fù)數(shù),而秒殺成功的人數(shù)也多余50人,訂單表里的訂單數(shù)量也多余50條(新測),下面是直接用Mysql的例子;

public function sqlMs()
 {
 $id = 1; //商品編號

 $count = 50;
 $ordersn = $this->build_order_no(); //生成訂單
 $uid = rand(0,9999); //隨機(jī)生成用戶id
 $status = 1;
 // 再進(jìn)行數(shù)據(jù)庫操作
 $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,"秒殺失敗");
  }
 }
 }

到此這篇關(guān)于Thinkphp5+Redis實(shí)現(xiàn)商品秒殺的文章就介紹到這了,更多相關(guān)Thinkphp5+Redis實(shí)現(xiàn)商品秒殺內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://www.cnblogs.com/zxf100/p/14173899.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品一区二区三区五区六区七区 | 亚洲精品中文字幕在线 | 久见久热 这里只有精品 | 99国产精品免费视频 | 美女班主任让我爽了一夜视频 | 小SAO货边洗澡边CAO你动漫 | 国产一级精品高清一级毛片 | 亚洲国产麻豆 | 1024国产精品视频观看 | 精品精品国产自在久久高清 | 99久久国产综合精品麻豆 | 亚洲不卡视频 | 日韩欧美一卡二区 | 精品无码乱码AV | 亚洲网站在线播放 | 欧美精品成人a多人在线观看 | 2019年国产高清情侣视频 | 欧美成人香蕉在线观看 | 国产99视频精品免费视频7 | 拔插拔插8x8x海外华人免费视频 | 四虎成人免费 | 日韩亚洲国产欧美精品 | 亚洲午夜精品久久久久久抢 | 四虎影院入口 | 亚洲一区二区三区不卡在线播放 | 国产女乱淫真高清免费视频 | 99久久香蕉国产线看观香 | 四虎影库紧急大通知 | 欧美老妪| 五月性| 成人猫咪maomiav永久网址 | 99久久九九 | 国产精品片 | 精品福利一区 | 五月一区二区久久综合天堂 | 日韩精品免费看 | 99热久久这里只有精品6国产网 | www日本在线观看 | 四虎成人免费观看在线网址 | 特黄特色大片免费视频播放 | 亚州春色 |