代码拉取完成,页面将自动刷新
<?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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。