Method of stealing code function in concurrency based on PHP using lock

Source: Internet
Author: User
Tags flock php language
This article is based on the PHP language using the lock implementation of the concurrency of the grab code function, the specific time period open grab code does not allow open code duplication, this article describes the very detailed, need to refer to the friend

Demand: Grab the code function

Requirements:

1, a specific period of time to open the grab code;

2, each time period release code is limited;

3, each code is not allowed to repeat;

Realize:

1, without regard to concurrency in the case of implementation:

function Get_code ($len) {$CHAR _arr = array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ' , ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' X ', ' Y ', ' Z ', ' W ', ' S ', ' R ', ' T '), $CHAR _arr_len = count ($CHAR _arr)-1; $code = "; while ( --$len > 0) {$code. = $CHAR _arr[rand (0, $CHAR _arr_len)];} return $code;} $pdo = new PDO (' Mysql:host=localhost;dbname=ci_test ', ' root ', ' root ');//Query The current time the number of verified codes issued $CODE_NUM_RS = $pdo->query (" Select COUNT (*) as Sum from Code_test "), $code _num_arr = $code _num_rs->fetch (PDO::FETCH_ASSOC), $code _num = $code _num_ arr[' Sum '];if ($code _num < 1) {<br> sleep (2);//pause 2 seconds $code = Get_code (6); Var_dump ($pdo->query ("INSERT into Co De_test (code,create_time) VALUES (' $code ', ". Time ().") );}

The above code defaults to meet the current open time, and code is not duplicated;

The process is not considered in the case of concurrency:

1) Select query the current database issued the number of verification code;

2) If there is a quota, generate the verification code, insert into the database, return the verification code to the client;

3) If it is full, the return prompt, there is no quota;

2, the implementation of concurrency:

So look at the results of the above code in the concurrency scenario:

Test concurrency, you can use Apache benchmark to test, Apache Benchmark is Apache's HTTP Server performance evaluation tool, through CMD into the Apche bin directory, through the AB command, such as: ab-c Concurrent Quantity-n total traffic URL

The code is as follows:


Cb-c 100-n http://localhost/php_mulit.php

This is 100 user colleagues to rob 1 places, in the query, each user query to also have a quota, will go to generate a verification code, insert the database, return the verification code, which caused the verification code to send more. In fact, after running the command, the database has more than 13 records, not one.

How to avoid this situation?

This process can be locked by an exclusive lock to ensure that only one process is running at any time during this judgment process. The implementation is as follows:

Generate Code function Get_code ($len) {$CHAR _arr = array (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' X ', ' Y ', ' Z ', ' W ', ' S ', ' R ', ' T '), $CHAR _arr_len = count ($CHAR _arr)-1; $code = "; while (--$len > 0) {$code. = $CHAR _arr[rand (0, $CHAR _arr_len)];} return $code;} $pdo = new PDO (' Mysql:host=localhost;dbname=ci_test ', ' root ', ' root '), $fp = fopen (' lock.txt ', ' r '),//Lock the procedure by an exclusive lock if ( Flock ($FP, LOCK_EX)) {//Query the current time the number of verified codes issued $CODE_NUM_RS = $pdo->query ("Select COUNT (*) as Sum from Code_test"); $code _num_ arr = $code _num_rs->fetch (PDO::FETCH_ASSOC), $code _num = $code _num_arr[' Sum '];if ($code _num < 1) {sleep (2); $code = Get_code (6); Var_dump ($pdo->query ("INSERT into Code_test (code,create_time) VALUES (' $code ',". Time ().) ")); Flock ($FP, Lock_un); fclose ($FP);}

Use the Flock function to lock the process.

For more flock information, refer to the PHP manual: http://php.net/manual/zh/function.flock.php

Run again

The code is as follows:


Cb-c 100-n http://localhost/php_mulit.php

The database adds only one record to ensure that the data is correct in the concurrency situation.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

Related Article

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.