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

View: 34174|Reply: 5

[.NET Core] .NET Core implements distributed lock principle parsing based on Redis

[Copy link]
Posted on 9/22/2020 9:17:22 AM | | | |
In business scenarios, simultaneous operations of content, such as commodity inventory, the same file, etc., are not allowed, and the program application is deployed on multiple servers.
[Actual combat] ASP.NET Core is based on Redis distributed lock flash sale
https://www.itsvse.com/thread-9397-1-1.html

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

What you also need to know to read this article:

Volatile vs. Interlocked vs. lock
https://www.itsvse.com/thread-5023-1-1.html

C# Parallel Computation Parallel.For&Parallel.For
https://www.itsvse.com/thread-3671-1-1.html

.NET Core implements distributed locks based on RedisThe principle is to call the SETNX command of redis, if the key already exists, the SETNX command does nothing. The command returns 1 when the setting succeeds and 0 when the setting fails.

The hyperlink login is visible.

Create a new .NET Core 3.1 console project, add a StackExchange.Redis reference, and use the nuget command as follows:

We call the library's StringSet method, through the StackExchange.Redis source code, we can see that the StringSet method will call the private method of GetStringSetMessage to execute different set commands through the enumeration value of when, the code is as follows:

We execute the SETNX command 2000 times through a parallel test call to the program, which is executed in 2 installments, in which the cache validity of the key is 5 seconds, that is, the key will be automatically released after 5 seconds, that is, the lock will be released.

The code is as follows:


As you can see, a total of 2 successful insertions and 1998 failed attempts are exactly what we expected, and the renderings are as follows:


At this time, the lock has not yet been released.
(End)









Previous:Failed to find a workaround for part of the path "\bin\roslyn\csc.exe"
Next:EF Core Series 2 encapsulates OnModelCreating, creates indexes using reflections, and more
 Landlord| Posted on 9/26/2020 3:37:52 PM |
test123
Posted on 11/20/2020 11:18:31 AM |
If there is a multi-threaded environment and a multi-instance environment when deploying containers, is there a problem with this handling?
 Landlord| Posted on 11/20/2020 11:29:44 AM |
qiuyueming Posted on 2020-11-20 11:18
If there is a multi-threaded environment and a multi-instance environment when deploying containers, is there a problem with this handling? ...

Multi-instance means multi-threading, and in the source code, multi-threaded calls redis, and only one thread gets a lock
Posted on 3/22/2022 2:32:40 PM |
Support, support
Posted on 3/23/2022 11:15:30 AM |
Learn this is good @
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