{"id":220763,"date":"2021-06-26T09:42:40","date_gmt":"2021-06-26T01:42:40","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=220763"},"modified":"2021-06-18T10:43:51","modified_gmt":"2021-06-18T02:43:51","slug":"redis-distributed-lock","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/redis-distributed-lock.html","title":{"rendered":"\u8be6\u89e3Redis\u5206\u5e03\u5f0f\u9501"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6700\u8fd1\u9996\u5ea6\u5e94\u7528\"\u5206\u5e03\u5f0f\u9501\"\uff0c\u73b0\u5728\u60f3\u60f3\uff0c\u5206\u5e03\u5f0f\u9501\u4e0d\u662f\u5b64\u7acb\u7684\u6280\u80fd\u70b9\uff0c\u8fd9\u5176\u5b9e\u5c31\u662f\u8de8\u4e3b\u673a\u7684\u7ebf\u7a0b\u540c\u6b65\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u5206\u5e03\u5f0f\u9501\u662f\"\u7ebf\u7a0b\u540c\u6b65\"\u7684\u5ef6\u7eed<\/strong><\/div>\n

\u6700\u8fd1\u9996\u5ea6\u5e94\u7528\"\u5206\u5e03\u5f0f\u9501\"\uff0c\u73b0\u5728\u60f3\u60f3\uff0c\u5206\u5e03\u5f0f\u9501\u4e0d\u662f\u5b64\u7acb\u7684\u6280\u80fd\u70b9\uff0c\u8fd9\u5176\u5b9e\u5c31\u662f\u8de8\u4e3b\u673a\u7684\u7ebf\u7a0b\u540c\u6b65\u3002<\/p>\n

\"\"<\/p>\n

\u5355\u673a\u670d\u52a1\u5668\u53ef\u4ee5\u901a\u8fc7\u5171\u4eab\u67d0\u5806\u5185\u5b58\u6765\u6807\u8bb0\u4e0a\u9501\/\u89e3\u9501\uff0c\u7ebf\u7a0b\u540c\u6b65\u8bf4\u5230\u5e95\u662f\u5efa\u7acb\u5728\u5355\u673a\u64cd\u4f5c\u7cfb\u7edf\u7684\u7528\u6237\u6001\/\u5185\u6838\u6001\u5bf9\u5171\u4eab\u5185\u5b58\u7684\u8bbf\u95ee\u63a7\u5236\u3002<\/p>\n

\u800c\u5206\u5e03\u5f0f\u670d\u52a1\u5668\u4e0d\u662f\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\uff1a\u8de8\u4e3b\u673a\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u9501\u6807\u8bb0\u5b58\u50a8\u5728\u6240\u6709\u673a\u5668\u8fdb\u7a0b\u90fd\u80fd\u770b\u5230\u7684\u5730\u65b9\u3002<\/p>\n

\u5728\u5f00\u53d1\u5f88\u591a\u4e1a\u52a1\u573a\u666f\u4f1a\u4f7f\u7528\u5230\u9501\uff0c\u4f8b\u5982\u5e93\u5b58\u63a7\u5236\uff0c\u62bd\u5956\u7b49\u3002<\/p>\n

\u4f8b\u5982\u5e93\u5b58\u53ea\u52691\u4e2a\u5546\u54c1\uff0c\u6709\u4e09\u4e2a\u7528\u6237\u540c\u65f6\u6253\u7b97\u8d2d\u4e70\uff0c\u8c01\u5148\u8d2d\u4e70\u5e93\u5b58\u7acb\u5373\u6e05\u96f6\uff0c\u4e0d\u80fd\u8ba9\u5176\u4ed6\u4e8c\u4eba\u4e5f\u8d2d\u4e70\u6210\u529f\u3002<\/p>\n

\u89e3\u8bfb\u5206\u5e03\u5f0f\u9501<\/strong><\/div>\n

\u6211\u4eec\u5e38\u8bf4\u7684\u7ebf\u7a0b\u5b89\u5168\u3001\u7ebf\u7a0b\u540c\u6b65\u65b9\u6848\uff0c\u5305\u62ec\u6b64\u6b21\u7684\u5206\u5e03\u5f0f\u9501\u90fd\u662f\u57fa\u4e8e<\/p>\n

\u201c\u591a\u7ebf\u7a0b\/\u591a\u8fdb\u7a0b\u5bf9\u7279\u5b9a\u8d44\u6e90\u540c\u65f6\u6709\u66f4\u65b0\u64cd\u4f5c\u201d\u3002<\/p>\n

\"\"<\/p>\n

\u57fa\u672c\u8003\u91cf<\/strong><\/div>\n
    \n
  1. \u5206\u5e03\u5f0f\u7cfb\u7edf\uff0c\u4e00\u4e2a\u9501\u5728\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u88ab\u4e00\u4e2a\u670d\u52a1\u5668\u83b7\u53d6 (\u8fd9\u662f\u5206\u5e03\u5f0f\u9501\u7684\u57fa\u7840)<\/li>\n
  2. \u5177\u5907\u9501\u5931\u6548\u673a\u5236\uff0c\u9632\u6b62\u6b7b\u9501 (\u9632\u6b62\u67d0\u4e9b\u610f\u5916\uff0c\u9501\u6ca1\u6709\u5f97\u5230\u91ca\u653e\uff0c\u522b\u4eba\u4e5f\u65e0\u6cd5\u5f97\u5230\u9501)<\/li>\n<\/ol>\n

    Redis SET resource-name anystring NX EX max-lock-time<\/p>\n

    \u662f\u4e00\u79cd\u6700\u7b80\u5355\u7684\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\u65b9\u6848\u3002<\/p>\n

    SET \u547d\u4ee4\u652f\u6301\u591a\u4e2a\u53c2\u6570\uff1a<\/p>\n

      \n
    • EX seconds-- \u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4(s)<\/li>\n
    • NX -- \u5982\u679ckey\u4e0d\u5b58\u5728\uff0c\u5219\u8bbe\u7f6e ......<\/li>\n<\/ul>\n

      \u56e0\u4e3aSET\u547d\u4ee4\u53c2\u6570\u53ef\u4ee5\u66ff\u4ee3SETNX\uff0cSETEX\uff0cGETSET\uff0c\u8fd9\u4e9b\u547d\u4ee4\u5728\u672a\u6765\u53ef\u80fd\u88ab\u5e9f\u5f03\u3002<\/p>\n

      \u4e0a\u9762\u7684\u547d\u4ee4\u8fd4\u56deOK(\u6216\u7ecf\u8fc7\u91cd\u8bd5)\uff0c\u5ba2\u6237\u7aef\u5c31\u83b7\u53d6\u5230\u8fd9\u4e2a\u9501;<\/p>\n

      \u4f7f\u7528DEL\u547d\u4ee4\u89e3\u9501;\u5230\u8fbe\u8d85\u65f6\u65f6\u95f4\u4f1a\u81ea\u52a8\u91ca\u653e\u9501\u3002<\/p>\n

      \u5728\u89e3\u9501\u65f6\uff0c\u589e\u52a0\u4e00\u4e9b\u8bbe\u8ba1\uff0c\u8ba9\u7cfb\u7edf\u66f4\u52a0\u5065\u58ee\uff1a<\/p>\n

      3.\u4e0d\u8981\u4f7f\u7528\u56fa\u5b9a\u7684String\u503c\u4f5c\u4e3a\u9501\u6807\u8bb0\u503c\uff0c\u800c\u662f\u4f7f\u7528\u4e00\u4e2a\u4e0d\u6613\u88ab\u731c\u4e2d\u7684\u968f\u673a\u503c\uff0c \u4e1a\u5185\u79f0\u4e3atoken<\/p>\n

      4.\u4e0d\u4f7f\u7528DEL\u547d\u4ee4\u91ca\u653e\u9501\uff0c\u800c\u662f\u53d1\u9001script\u53bb\u79fb\u9664key<\/p>\n

      \u7b2c3\u30014\u70b9\u662f\u4e3a\u4e86\u89e3\u51b3 \uff1a\u201c\u9501\u63d0\u524d\u8fc7\u671f\uff0c\u5ba2\u6237\u7aefA\u8fd8\u6ca1\u6709\u6267\u884c\u5b8c\uff0c\u7136\u540e\u5ba2\u6237\u7aefB\u83b7\u53d6\u4e86\u9501\uff0c\u8fd9\u65f6\u5ba2\u6237\u7aefA\u6267\u884c\u5b8c\u4e86\uff0c\u4f1a\u4e0d\u4f1a\u5728\u5220\u9501\u7684\u65f6\u5019\u628aB\u7684\u9501\u7ed9\u5220\u6389\u201d -- 4\u662f3\u6280\u672f\u4e0a\u7684\u63a8\u8350\u5b9e\u73b0\u3002<\/p>\n

      \u811a\u672c\u5982\u4e0b\uff1a<\/p>\n

      \r\nif redis.call(\"get\",KEYS1] ==ARGV[1]) \r\nthen \r\n   return  redis.call(\"DEL\",KEYS[1]) \r\nelse \r\n  return 0 \r\nend <\/pre>\n

      \u4e0b\u9762\u4f7f\u7528StackExchange.Redis \u5199\u4e86\u57fa\u4e8e\u4ee5\u4e0a\u8003\u91cf\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n

      \r\n\/\/\/  \r\n\/\/\/ Acquires the lock. \r\n\/\/\/ <\/summary> \r\n\/\/\/  \r\n\/\/\/ \u968f\u673a\u503c \r\n\/\/\/  \r\n \/\/\/ \u975e\u963b\u585e\u9501 \r\nstatic bool Lock(string key, string token,int expireSecond=10, double waitLockSeconds = 0) \r\n{ \r\n    var waitIntervalMs = 50; \r\n    bool isLock; \r\n             \r\n    DateTime begin = DateTime.Now; \r\n    do \r\n    { \r\n         isLock = Connection.GetDatabase().StringSet(key, token, TimeSpan.FromSeconds(expireSecond), When.NotExists); \r\n         if (isLock) \r\n             return true; \r\n             \/\/\u4e0d\u7b49\u5f85\u9501\u5219\u8fd4\u56de \r\n             if (waitLockSeconds == 0) break; \r\n             \/\/\u8d85\u8fc7\u7b49\u5f85\u65f6\u95f4\uff0c\u5219\u4e0d\u518d\u7b49\u5f85 \r\n             if ((DateTime.Now - begin).TotalSeconds >= waitLockSeconds) break; \r\n             Thread.Sleep(waitIntervalMs); \r\n     } while (!isLock); \r\n     return false; \r\n } \r\n        \r\n\/\/\/    \r\n\/\/\/ Releases the lock.   \r\n\/\/\/ <\/summary>   \r\n\/\/\/ true<\/c>, if lock was released, false<\/c> otherwise.<\/returns>   \r\n\/\/\/ Key.   \r\n\/\/\/ value   \r\nstatic bool UnLock(string key, string value) \r\n{ \r\n    string lua_script = @\"   \r\n    if (redis.call('GET', KEYS[1]) == ARGV[1]) then   \r\n         redis.call('DEL', KEYS[1])   \r\n          return true   \r\n          else   \r\n          return false   \r\n        end   \r\n      \"; \r\n     try \r\n     { \r\n          var res = Connection.GetDatabase().ScriptEvaluate(lua_script, \r\n                                                           new RedisKey[] { key }, \r\n                                                           new RedisValue[] { value }); \r\n            return (bool)res; \r\n      } \r\n     catch (Exception ex) \r\n     { \r\n          Console.WriteLine($\"ReleaseLock lock fail...{ex.Message}\"); \r\n          return false; \r\n     } \r\n} \r\n         \r\n        private static Lazy lazyConnection = new Lazy<\/connectionmultiplexer>(() => \r\n        { \r\n            ConfigurationOptions configuration = new ConfigurationOptions \r\n            { \r\n                AbortOnConnectFail = false, \r\n                ConnectTimeout = 5000, \r\n            }; \r\n            configuration.EndPoints.Add(\"10.100.219.9\", 6379); \r\n            return ConnectionMultiplexer.Connect(configuration.ToString()); \r\n        });  \r\n        public static ConnectionMultiplexer Connection => lazyConnection.Value; <\/connectionmultiplexer><\/pre>\n

      \u4ee5\u4e0a\u4ee3\u7801\u65b0\u589e\u4e86\u7b2c\u4e94\u70b9\u8003\u91cf\uff1a<\/p>\n

      5. \u4e3a\u907f\u514d\u65e0\u9650\u5236\u62a2\u9501\uff0c\u589e\u52a0\u4e86\u975e\u963b\u585e\u9501\uff1a\u8f6e\u8be2_s\u7b49\u5f85\u9501\uff0c\u672a\u7b49\u5230\u5219\u4e0d\u518d\u62a2\u9501<\/p>\n

      \u4f7f\u7528\u65b9\u5f0f\uff1a<\/strong><\/div>\n

      \u4e0b\u9762\u5e76\u884c\u5f00\u542f\u4e09\u4e2a\u4efb\u52a1\uff0c\u540c\u65f6\u51cf\u5c11\u5e93\u5b58\uff1a<\/p>\n

      \r\nstatic void Main(string[] args) \r\n{ \r\n     \/\/ \u5c1d\u8bd5\u5e76\u884c\u6267\u884c3\u4e2a\u4efb\u52a1 \r\n     Parallel.For(0, 3, x => \r\n     { \r\n           string token = $\"loki:{x}\"; \r\n           bool isLocked = Lock(\"loki\", token, 5, 10); \r\n             \r\n           if (isLocked) \r\n           { \r\n               Console.WriteLine($\"{token} begin reduce stocks (with lock) at {DateTime.Now}.\"); \r\n               Thread.Sleep(1000); \r\n               Console.WriteLine($\"{token} release lock {UnLock(\"loki\", token)} at {DateTime.Now}. \"); \r\n           } \r\n           else \r\n           { \r\n             Console.WriteLine($\"{token} begin reduce stocks at {DateTime.Now}.\"); \r\n           } \r\n       }); \r\n} <\/pre>\n

      \"\"<\/p>\n

      \u53ef\u4ee5\u770b\u5230\u4e09\u4e2a\u5e76\u884c\u4efb\u52a1\u4f9d\u6b21\u83b7\u53d6\/\u91ca\u653e\u9501<\/p>\n

      \u8f93\u51fa\u603b\u7ed3<\/strong><\/div>\n

      \u672c\u6587\u4ece\u57fa\u7840\u7684\u7ebf\u7a0b\u5b89\u5168\u3001\u7ebf\u7a0b\u540c\u6b65\uff0c\u8ba4\u8bc6\u5230\u5206\u5e03\u5f0f\u9501\u662f\u8de8\u4e3b\u673a\u7684\u8d44\u6e90\u7ebf\u7a0b\/\u8fdb\u7a0b\u540c\u6b65\u65b9\u6848\uff0c \u4ee5\u6b65\u6b65\u4e3a\u8425\u7684\u98ce\u683c \u6f14\u793a\u4e86RedisSET\u547d\u4ee4\u505a\u5206\u5e03\u5f0f\u9501\u7684\u8bbe\u8ba1\u8003\u91cf\uff0c\u597d\u8bb0\u6027\u4e0d\u5982\u70c2\u7b14\u5934\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

      \u6700\u8fd1\u9996\u5ea6\u5e94\u7528”\u5206\u5e03\u5f0f\u9501”\uff0c\u73b0\u5728\u60f3\u60f3\uff0c\u5206\u5e03\u5f0f\u9501\u4e0d\u662f\u5b64\u7acb\u7684\u6280\u80fd\u70b9\uff0c\u8fd9\u5176\u5b9e\u5c31\u662f\u8de8\u4e3b\u673a\u7684\u7ebf\u7a0b\u540c\u6b65\u3002 \u5355\u673a\u670d\u52a1\u5668\u53ef\u4ee5\u901a\u8fc7 […]<\/p>\n","protected":false},"author":317,"featured_media":202522,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-220763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=220763"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220763\/revisions"}],"predecessor-version":[{"id":220769,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/220763\/revisions\/220769"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/202522"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=220763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=220763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=220763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}