您现在的位置是:网站首页 > 学无止境

高并发下秒杀功能Redis队列

ImdaM 2020-04-15 14:11:31 0人围观
简介高并发下秒杀功能Redis队列PHP商城活动

高并发下秒杀功能Redis队列PHP商城活动

视图界面代码:test.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<center>
<img src="xianhua.png"/>
</center>
<center>
<span>秒杀开启了</span>
<p>库存量:10 </p>
<p>价格:999元 </p>
</center>
<center>
<button>秒杀</button>
</center>
</body>
</html>

活动开启 秒杀 名额  代码:start-ms.php

<?php
//1. 开启秒杀活动 创建一个队列
$rd=new Redis(); //

// 2. 连接Redis
$rd->connect("127.0.0.1","6379");

// 3. 创建队列 lpush 从left 往队列里 添加值

for($i=1;$i<4;$i++){
$rd->lpush("mslist",$i);
}

echo "hzminc.com write ms start ...";

?>

秒杀开始 出队列 代码:doms.php

<?php
// 点击秒杀按钮 执行的php 脚本 处理秒杀功能
$rd=new Redis(); //
// 2. 连接Redis
$rd->connect("127.0.0.1","6379");
// 3. 出队列 rpop
$listres=$rd->rpop("mslist"); // false

$listlen= $rd->llen("mslist"); //取队列里 长度

if($listres==false && $listlen ==0 ){
echo "ms over...";
}else{
//秒杀功能后:
//1. 生成订单号 时间+随机数+uniqid
$orderNum = time() . rand(1,1000) . uniqid();
echo "orderNum: is ".$orderNum;
echo "<br/>";
//2. 修改mysql库存量
$db=new PDO("mysql:host=127.0.0.1;dbname=test","root","root");
$res=$db->exec("update `redis_goods` set `sp_nums` = `sp_nums` -1 where id = 1 ");
echo "MS success ...";
//
}

压力工具测试:运用ab.exe模拟大并发测试,库存量。


上一篇:Thinkphp3.2.3 thinkphp5.0 TP3.2 tp5.1区别

下一篇:抗击疫情,美国全面打击华为?

文章评论