This article is a mirror article of machine translation, please click here to jump to the original article.

View: 163142|Reply: 27

[.NET Core] [Actual combat] ASP.NET Core is based on Redis distributed lock flash sale

[Copy link]
Posted on 9/26/2020 3:34:26 PM | | | |
The previously written zk-based distributed lock is as follows:

.net/c# Zookeeper Distributed Lock Implementation [Source Code]
https://www.itsvse.com/thread-4651-1-1.html
Redis implements the principle of distributed locking:

.NET Core implements distributed lock principle parsing based on Redis
https://www.itsvse.com/thread-9391-1-1.html
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, pleaseReply





Previous:JS adapts to the screen size carousel graph
Next:This detailed explanation of Javascript
 Landlord| Posted on 5/9/2021 9:12:58 PM |
Website simulations have10000 items, and then wrote a console to simulate the HTTP request time The sold-out is completed in 6 seconds


.NET Core implements distributed locks based on Redis Lua [with source code]
https://www.itsvse.com/thread-9642-1-1.html


Posted on 11/5/2023 11:02:52 AM |
Look at Console.WriteLine($"Rush Successful: {safeInstanceSuccessedCount}, Failed: {safeInstanceFailedCount}, Sold Out: {safeInstanceDoneCount}");
 Landlord| Posted on 4/27/2021 11:17:53 AM |
There are some problems with the code, and the Task blocking should use await Task.Delay(i);  , do not use sleep
Posted on 9/27/2020 2:13:33 PM |
mark,mark,mark
Posted on 10/5/2020 11:12:20 AM |
mark mark mark
Posted on 11/2/2020 3:39:13 PM |
dfdsfdsfds
Posted on 11/5/2020 10:08:13 AM |
Source code is needed hahahahahahaha
Posted on 12/2/2020 3:11:30 PM |
Thanks for sharing.
Posted on 12/7/2020 9:10:03 PM |
[Actual combat] ASP.NET Core is based on Redis distributed lock flash sale
Posted on 12/9/2020 5:17:34 PM |
Check out the actual effect and source code.
Posted on 1/20/2021 3:17:50 PM |
Thank you, thank you, thank you, thank you
Posted on 1/26/2021 4:10:46 PM |
MARK MARK MARK MARK
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com