The previously written zk-based distributed lock is as follows:
Redis implements the principle of distributed locking:
The principle of implementing distributed locks in Redis is to call the SETNX command of redis, and if the key already exists, the SETNX command does nothing. The command returns 1 when the setting succeeds and 0 when the setting fails.
First, our website simulates 10,000 products, and then writes a console simulating HTTP request, which takes 40 seconds to complete the purchase, the rendering is as follows:
Create a new website ASP.NET Core 3.1, and the redis lock package is as follows:
I saw that many release locks on the Internet are written as follows:
However, it feels like there may be a problem, in the case of concurrency, it is possible to return true at the time of judgment, it is indeed a lock added by itself, but when the lock is deleted, it may be inIn extreme cases, the lock has been obtained by another thread, in case someone else's lock is deleted.
Therefore, when obtaining a lock, you need to open a thread to extend the lock's expiration time.
The WeatherForecastController interface is as follows:
You can start the API website via the command line, which can be easily restarted, as follows:
Create a new .NET Core mockup HTTP request for snap purchase, the code is as follows:
If there is anything wrong, thank you for correcting it.
Finally, attach the source code:
Tourists, if you want to see the hidden content of this post, please Reply
|