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

Thinkphp5+Redis实现商品秒杀代码实例讲解
⽬录
⼀、安装Redis扩展
⼆、数据结构
三、代码
四、压⼒测试
环境:wamp,redis
要求:安装WAMP,Redis,以及为PHP安装Redis扩展
秒杀功能⼤致思路:获取缓存列表的长度,如果长度(llen)等于0,就停⽌秒杀,即秒杀失败,如果长度⼤于0,则继续运⾏,先从缓存中移除⼀个元素(lpop),再进⾏数据库操作(添加订单表,商品库存数量减⼀),如果再进⼀个⼈秒杀,就再⾛⼀遍流程,循环往复。
⼀、安装Redis扩展
1.查看PHP版本信息
打开phpinfo.php,查看PHP版本,我的是PHP7.3.4,还有⼀个需要注意Architecture x64
2.下载扩展⽂件
根据⾃⼰环境,选择合适的版本
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);
// ⾃动减少⼀个库存
bf-269
$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测试相关参数说明
-r 指定接收到错误信息时不退出程序
-t 等待响应的最⼤时间
-n 指定压⼒测试总共的执⾏次数
-c ⽤于指定压⼒测试的并发数
1.初始化50个库存,运⾏ms_init⽅法
2.测试命令⾏:
  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,'秒杀失败');
}
}
}
到此这篇关于Thinkphp5+Redis实现商品秒杀的⽂章就介绍到这了,更多相关Thinkphp5+Redis实现商品秒杀内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

本文发布于:2024-09-23 19:18:02,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/153253.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:秒杀   商品   库存
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议