最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
yii框架redis结合php实现秒杀效果(实例代码)
时间:2017-11-15 编辑:猪哥 来源:一聚教程网
废话不多说了,直接给大家贴代码了,具体代码如下所示:
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 "秒杀失败"; } } } ?>
-
上一个: PHP对象的浅复制与深复制的实例详解
-
下一个: PHP 中魔术常量的实例详解
相关文章
- Yii框架参数配置文件params用法实例分析 09-11
- Yii 2.0中场景的使用教程 06-05
- Yii 2 创建 migration 给表添加字段的例子 08-01
- YII中assets的使用示例 05-24
- Yii CDBCriteria常用方法 01-05
- YII框架中save()和insert()的用法差异 01-05