thinkphp+redis+ queue

Source: Internet
Author: User
Tags install redis redis server

1, install Redis, install the corresponding Redis extension according to your PHP version (this step is a simple description)

1.1, install the Php_igbinary.dll,php_redis.dll extension here to pay attention to your PHP version

  

1.2,php.ini file added extension=php_igbinary.dll;extension=php_redis.dll two extension

OK, here's the first step. Redis environment Setup Complete See Phpinfo

The actual use of Redis in the project

2.1, the first step is to configure the Redis parameters as follows, the default port for Redis installation is 6379: 

/*Database Configuration*/return Array(    ' Data_cache_prefix ' = ' redis_ ',//Cache prefix' Data_cache_type ' = ' Redis ',//The default dynamic cache is Redis' Data_cache_timeout ' =false, ' redis_rw_separate ' =true,//Redis Read-write separation true on' Redis_host ' = ' 127.0.0.1 ',//Redis server IP, multiple units separated by commas, read/write separation is on, the first one is responsible for writing, and other [random] responsible for reading;' Redis_port ' = ' 6379 ',//Port number' Redis_timeout ' = ' 300 ',//Timeout period' Redis_persistent ' =false,//whether long connection false= short connection' Redis_auth ' = ',//Auth Authentication Password);?>

2.2, use Redis in the actual function:

/* *        * redis Connection        * @access Private        * @return Resource        * @author bieanju            */ Privatefunction  Connectredis () {        $redis=new  \redis ();         $redis->connect (C ("Redis_host"), C ("Redis_port"));                return $redis ;    }

2.3, the core of the second kill is in the case of large concurrency will not exceed the purchase of inventory, this is the key to processing so the idea is the first step in the second kill class first do some basic data generation:

//now initialize the inside to define the Redis parameters to be used behind Public function_initialize () {Parent::_initialize (); $goods _id= I ("goods_id", ' 0 ', ' intval '); if($goods _id){            $this->goods_id =$goods _id; $this->user_queue_key = "Goods_".$goods _id." _user ";//user status for current commodity queue            $this->goods_number_key = "goods".$goods _id;//Inventory queue for the current item        }        $this->user_id =$this->user_id?$this->USER_ID:$_session[' UID ']; }

2.4, the second step is the key, the user enters the Product Details page before the inventory of the current product to the queue into Redis as follows:

/** * The current product Inventory queue * @access public * Before accessing the product @author Bieanju*/     Public function_before_detail () {$where[' goods_id '] =$this-goods_id; $where[' start_time '] =Array("LT", Time()); $where[' end_time '] =Array("GT", Time()); $goods= M ("goods")->where ($where)->field (' Goods_num,start_time,end_time ')find (); !$goods&&$this->error ("The current Kill is over!") "); if($goods[' Goods_num '] >$goods[' Order_num ']){            $redis=$this-Connectredis (); $getUserRedis=$redis->hgetall ("{$this->user_queue_key} "); $gnRedis=$redis->llen ("{$this->goods_number_key} "); /*if no members come in the queue inventory*/            if(!Count($getUserRedis) &&!$gnRedis){                             for($i= 0;$i<$goods[' Goods_num '];$i++) {                    $redis->lpush ("{$this->goods_number_key} ", 1); }            }            $resetRedis=$redis->llen ("{$this->goods_number_key} "); if(!$resetRedis){                $this->error ("The system is busy, please snap it later!") "); }        }Else{            $this->error ("The current product has been killed!") "); }             }

The next thing to do is to use Ajax to asynchronously process the user clicks the Buy button to enter the queued queue of the purchase (if the current user is not in the queue of the current product user and POPs into a stock queue, if it is thrown, if it is on)

/** * to deal with the current membership in the queue before the purchase of goods * @access public * @author Bieanju*/     Public functionGoods_number_queue () {!$this->user_id &&$this->ajaxreturn (Array("Status" = "1", "msg" = "Please login First")); $model= M ("Flash_sale"); $where[' goods_id '] =$this-goods_id; $goods _info=$model->where ($where),find (); !$goods _info&&$this->error ("Sorry" the current product does not exist or has been off the shelf!) "); /*Redis Queue*/          $redis=$this-Connectredis (); /*Enter Queue*/        $goods _number_key=$redis->llen ("{$this->goods_number_key} "); if(!$redis->hget ("{$this->user_queue_key} ",$this-user_id)) {            $goods _number_key=$redis->lpop ("{$this->goods_number_key} "); }                 if($goods _number_key){            //determine if the user is already in the queue            if(!$redis->hget ("{$this->user_queue_key} ",$this-user_id)) {                //Insert snapped user information                $userinfo=Array(                    "USER_ID" =$this->user_id, "create_time" = Time()                ); $redis->hset ("{$this->user_queue_key} ",$this-&GT;USER_ID,Serialize($userinfo)); $this->ajaxreturn (Array("Status" = "1")); }Else{                $modelCart= M ("Cart"); $condition[' user_id '] =$this-user_id; $condition[' goods_id '] =$this-goods_id; $condition[' prom_type '] = 1; $cartlist=$modelCart->where ($condition),Count(); if($cartlist> 0){                    $this->ajaxreturn (Array("Status" = "2")); }Else{                                     $this->ajaxreturn (Array("Status" = "1")); }                             }                     }Else{            $this->ajaxreturn (Array("Status" = "-1", "msg" and "=" system busy, please try again!) ")); }    }

Attach a debug function to delete the specified queue value:

 Public function Clearredis () {         set_time_limit(0);          $redis $this-Connectredis ();          // $Rd = $redis->del ("{$this->user_queue_key}");         $Rd $redis->hdel ("goods49", ' User ID ');         ' User ID ');         if (! $a) {             dump ($a);         }                  if ($Rd = = 0) {              exit ("Redis queue released!) ");                    }     }

thinkphp+redis+ queue

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.