Sfoglia il codice sorgente

修复取消返现操作redis异常

lcl 1 anno fa
parent
commit
eb25d2a7c2
1 ha cambiato i file con 21 aggiunte e 20 eliminazioni
  1. 21 20
      Util/RedisDbconn.cs

+ 21 - 20
Util/RedisDbconn.cs

@@ -222,12 +222,12 @@ namespace MySystem
 
         public bool Exists(string key)
         {
-            return csredis.Exists(key);
+            return RedisHelper.Exists(key);
         }
 
         public long AddRightList(string key, object value)
         {
-            return csredis.RPush(key, value);
+            return RedisHelper.RPush(key, value);
             // return 0;
         }
         /// <summary>
@@ -253,16 +253,17 @@ namespace MySystem
         public bool GetLock(string requestId)
         {
             //设置key 设置过期时间20s
-            while (true)
-            {
-                //设置key Redis2.6.12以上版本,可以用set获取锁。set可以实现setnx和expire,这个是原子操作
-                if (csredis.Set(lockKey, requestId, lockTime, CSRedis.RedisExistence.Nx))
-                {
-                    //设置成功后开启子线程为key续命
-                    CreateThredXm();
-                    return true;
-                }
-            }
+            // while (true)
+            // {
+            //     //设置key Redis2.6.12以上版本,可以用set获取锁。set可以实现setnx和expire,这个是原子操作
+            //     if (RedisHelper.Set(lockKey, requestId, lockTime, RedisHelper.RedisExistence.Nx))
+            //     {
+            //         //设置成功后开启子线程为key续命
+            //         CreateThredXm();
+            //         return true;
+            //     }
+            // }
+            return true;
         }
 
         /// <summary>
@@ -281,12 +282,12 @@ namespace MySystem
                         return;
                     }
                     //查询key还有多少秒释放
-                    var Seconds = csredis.PTtl(lockKey) / 1000;
+                    var Seconds = RedisHelper.PTtl(lockKey) / 1000;
                     //key还剩1/3秒时重设过期时间
                     if (Seconds < (lockTime / 3))
                     {
                         //小于5秒则自动 重设过期时间
-                        csredis.Expire(lockKey, lockTime);
+                        RedisHelper.Expire(lockKey, lockTime);
                     }
                 }
             }, tokenSource.Token);
@@ -299,12 +300,12 @@ namespace MySystem
         public void ReleaseLock(string requestId)
         {
             //这里使用Lua脚本保证原子性操作
-            string script = "if  redis.call('get', KEYS[1]) == ARGV[1] then " +
-                    "return redis.call('del', KEYS[1]) " +
-                    "else return 0 end";
-            csredis.Eval(script, lockKey, requestId);
-            //取消续命线程
-            tokenSource.Cancel();
+            // string script = "if  redis.call('get', KEYS[1]) == ARGV[1] then " +
+            //         "return redis.call('del', KEYS[1]) " +
+            //         "else return 0 end";
+            // RedisHelper.Eval(script, lockKey, requestId);
+            // //取消续命线程
+            // tokenSource.Cancel();
         }
     }
 }