PHP _php instance of grabbing code function in concurrent case by adding lock

Source: Internet
Author: User
Tags benchmark current time flock rand sleep

Demand: Scramble Code function

Requirements:

1, a specific period of time before the opening grab code;

2, each time period releases the code to be limited;

3, each code does not allow duplication;

Realize:

1, do not consider the case of concurrent 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 current time issued number of Authenticode
$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 Code_test (code,create_time) VALUES (' $code ',". Time (). "));

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

process without consideration of concurrency:

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

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

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

2. Implementation under concurrent conditions:

So look at the results of the above code in concurrent situations:

Test concurrency, you can use the 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 call, such as: ab-c Concurrent Quantity-n total traffic URL

Copy Code code 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 is queried to have a quota, will go to generate the verification code, insert the database, return to the verification code; This creates a lot of verification code. In fact, after running the command, the database has 13 more records, not one.

How can we avoid this happening?

Can be locked to the insertion process by adding exclusive locks to ensure that there is only one process running at any time in this judgment process. Implemented as follows:

Build 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 exclusive lock
if (Flock ($FP, lock_ex)) {
//Query The current time has issued the number of Authenticode
$code = $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);

The procedure is locked by the flock function.

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

Run again

Copy Code code 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 concurrent situations.

The above is a small series to introduce the PHP through the lock to achieve concurrent situation Rob code function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.