>  기사  >  백엔드 개발  >  플래시 세일 효과를 달성하기 위한 Yii 프레임워크 redis+php 분석 예시

플래시 세일 효과를 달성하기 위한 Yii 프레임워크 redis+php 분석 예시

黄舟
黄舟원래의
2017-10-27 09:09:361449검색

더 이상 헛소리하지 마세요. 구체적인 코드는 다음과 같습니다.

<?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(&#39;index&#39;,[&#39;data&#39;=>$data]);
}
Public function actionXx()
{
$id=yii::$app->request->get(&#39;id&#39;);
$data=yii::$app->db->createCommand("select * from goods where id=&#39;$id&#39;")->queryOne();
return $this->render(&#39;info&#39;,[&#39;data&#39;=>$data]);
}
Public function actionAdd()
{
$a=yii::$app->redis;
if (Yii::$app->request->isPost)
{
$data=yii::$app->request->post();
$res=yii::$app->db->createCommand()->insert(&#39;goods&#39;,$data)->execute();
if($res)
{
for ($i=0; $i <$data[&#39;counts&#39;] ; $i++)
{ 
$a->lpush(&#39;goods&#39;,1);
}
return $this->redirect(array(&#39;goods/info&#39;));
}
else
{
echo "添加失败";
}
}
else
{
return $this->render(&#39;add&#39;);
}
}
public function actionOrder()
{
$redis=yii::$app->redis;
$count=$redis->lpop(&#39;goods&#39;);
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 "秒杀失败";
}
}
}
?>

위 내용은 플래시 세일 효과를 달성하기 위한 Yii 프레임워크 redis+php 분석 예시의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.