1 Star 4 Fork 2

蓝天白云11846/PHP操作Redis-消息队列-秒杀功能实现

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MysqlKill.class.php 3.66 KB
一键复制 编辑 原始数据 按行查看 历史
蓝天白云11846 提交于 2017-12-01 18:00 . 修改 并测试 redis并发
<?php
include_once("KillCommonModel.class.php");
include_once("Order.class.php");
include_once("Goods.class.php");
class MysqlKill extends KillCommonModel{
public $db="goods";
public $mysqlIp="localhost";
public $userDb="root";
public $userPwd="root";
private $conn=null;
public function setConfig($db,$mysqlIp,$userDb,$userPwd){
$this->db=$db;
$this->mysqlIp=$mysqlIp;
$this->userDb=$userDb;
$this->userPwd=$userPwd;
}
/**
* 抢购入口
* @param $id
* @param $userId
*/
public function RunPurchase($id,$userId){
$this->goodsIdBy=$id;
$this->userId;
$this->Run();
}
/*
* 运行抢购所需要的类及方法
*/
private function Run(){
if(!$this->checkClassExists()){
return false;
}
$this->getCoon();
$this->Manage();
}
/*
* 链接数据
*/
private function getCoon(){
$conn=$this->conn=mysql_connect($this->mysqlIp,$this->userDb,$this->userPwd);
mysql_select_db($this->db,$conn);
mysql_query("set names utf8");
}
/*
* 购买商品业务 下单
*/
private function Manage(){
$goodsObj=$this->goodsObj;
$where=array($this->goodsId=>$this->goodsIdBy);
$goodsInfo=$goodsObj->getRowGoodsInfo($where);
if($goodsInfo==null || !$goodsInfo){
$this->error="商品不存在";
return false;
}
//此时这条记录被锁住,其它事务必须等待此次事务提交后才能执行
mysql_query("BEGIN"); //开始事务
$sql="select '.$this->counts.' from '.$this->goodsTable.' where '.$this->goodsId.'='.$this->goodsIdBy.' FOR UPDATE";
$rs=mysql_query($sql,$this->conn);
$row=mysql_fetch_assoc($rs);
if($row[$this->counts]<=0){
mysql_query("ROLLBACK");
mysql_query("COMMIT");//事务提交即解锁
$this->error="商品库存不足";
return false;
}
$resul=$this->orderInfo=$this->oderObj->createOrder($goodsInfo,$this->userId);
if(!$resul){
mysql_query("COMMIT");//事务提交即解锁
$this->error="购买超时,请重试 ";
return false;
}
$sql="update ih_store set {$this->counts}={$this->counts}-1 where {$this->goodsId}='".$this->goodsIdBy."'";
$store_rs=mysql_query($sql,$this->conn);
if(!mysql_affected_rows()){
mysql_query("ROLLBACK");
mysql_query("COMMIT");
$this->error="购买超时,请重试 ";
return false;
}
mysql_query("COMMIT");
return true;
}
/*
* 检查初始化配置是否存在
* @return bool
*/
private function checkClassExists(){
if(!class_exists($this->goodsObj)){
$this->error="商品表类不存在";
return false;
}
$this->goodsObj=$goodsObj=new $this->goodsObj();
if(!method_exists($goodsObj,"getRowGoodsInfo")){
$this->error="商品表类中getRowGoodsInfo方法不存在";
return false;
}
if(!method_exists($goodsObj,"updateCount")){
$this->error="商品表类中updateCount方法不存在";
return false;
}
if(!class_exists($this->oderObj)){
$this->error="订单表类不存在";
return false;
}
$this->oderObj=$orderObj=new $this->oderObj();
if(!method_exists($orderObj,"createOrder")){
$this->error="订单表类中createOrder方法不存在";
return false;
}
return true;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/huzhengping_admin/PHPCaoZuoRedis-XiaoXiDuiLie-MiaoShaGongNenShiXian.git
git@gitee.com:huzhengping_admin/PHPCaoZuoRedis-XiaoXiDuiLie-MiaoShaGongNenShiXian.git
huzhengping_admin
PHPCaoZuoRedis-XiaoXiDuiLie-MiaoShaGongNenShiXian
PHP操作Redis-消息队列-秒杀功能实现
master

搜索帮助