当前位置:首页 > CMS教程 > Thinkphp > 列表

Thinkphp5+Redis实现商品秒杀代码实例讲解

发布:smiling 来源: PHP粉丝网  添加日期:2022-04-07 08:18:37 浏览: 评论:0 

这篇文章主要介绍了Thinkphp5+Redis实现商品秒杀代码实例讲解,代码和步骤讲解的很清楚,有需要的同学可以借鉴参考下。

目录

一、安装Redis扩展

二、数据结构

三、代码

四、压力测试

环境:wamp,redis

要求:安装WAMP,Redis,以及为PHP安装Redis扩展

秒杀功能大致思路:获取缓存列表的长度,如果长度(llen)等于0,就停止秒杀,即秒杀失败,如果长度大于0,则继续运行,先从缓存中移除一个元素(lpop),再进行数据库操作(添加订单表,商品库存数量减一),如果再进一个人秒杀,就再走一遍流程,循环往复。

一、安装Redis扩展

1.查看PHP版本信息

打开phpinfo.php,查看PHP版本,我的是PHP7.3.4,还有一个需要注意Architecture x64

Thinkphp5+Redis实现商品秒杀代码实例讲解

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

Thinkphp5+Redis实现商品秒杀代码实例讲解

4.修改php.ini

添加如下代码:

extension=php_igbinary.dll

extension=php_redis.dll

如果有这两句可以把前面的分号删掉,没有就自己添加上,要注意顺序,php_igbinary.dll 要在php_redis.dll 前面

Thinkphp5+Redis实现商品秒杀代码实例讲解

5.重启Apache

重启后,再运行phpinfo.php,查看是否安装成功

Thinkphp5+Redis实现商品秒杀代码实例讲解

二、数据结构

一共三张表,ab_goods商品表,ab_order订单表,ab_log日志表

商品表

Thinkphp5+Redis实现商品秒杀代码实例讲解

订单表

Thinkphp5+Redis实现商品秒杀代码实例讲解

日志表 记录秒杀信息

Thinkphp5+Redis实现商品秒杀代码实例讲解

三、代码

  1. <?php 
  2. namespace app\index\controller; 
  3. use think\Controller; 
  4. use think\Db; 
  5. use think\cache\driver\Redis; 
  6.  
  7. class Miaosha extends Controller 
  8. { 
  9.  
  10.  private $redis = null; 
  11.  private $cachekey = null; //缓存变量名 
  12.  private $basket = []; //私有数组,存放商品信息 
  13.  
  14.  private $store = 50; 
  15.  
  16.  /** 
  17.  * 购物车初始化,传入用户id 
  18.  */ 
  19.  public function __construct() 
  20.  { 
  21.  parent::__construct(); 
  22.  
  23.  $this->redis = new \Redis(); // 实例化 
  24.  $this->redis->connect('127.0.0.1','6379'); 
  25.  $this->redis->auth('zxf123456'); 
  26.  
  27.  } 
  28.  
  29.  /** 
  30.  * 秒杀初始化 
  31.  */ 
  32.  public function Ms_init() 
  33.  { 
  34.  // 删除缓存列表 
  35.  $this->redis->del($this->cachekey); 
  36.  
  37.  $len = $this->redis->llen($this->cachekey); 
  38.  $count = $this->store - $len; 
  39.  
  40.  for ($i=0; $i < $count; $i++) {  
  41.  
  42.  // 向库存列表推进50个,模拟50个商品库存 
  43.  $this->redis->lpush($this->cachekey,1); 
  44.  } 
  45.  
  46.  echo "库存初始化完成:".$this->redis->llen($this->cachekey); 
  47.  } 
  48.    
  49.  
  50.  /** 
  51.  * 秒杀入口 
  52.  */ 
  53.  public function index() 
  54.  { 
  55.  $id = 1; //商品编号 
  56.    
  57.  if (emptyempty($id)) { 
  58.  // 记录失败日志 
  59.  return $this->writeLog(0,'商品编号不存在');  
  60.  } 
  61.  
  62.  // 计算库存列表长度 
  63.  $count = $this->redis->llen($this->cachekey); 
  64.  
  65.  // 先判断库存是否为0,为0秒杀失败,不为0,则进行先移除一个元素,再进行数据库操作 
  66.  if ($count == 0) { //库存为0 
  67.  
  68.  $this->writeLog(0,'库存为0'); 
  69.  echo "库存为0"; 
  70.  exit; 
  71.  
  72.  }else{ 
  73.  // 有库存 
  74.  //先移除一个列表元素 
  75.  $this->redis->lpop($this->cachekey); 
  76.  
  77.  $ordersn = $this->build_order_no(); //生成订单 
  78.  $uid = rand(0,9999); //随机生成用户id 
  79.  $status = 1; 
  80.  // 再进行数据库操作 
  81.  $data = Db::table('ab_goods')->field('count,amount')->where('id',$id)->find(); //查找商品 
  82.  
  83.  if (!$data) { 
  84.  return $this->writeLog(0,'该商品不存在'); 
  85.  } 
  86.  
  87.  $insert_data = [ 
  88.  'order_sn' => $ordersn, 
  89.  'user_id' => $uid, 
  90.  'goods_id' => $id, 
  91.  'price' => $data['amount'], 
  92.  'status' => $status, 
  93.  'addtime' => date('Y-m-d H:i:s') 
  94.  ]; 
  95.  
  96.  // 订单入库 
  97.  $result = Db::table('ab_order')->insert($insert_data); 
  98.  // 自动减少一个库存 
  99.  $res = Db::table('ab_goods')->where('id',$id)->setDec('count'); 
  100.  
  101.  if ($res) { 
  102.  echo "第".$count."件秒杀成功"; 
  103.  $this->writeLog(1,'秒杀成功'); 
  104.  }else{ 
  105.  echo "第".$count."件秒杀失败"; 
  106.  $this->writeLog(0,'秒杀失败'); 
  107.  } 
  108.  } 
  109.  } 
  110.  
  111.  /** 
  112.  * 生成订单号 
  113.  */ 
  114.  public function build_order_no() 
  115.  { 
  116.  return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); 
  117.  } 
  118.  
  119.  /** 
  120.  * 生成日志 1成功 0失败 
  121.  */ 
  122.  public function writeLog($status = 1,$msg) 
  123.  { 
  124.  $data['count'] = 1; 
  125.  $data['status'] = $status; 
  126.  $data['addtime'] = date('Y-m-d H:i:s'); 
  127.  $data['msg'] = $msg; 
  128.  return Db::table('ab_log')->insertGetId($data); 
  129.  } 
  130.  
  131. } 

四、压力测试

使用apache压力测试工具 AB 测试,模拟多用户秒杀商品,模拟60秒内发起3000个请求,并发600次,秒杀50个库存商品

AB测试相关参数说明

-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 

Thinkphp5+Redis实现商品秒杀代码实例讲解

3.检测数据库数据

Thinkphp5+Redis实现商品秒杀代码实例讲解

日志表状态为1(秒杀成功)的数据有50人,订单表里的订单数也是50条,商品表里的商品数量变成了0(测试之前是50),商品秒杀成功完成!

如果不用redis而是直接用mysql的话,商品表订单的数量count会变成负数,而秒杀成功的人数也多余50人,订单表里的订单数量也多余50条(新测),下面是直接用Mysql的例子;

  1. public function sqlMs() 
  2.  { 
  3.  $id = 1; //商品编号 
  4.  
  5.  $count = 50; 
  6.  $ordersn = $this->build_order_no(); //生成订单 
  7.  $uid = rand(0,9999); //随机生成用户id 
  8.  $status = 1; 
  9.  // 再进行数据库操作 
  10.  $data = Db::table('ab_goods')->field('count,amount')->where('id',$id)->find(); //查找商品 
  11.  
  12.  // 查询还剩多少库存 
  13.  $rs = Db::table('ab_goods')->where('id',$id)->value('count'); 
  14.  if ($rs <= 0) { 
  15.    
  16.  $this->writeLog(0,'库存为0'); 
  17.  }else{ 
  18.  
  19.  $insert_data = [ 
  20.  'order_sn' => $ordersn, 
  21.  'user_id' => $uid, 
  22.  'goods_id' => $id, 
  23.  'price' => $data['amount'], 
  24.  'status' => $status, 
  25.  'addtime' => date('Y-m-d H:i:s') 
  26.  ]; 
  27.  
  28.  // 订单入库 
  29.  $result = Db::table('ab_order')->insert($insert_data); 
  30.  // 自动减少一个库存 
  31.  $res = Db::table('ab_goods')->where('id',$id)->setDec('count'); 
  32.  
  33.  if ($res) { 
  34.  echo "第".$data['count']."件秒杀成功"; 
  35.  $this->writeLog(1,'秒杀成功'); 
  36.  }else{ 
  37.  echo "第".$data['count']."件秒杀失败"; 
  38.  $this->writeLog(0,'秒杀失败'); 
  39.  } 
  40.  } 
  41.  }<br type="_moz" data-filtered="filtered">

Tags: Thinkphp5+Redis

分享到: