In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
In fact, it is not appropriate to modify data with multi-thread. after all, the redis server is single-threaded, and all commands are executed serially, but when the client sends commands concurrently, it leads to some problems in the arrangement of serial commands and the time difference in the network, which leads to data inconsistency. Although this article is the addition and subtraction of numbers, but in order to illustrate the situation of locks, it is not intended to use the atomic command incr.
Start with a simple RedisHelper, a set value, a get value, and a set concurrency lock so that you can know exactly what I did in the rest of my operation.
Public class RedisHelper {public RedisClient client = new RedisClient ("127.0.0.1", 6379); public void Set (string key, T val) {client.Set (key, val);} public T Get (string key) {var result = client.Get (key); return result } public IDisposable Acquire (string key) {return client.AcquireLock (key);}}
Let's take a look at the concurrent code. I only new two Thread. Two threads want to access the same key for 50, 000 times at the same time. Under concurrent conditions, it is difficult for us to guarantee the accuracy of the data. Please compare the output results.
Static void Main (string [] args) {RedisHelper rds = new RedisHelper (); rds.Set ("mykey1", 0); Thread myThread1 = new Thread (AddVal); Thread myThread2 = new Thread (AddVal); myThread1.Start (); myThread2.Start (); Console.WriteLine ("wait for two threads to end") Console.ReadKey ();} public static void AddVal () {RedisHelper rds = new RedisHelper (); for (int I = 0; I < 50000; iTunes +) {int result = rds.Get ("mykey1"); rds.Set ("mykey1", result + 1) } Console.WriteLine ("thread ends, output" + rds.Get ("mykey1"));}
Yes, and we single-threaded, run two 50000, will output 100000. Now it is two concurrent threads running at the same time that the data results caused by concurrency are often not what we want. So how to solve this problem, Redis is ready for us!
You can see that one method in my RedisHelper is public IDisposable Acquire (string key). You can also see that he returned IDisposable, proving that we need to release resources manually.
The AcquireLock inside the method is the key, it is like asking for a lock in redis, the locked resources can only be accessed by a single thread, and will not be get or set by two threads at the same time, these two threads must be carried out alternately, of course, the alternation here does not refer to you once or me once, or you many times, I once, let's see the code below.
Static void Main (string [] args) {RedisHelper rds = new RedisHelper (); rds.Set ("mykey1", 0); Thread myThread1 = new Thread (AddVal); Thread myThread2 = new Thread (AddVal); myThread1.Start (); myThread2.Start (); Console.WriteLine ("wait for two threads to end") Console.ReadKey ();} public static void AddVal () {RedisHelper rds = new RedisHelper (); for (int I = 0; I < 50000; iTunes +) {using (rds.Acquire ("lock")) {int result = rds.Get ("mykey1") Rds.Set ("mykey1", result + 1);}} Console.WriteLine ("Thread ends, output" + rds.Get ("mykey1"));}
You can see that I used using and called my Acquire method to get the lock.
The final output is 100000, which is exactly the correct result we want. The previous 8W + is because the execution of one of the two threads ends first.
Also, in the process of formal use, it is recommended that the lock given to us should be deleted after use, add an expiration time, and use expire.
In case the program exits unexpectedly during the execution of the program, the lock always exists, and the locked data may not be updated or obtained in the future.
You can also try not to set expire, close console at the beginning of the program execution, rerun the program, and in the redis-cli console, get the value you lock will never be obtained.
All machines connected to this redis instance can only have one lock that acquires the specified name at a time.
Here is how to write StackExchange.Redis
Var info = "name-" + Environment.MachineName; / / if the lock is not released for 5 seconds, the lock is automatically released. Avoid deadlock if (db.LockTake ("name", info) TimeSpan.FromSeconds (5)) {try {} catch (Exception ex) {} finally { Db.LockRelease ("name" Token) }}
The above are the details of the simple application of Redis lock, please pay attention to other related articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.