PHP code grabbing function in case of concurrent execution by locking _ php instance

Source: Internet
Author: User
Tags flock
This article uses the php language to implement the code grabbing function in the case of concurrent requests. opening code grabbing in a specific period of time does not allow repeated requests. This article describes in detail. For more information, see the following requirements: code grabbing

Requirements:

1. code snatching is enabled only in a specific period of time;

2. the number of opened codes in each time period is limited;

3. duplicate codes are not allowed;

Implementation:

1. implemented without considering concurrency:

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 number of verification codes issued at the current time $ 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); // pause for 2 seconds $ code = get_code (6); var_dump ($ pdo-> query ("insert into code_test (code, create_time) VALUES ('$ code ',". time (). ")"));}

By default, the above code satisfies the current open time and the code is not repeated;

Process without considering concurrency:

1) select the number of verification codes issued by the current database;

2) if there are more places, a verification code will be generated, inserted into the database, and the verification code will be returned to the client;

3) if it is full, a prompt is returned, indicating that there are no more places;

2. concurrency:

Let's take a look at the result of the above code in the case of concurrency:

You can use apache benchmark to test the concurrency. apache benchmark is a performance evaluation tool of APACHE's http server. it enters the bin directory of apche through cmd and is called by the AB Command, for example: AB-c concurrency-n total access url

The code is as follows:


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


In this way, 100 user colleagues will compete for one quota. during the query, each user will find another quota, and a verification code will be generated, inserted into the database, and a verification code will be returned; this leads to more verification codes. In fact, after running this command, the database has 13 more records instead of one.

How can this problem be avoided?

The exclusive lock can be applied to determine the insertion process. This ensures that only one process is running at any time. 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'); // if (flock ($ fp, LOCK_EX )) {// query the number of verification codes issued at the current time $ 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 100 http: // localhost/php_mulit.php

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

The above is a small Editor to introduce to you the PHP code grabbing function when implementing concurrency through locking. I hope it will be helpful to you. if you have any questions, please leave a message for me, the editor will reply to you in a timely manner. I would like to thank you for your support for the script home website!

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.