Currently, what I use is to set a value in the session. Every time I come in, I check whether the value exists. If it exists, it means it is locked. This request will not be executed. If it does not exist, set the value and perform subsequent operations.
<code>class controller{ function index(){ if(isset($_SESSION['lock'])) return; $_SESSION['lock'] = 1; sleep(1);//这里会查询用户的资格,并给用户发送流量,如果两次同时进来,会造成两次都认为是有资格的,并且会给用户发两次相同流量 unset($_SESSION['lock']); } } </code>
I thought it was possible to do this, and the session is stored in redis very quickly. Until I couldn't sleep and fiddled with it just now, I found that it was still possible for a user to execute multiple requests for the same interface at the same time.
If you want multiple requests from one person at the same time, only the first one will be processed. To determine whether they are the same person, judge based on session.
How do you ensure this?
Currently, what I use is to set a value in the session. Every time I come in, I check whether the value exists. If it exists, it means it is locked. This request will not be executed. If it does not exist, set the value and perform subsequent operations.
<code>class controller{ function index(){ if(isset($_SESSION['lock'])) return; $_SESSION['lock'] = 1; sleep(1);//这里会查询用户的资格,并给用户发送流量,如果两次同时进来,会造成两次都认为是有资格的,并且会给用户发两次相同流量 unset($_SESSION['lock']); } } </code>
I thought it was possible to do this, and the session is stored in redis very quickly. Until I couldn't sleep and fiddled with it just now, I found that it was still possible for a user to execute multiple requests for the same interface at the same time.
If you want multiple requests from a person at the same time, only the first one will be processed. To determine whether they are the same person, judge based on session.
How do you ensure this?
If the database is involved, use locking, or put all requests into a queue and process them one by one
I thought about it and I should write two things, one is the entry time and the exit time.
The access starts writing the start time, and the exit writes the exit time.
Assume that the first visit must be empty, start=02:26:30 end=02:26:50
Then when I re-initiate the visit, judge:
<1>If start>end , indicating that there is another situation during the access, that is, the access is interrupted and there is no time to write the end time, so an initialization judgment must be made here, such as start>end&&start-end>100 to directly change end to the current Time, then it is free, and the access is carried out
<2>If start
You can use the database to process it. Set a
mark. When the user enters the request, check the database first. If the
mark exists, it will not be executed.
Use the cache to store the id of the user being processed, operate the id through the filter, and the user request comes in. If the filter determines that the id is not in the cache, it will be added, and the id will be deleted after processing.
There are many methods. The question is what are your actual needs. I hope you can describe it from the perspective of final data. Maybe it is simpler not to describe it from the perspective of API
Judging from your question, you should have come up with a stupid solution. It is recommended that you state your original needs.
1. If you use Mysql, you can lock Mysql. After a request is completed, the lock must be released before other requests can be executed.
2. You can use a counter. When a request comes in, you can add one to the counter. After the request ends, clear the counter. This method is more recommended
3. In addition, the file session itself is locked. If each request by the same user is not completed, other requests will also be blocked
After typing a file, use File Lock to unlock the file after the operation is completed. The next user can continue to access and continue to lock