高并发下PHP秒杀功能Redis队列

1. view界面: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>

2.初始化队列: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 ...@test18580";

?>

3.出队列: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 ...";
    //
}

4.运用ab.exe模拟大并发测试,库存量。

博主网址:www.imdam.com it博客之家

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值