1 Star 4 Fork 2

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

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
RedisKillModel.class.php 4.72 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 RedisKillModel extends KillCommonModel{
private $redis=null;
//redis链接地址
private $ip="127.0.0.1";
private $port="6379";
/**
* 链接redis
* connectRedis
* @param string $ip
* @param string $port
*/
public function connectRedis($ip="",$port="")
{
$this->redis=new Redis();
$this->redis->connect($this->ip,$this->port);
}
/**
* 抢购入口
* @param $id
* @param $userId
*/
public function RunPurchase($id,$userId){
$this->goodsIdBy=$id;
$this->userId=$userId;
if(!$this->run()){
return false;
}
}
/**
* 初始化商品库存
* @param $id
* @return bool
*/
public function manageRedisCount($id){
$this->checkClassExists();
$this->connectRedis();
$goodsObj=$this->goodsObj;
$where=array($this->goodsId=>$id);
$goodsInfo=$goodsObj->getRowGoodsInfo($where);
if($goodsInfo==null || !$goodsInfo){
$this->error="商品不存在";
return false;
}
if($goodsInfo[$this->counts]<1){
$this->error="商品没有库存";
return false;
}
$key = 'goods_list_'.$id;
if($count=$this->redis->llen($key)){
for($i=1;$i<=$count;$i++){
$this->redis->lpop($key);
}
}
for($i=1;$i<=$goodsInfo[$this->counts];$i++){
echo $this->redis->lpush($key,1);
}
}
private function run(){
if(!$this->checkClassExists()){
return false;
}
if($this->connectRedis()){
return false;
}
if(!$this->manage()){
return false;
}
}
public function setConfig($ip,$port){
$this->ip=$ip;
$this->port=$port;
}
//处理抢购的并发问题
private function manage(){
$goodsObj=$this->goodsObj;
$where=array($this->goodsId=>$this->goodsIdBy);
$goodsInfo=$goodsObj->getRowGoodsInfo($where);
if($goodsInfo==array() || !$goodsInfo){
$this->error="商品不存在";
return false;
}
if($this->checkUserBuyStatus()){
$this->error="你已经抢购过,该商品。";
return false;
}
if(!$this->checkGoodsCountExists()){
$this->error="库存已完";
return false;
}
$resul=$this->oderObj->createOrder($goodsInfo,$this->userId);
if($resul){
$result=$this->goodsObj->updateCount($goodsInfo);
if($result){
$this->error="抢购成功";
$this->setUserBuyInfo();
$this->redis->close();
return true;
}
}
$key = 'goods_list_'.$this->goodsIdBy;
$this->redis->lpush($key,1);
$this->error="网络超时";
return false;
}
private function setUserBuyInfo(){
$key="'goods_list_".$this->goodsIdBy."_".$this->userId."'";
$this->redis->set($key,$this->userId);
}
/*
* 检查商品的库存是否还存在
* @return bool
*/
private function checkGoodsCountExists(){
$key = 'goods_list_'.$this->goodsIdBy;
$count = $this->redis->lpop($key);
return $count;
}
/*
* 检查该用户是否已经购买该商品
* @return bool
*/
private function checkUserBuyStatus(){
$key="'goods_list_".$this->goodsIdBy."_".$this->userId."'";
$value=$this->redis->exists($key);
return $value;
}
/*
* 检查初始化配置是否存在
* @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

搜索帮助