廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
<?phpnamespace 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 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 "秒殺失敗";}}}?>總結(jié)
以上所述是小編給大家介紹的yii框架redis結(jié)合php實(shí)現(xiàn)秒殺效果(實(shí)例代碼),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對VeVb武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選