Home>Article>Backend Development> Example analysis of Yii framework redis+php to achieve flash sale effect

Example analysis of Yii framework redis+php to achieve flash sale effect

黄舟
黄舟 Original
2017-10-27 09:09:36 1425browse

Without further ado, I will post the code directly for you. The specific code is as follows:

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 "秒杀失败"; } } } ?>

The above is the detailed content of Example analysis of Yii framework redis+php to achieve flash sale effect. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn