How does php lock an interface so that one interface can only process one request from the same person at a time?

Source: Internet
Author: User
Currently, I used to set a value in the session. every time I come in and judge whether this value exists, it means it is locked and will not execute this request. if it does not exist, set this value, and perform subsequent operations. {Code ...} I thought this was okay, and the session exists in redis... currently, I used to set a value in the session. every time I come in and judge whether this value exists, it means it is locked and will not execute this request. if it does not exist, set this value, and perform subsequent operations.

Class controller {function index () {if (isset ($ _ SESSION ['lock']) return; $ _ SESSION ['lock'] = 1; sleep (1 ); // Here, the user's qualifications will be queried and the traffic will be sent to the user. if two requests come in at the same time, both requests will be deemed as eligible, and the user will be sent two identical traffic unset ($ _ SESSION ['lock']);}

I thought it was okay to do this, and the session was saved in redis at a very fast speed until I couldn't get up and play with it, it turns out that a single user can execute multiple requests of the same interface at the same time.

If you want to process multiple requests at the same time, you can only process the first request. Determine whether a person is the same, and determine based on the session.

How do you ensure this?

Reply content:

Currently, I used to set a value in the session. every time I come in and judge whether this value exists, it means it is locked and will not execute this request. if it does not exist, set this value, and perform subsequent operations.

Class controller {function index () {if (isset ($ _ SESSION ['lock']) return; $ _ SESSION ['lock'] = 1; sleep (1 ); // Here, the user's qualifications will be queried and the traffic will be sent to the user. if two requests come in at the same time, both requests will be deemed as eligible, and the user will be sent two identical traffic unset ($ _ SESSION ['lock']);}

I thought it was okay to do this, and the session was saved in redis at a very fast speed until I couldn't get up and play with it, it turns out that a single user can execute multiple requests of the same interface at the same time.

If you want to process multiple requests at the same time, you can only process the first request. Determine whether a person is the same, and determine based on the session.

How do you ensure this?

If the database is involved, use locks, or process all requests in a queue one by one.

I thought, I should write two things. one is the entry time and the exit time.
Access start time and exit time.
Assume that the first access is always empty, start = 02:26:30 end = 02:26:50
In this case, when I initiate a new access request, let's take a look:
<1> if start> end
There is also a situation where access is interrupted and the end time cannot be written. Therefore, an initialization judgment must be made here, for example, if start> end & start-end> 100 changes the end value to the current time, it is idle for access.
<2> if start Note: The timestamp should be accurate to microseconds for easy calculation.

You can use a database for processing. setIdentifierThe user checks the database before entering the request.IdentifierNot executed,

Use the cache to store the id of the user being processed. use the filter operation id. if the user request comes in, the filter determines that the id is not in the cache, and the deletion id is processed.

There are many methods. The problem is what your actual needs are. I hope you can describe it from the perspective of final data. maybe it's easier not to use APIs.

From the perspective of your question, you should have proposed a solution to the problem. We recommend that you describe the original requirements.

1. if Mysql is used, you can lock Mysql. after a request is completed, other requests to release the lock can be executed.
2. you can use a counter. if the request comes in, you can add one to the counter. after the request ends, the counter is cleared. this method is recommended.
3. In addition, the session of the file is locked. if each request of the same user is not completed, other requests will be blocked.

After a file is closed, the file is unlocked after the file lock operation is completed. The next user can continue to access the lock

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.