Redis modifies multi-thread concurrency of data-Redis concurrent lock and redis multi-thread
Reprinted crawlers please specify the address, blog garden snail http://www.cnblogs.com/tdws/p/5712835.html
Configure a simple RedisHelper, a set value, a get value, and a concurrent lock so that you can understand what I have done in my subsequent operations.
1 public class RedisHelper 2 {3 public RedisClient client = new RedisClient ("127.0.0.1", 6379); 4 public void Set <T> (string key, T val) 5 {6 client. set (key, val); 7} 8 public T Get <T> (string key) 9 {10 var result = client. get <T> (key); 11 return result; 12} 13 public IDisposable Setnx (string key) 14 {15 return client. acquireLock (key); 16} 17}View Code
Now let's take a look at the concurrent code. I only have two new threads. The two threads want to access the same key at the same time and access the same key for 50 thousand times respectively. In the case of concurrency, it is difficult for us to ensure data accuracy. Please compare the output results.
1 static void Main (string [] args) 2 {3 RedisHelper rds = new RedisHelper (); 4 rds. set <int> ("mykey1", 0); 5 Thread myThread1 = new Thread (AddVal); 6 Thread myThread2 = new Thread (AddVal); 7 myThread1.Start (); 8 myThread2.Start (); 9 Console. writeLine ("waiting for two threads to end"); 10 Console. readKey (); 11} 12 13 public static void AddVal () 14 {15 RedisHelper rds = new RedisHelper (); 16 for (int I = 0; I <50000; I ++) 17 {18 19 int result = rds. get <int> ("mykey1"); 20 rds. set <int> ("mykey1", result + 1); 21 22} 23 Console. writeLine ("End of thread, output" + rds. get <int> ("mykey1"); 24}View Code
Yes. We run two tasks in a single thread.50000, Will output100000. At present, two concurrent threads are running at the same time because the data results caused by concurrency are often not what we want. So how can we solve this problem? Redis is ready for us!
You can see that the RedisHelper method isPublic IDisposable Setnx (string key ).The returned IDisposable proves that we need to manually release the resource. Internal MethodAcquireLockIt is the key. Like redis, it requests a lock header. The locked resources can only be accessed by a single thread, and will not be get or set at the same time by two threads, these two threads Must be alternating. Of course, the alternation here does not mean that you are me once, or you are multiple times. I will check the code below.
1 static void Main (string [] args) 2 {3 RedisHelper rds = new RedisHelper (); 4 rds. set <int> ("mykey1", 0); 5 Thread myThread1 = new Thread (AddVal); 6 Thread myThread2 = new Thread (AddVal); 7 myThread1.Start (); 8 myThread2.Start (); 9 Console. writeLine ("waiting for two threads to end"); 10 Console. readKey (); 11} 12 13 public static void AddVal () 14 {15 RedisHelper rds = new RedisHelper (); 16 for (int I = 0; I <50000; I ++) 17 {18 using (rds. setnx ("lock") 19 {20 int result = rds. get <int> ("mykey1"); 21 rds. set <int> ("mykey1", result + 1); 22} 23} 24 Console. writeLine ("End of thread, output" + rds. get <int> ("mykey1"); 25}View Code
We can see that I used using to callSetnxMethod to obtain the lock.
The output result is100000Is the correct result. Previous8 W+ Execution is completed first for one of the two threads.
Also, in the formal use process, we recommend that you give us the lock, after usingDeleteDrop, andAdd an expiration time, UseExpire.
To avoid unexpected exit during the execution of the program, the lock will always exist, and the lock data may not be updated or retrieved in the future.
You can also try not to set expire. When the program starts to run, close the console, run the program again, and get the value you have locked in the redis-cli operation console, it will never be obtained.