业务开发过程中经常会遇到并发的情况,或者多个业务接口会操作同一张表等需求,如果不处理的话就会导致重复触发的问题,比如对同一个库位进行操作,比如条码生成流水号等等。
简单使用
包RedLock.net
使用
初始化
var existingConnectionMultiplexer1 = ConnectionMultiplexer.Connect("127.0.0.1:6379");
            var multiplexers = new List<RedLockMultiplexer>
 {
     existingConnectionMultiplexer1,
 };
             RedLockFactory = RedLockFactory.Create(multiplexers);
使用
var resource = "the-thing-we-are-locking-on";
             var expiry = TimeSpan.FromSeconds(30);// 超时时间
             var wait = TimeSpan.FromSeconds(10);//等待时间
             var retry = TimeSpan.FromSeconds(1);//重试间隔
             this.Invoke(() =>
             {
                 if (from == "1")
                     textBox1.Text = $"{from}获取锁开始{DateTime.Now}";
                 else
                     textBox2.Text = $"{from}获取锁开始{DateTime.Now}";
             });
             // blocks until acquired or 'wait' timeout
             await using (var redLock = await RedLockFactory.CreateLockAsync(resource, expiry, wait, retry)) // there are also non async Create() methods
             {
                 // make sure we got the lock
                 if (redLock.IsAcquired)
                 {
                    // do stuff
                 }
                 this.Invoke(() =>
                 {
                     if (from == "1")
                         textBox1.Text = $"{from}获取锁{redLock.IsAcquired},{DateTime.Now}";
                     else
                         textBox2.Text = $"{from}获取锁{redLock.IsAcquired},{DateTime.Now}";
                 });
                 await Task.Delay(100);
             }
redLock.IsAcquired 为true则获取到唯一锁可进行业务操作。










