for a long time not to sort out the article, idle writing article records under the record of php+redis to realize the commercial city second kill function.

1, install redis, according to its own version of PHP to install the corresponding redis extension (this step is simple to describe)

1.1. install php_igbinary.dll, php_redis.dll extended here need to pay attention to your PHP version:

1.2.php.ini extension=php_igbinary.dll extension=php_redis.dll two new document; at the extended

ok here has completed the first step redis environment to build complete look at phpinfo src= "//files.jb51.net/file_images/article/201802/201802080842172.png" >

2, the project in the actual use of redis

2.1. as the first step of redis configuration parameters, the installation of redis

 < 6379: the default port for PHP database configuration; / * *? Return / array ('DATA_CACHE_PREFIX'=>'Redis_','Redis','DATA_CACHE_TYPE'=> / / prefix cache; / / default dynamic cache is Redis false,'DATA_CACHE_TIMEOUT' =>'REDIS_RW_SEPARATE'=> true, //Redis read and write separate true open'REDIS_HOST'=>'127.0.0.1', //redis server IP, Taiwan by commas; read and write separate open, first wrote the other. [random] responsible for reading;'REDIS_PORT'=>'6379','300','REDIS_TIMEOUT'=> / / port number; / / false / /'REDIS_PERSISTENT'=> timeout; whether the long connecting short false= connection'REDIS_AUTH'=> ', //AUTH authentication password); > redis:

?
 / * * * * * @return private @access redis connection resource * @author bieanju * / private function 

the actual function in

2.2. ConnectRedis (Redis) {$redis=new ($redis->);; connect (C ("REDIS_HOST"), C ("REDIS_PORT")); return $redis;}

2.3. is the core problem of seckill stock purchase will not exceed in the concurrent case, the key is treated so thinking is the first step in seckill class the first to do some basic data generation:

 / / now initialization definition to use redis behind the parameters of public function (_initialize) {parent:: _initialize (); $goods_id = I ("goods_id",'0','intval'); if ($goods_id) {$this-> goods_id = $goods_id; $this-> = goods_; user_queue_key ".$goods_id." _user "; / / the current commodity queue user $this-> goods_number_key =" goods ".$goods_id; / / the current inventory queue} $th Is-> user_id = $this-> user_id? $this-> user_id: $_SESSION['uid'];} 

2.4. second step is the key, the user enters the product details page before the current inventory queue in redis as follows:

 / * * * access products before the product inventory queue public * @access * @author * bieanju public function (_before_detail) {$where['goods_id'] = $this-> goods_id; $where['start_time'] = array ("lt"); $where['end_time'] (time) = array ("GT"); $goods (time) = M ("goods") -> where ($where) -> field ('goods_num, start_time, -> find (end_time') $goods; & &)! "(error; $this-> current seckill has ended! "($goods['goods_num']); if > $goods['order_num']) {$redis = $this-> $getUserRedis = $redis-> (connectRedis); hGetAll (" {$this-> user_queue_key} "); $gnRedis = $redis-> llen ({$this-> goods_number_key}"); / * if there is no member in the queue inventory / if (! Count ($getUserRedis) & &! $gnRedis) {for ($i = 0; $i < $goods['goods_num'] $i; lpush (+ +) {$redis-> {$this-> "goods_number_key}", 1);}} $resetRedis = $redis-> llen ({$this-> goods_number_key}); if (! $resetRedis) {$this-> error ("the system is busy, please try later panic buying! ");}}else{$this-> error (" the current product has been killed! ");}}

the next thing to do is to use ajax to handle asynchronous users click on the buy button for data in accordance with the conditions to buy into the queue (if the current user is not in the current user queue and pop queue to enter an inventory queue, if we throw in,):

 / * * * panic buying the goods before processing whether the current members to enter the queue public * @author bieanju * / * @access public function (goods_number_queue) {$this-> user_id! & & $this-> ajaxReturn (array (" status "= & gt;" -1 "," MSG "=>" please login ")); $model = M (" flash_sale "); $where['goods_id'] = $this-> $goods_info = goods_id; $model-> where ($where) -> ($goods_info); find; & & $this->! Error (sorry;" current business The product does not exist or has gone down! "); / * * / redis queue $redis = $this-> (connectRedis); / * * / enter the 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) {} / / to determine whether the user has been in the queue (if $redis-> hGet (" {$this->! "; user_queue_key}, $this-> user_id)) {/ / $userinfo = array is inserted into the panic buying user information (" user_id "=> $this-> user_id, => time (" create_time "); $redis-> (hSet);" {$this-> user_queue_key} ", $this-> user_id, serialize; $this-& ($userinfo)); Gt; ajaxReturn (array ("status =>"; "1"));}else{$modelCart = M ("cart"); $condition['user_id'] = $this-> $condition['goods_id'] = user_id; $this-> goods_id; $condition['prom_type'] = 1; $cartlist = $modelCart- > where ($condition) -> (count); if ($cartlist > 0 {$this-> (array); ajaxReturn ("status =>"; "2"); $this-> (}else{); ajaxReturn array ("status =>"; "1")}});}else{$this-> ajaxReturn (array ("status =>"; "-1", "MSG" => "the system is busy. Please try again! "));}} 

add a debug function, delete the specified queue value:

 public function (clearRedis) {set_time_limit (0); $redis = $this-> //$Rd = $redis-> (connectRedis); del (" {$this-> user_queue_key} "); $Rd = $redis-> hDel (" goods49 ". The user 'id''); $a = $redis-> hGet (" goods_49_user "," id'if (user);! $a) {dump} ($a); if ($Rd = = 0) {exit ("Redis queue has been released! ");}} 

goes to the core of the basic time seckill is finished, the details still need to yourself to improve, like a shopping cart here as well as the processing of orders, well began to run their own AB program using Apache to simulate simple concurrent test as follows:

https://files.jb51.net/file_images/article/201802/20182884458630.jpg? 20181884510 runs, I cleaned up running redis without any reaction, the less important step step is to open the redis service, please refer to the redis service management tools, their system under a redisbin_x32 or redisbin_x64 click redis-server.exe, OK has so far completed as below:

 src=

above is the entire contents, want to help everyone to learn, I hope you will support a script.

This paper fixed link:http://www.script-home.com/phpredis-to-realize-the-commercial-citys-second-kill-function.html | Script Home | +Copy Link

Article reprint please specify:Php+redis to realize the commercial city’s second kill function | Script Home

You may also be interested in these articles!