/**
* Site controller
*/
class MiaoController extends Controller
{
public function actionIndex()
{
// $this->actionRedis();die;
$this->actionAll();
$model = new miao();
$data = $model->find()->all();
return $this->render('index',['data'=>$data]);
}
public function actionMiaosha()
{
$model = new miao();
$id = $_GET['id'];
if(Yii::$app->request->post()){
$id = $_POST['Miao']['id'];
$redis = $this->actionRedis();
$nu = $redis->lpop($id);
if(!$nu){
echo "失败";die;
}
$list = $model->find()->where(['id'=>$id])->one();
// var_dump($list);die;
$list->num = $list['num']-1;
$list->save();
}
return $this->render('miaosha',['model'=>$model,'id'=>$id]);
}
//redis
public function actionRedis()
{
$redis = new \Redis();
$redis->connect('127.0.0.1',6379,1);
return $redis;
}
//所有商品id如队列
public function actionAll()
{
$redis = $this->actionRedis();
$model = new miao();
$data = $model->find()->asArray()->all();
foreach ($data as $key => $value) {
$re = $redis->lpush($value['id'],1);
}
}
}
* Site controller
*/
class MiaoController extends Controller
{
public function actionIndex()
{
// $this->actionRedis();die;
$this->actionAll();
$model = new miao();
$data = $model->find()->all();
return $this->render('index',['data'=>$data]);
}
public function actionMiaosha()
{
$model = new miao();
$id = $_GET['id'];
if(Yii::$app->request->post()){
$id = $_POST['Miao']['id'];
$redis = $this->actionRedis();
$nu = $redis->lpop($id);
if(!$nu){
echo "失败";die;
}
$list = $model->find()->where(['id'=>$id])->one();
// var_dump($list);die;
$list->num = $list['num']-1;
$list->save();
}
return $this->render('miaosha',['model'=>$model,'id'=>$id]);
}
//redis
public function actionRedis()
{
$redis = new \Redis();
$redis->connect('127.0.0.1',6379,1);
return $redis;
}
//所有商品id如队列
public function actionAll()
{
$redis = $this->actionRedis();
$model = new miao();
$data = $model->find()->asArray()->all();
foreach ($data as $key => $value) {
$re = $redis->lpush($value['id'],1);
}
}
}