There is a query system. When the user queries, if there is no data in the database, he needs to call a third-party query interface to obtain the data, then insert the data into the database, and then retrieve the data from the database and return it to the user. [The system is developed based on PHP]
If multiple users query, there will be multiple requests to the third-party interface for query at the same time, and the problem of repeated data insertion will arise.
How to solve this problem?
When calling a third-party interface, use Redis
to ensure that only one request goes to the third party, thereby avoiding repeated query and insertion of data. Is there any other good method? ? How to achieve it specifically?
Thank you!
Lock mechanism. Before the code enters the operation, check whether the operation is locked. If it is locked, interrupt the operation. Otherwise, proceed to the next operation. The first step is to lock the operation, then execute the code. After executing the code, don't forget to unlock the operation lock. Otherwise, you won’t be able to carry out the execution.
There are many locking codes, and the one given above is one of them. Redismemcachecache files can be used. If the concurrency of operations is relatively high, it is recommended to use redis like the one above. (In fact, it is to use the string data type to assign a value to the lock key {lock}, and when unlocking, the value of the key will be cleared or assigned a value of 0)
The redis "lock" mentioned by the poster is of course feasible
In addition, can a unique ID be set for the queried data? This is double verification