最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
yii框架redis结合php实现秒杀效果(实例代码)
时间:2022-06-24 14:38:31 编辑:袖梨 来源:一聚教程网
废话不多说了,直接给大家贴代码了,具体代码如下所示:
db->createCommand("select * from goods ")->queryAll();
return $this->render('index',['data'=>$data]);
}
Public function actionXx()
{
$id=yii::$app->request->get('id');
$data=yii::$app->db->createCommand("select * from goods where id='$id'")->queryOne();
return $this->render('info',['data'=>$data]);
}
Public function actionAdd()
{
$a=yii::$app->redis;
if (Yii::$app->request->isPost)
{
$data=yii::$app->request->post();
$res=yii::$app->db->createCommand()->insert('goods',$data)->execute();
if($res)
{
for ($i=0; $i <$data['counts'] ; $i++)
{
$a->lpush('goods',1);
}
return $this->redirect(array('goods/info'));
}
else
{
echo "添加失败";
}
}
else
{
return $this->render('add');
}
}
public function actionOrder()
{
$redis=yii::$app->redis;
$count=$redis->lpop('goods');
if(empty($count))
{
echo "库存不足";die;
}
$res=yii::$app->db->createCommand("update goods set counts=counts-1 where id=1 and counts>0")->execute();
if($res)
{
echo "秒杀成功";
}
else
{
echo "秒杀失败";
}
}
}
?>
相关文章
- 小米路由器网络不稳定的原因有哪些(如何解决小米路由器网络不稳定的问题) 08-18
- 吉利汽车2026上半年“量价利齐升”:营收破1700亿 海外销量激增158% 08-18
- 两个结构化数据自检工具的分工:语法校验与效果预览 08-18
- 元旦放假通知模板:清晰简洁的放假安排与温馨祝福 08-18
- 和平精英海岛 2.0 更新奖励是什么 08-18
- excel表格如何利用VBA代码插入交叉矩形图 08-18