An example analysis of the effect of redis+php of yii frame on the second kill

Source: Internet
Author: User
Not much nonsense to say, directly to everyone to paste the code, the specific code is as follows:

<?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 "Add failed";}} Else{return $this->render (' Add ');}} Public Function Actionorder () {$redis =yii:: $app->redis; $count = $redis->lpop ('Goods '), if (Empty ($count)) {echo ' insufficient inventory ';d ie;} $res =yii:: $app->db->createcommand ("Update goods set counts=counts-1 where id=1 and counts>0")->execute (); if ($res) {echo "Second Kill succeeded";} Else{echo "second kill Failed";}}? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.