yii framework redis combined with php to achieve spike effect of example code

  • 2021-08-10 07:09:30
  • OfStack

No more nonsense, just post the code for everyone. The specific code is as follows:


<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
/**
* 
*/
class GoodsController extends Controller
{
public $enableCsrfValidation=false;
public function actionInfo()
{
$data=yii::$app->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 " Add failed ";
}
}
else
{
return $this->render('add');
}
}
public function actionOrder()
{
$redis=yii::$app->redis;
$count=$redis->lpop('goods');
if(empty($count))
{
echo " Insufficient stock ";die;
}
$res=yii::$app->db->createCommand("update goods set counts=counts-1 where id=1 and counts>0")->execute();
if($res)
{
echo " Spike success ";
}
else
{
echo " Spike failure ";
}
}
}
?>

Summarize


Related articles: