菜单

Yii framework redis combined with PHP to achieve the effect of the spike (example code)

2017年11月22日 - PHPer

Nonsense, say, directly to everyone posted code, the specific code 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 id=’$id'”) -> queryOne () where;

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 inventory”; die;

}

$res=yii:: $app-> db-> createCommand (“update goods set counts=counts-1 where and counts> 0”) -> execute () id=1;

If ($res)

{

Echo “spike success””;

}

Else

{

Echo “spike failure””;

}

}

}

&gt?;

发表评论

电子邮件地址不会被公开。