{"id":201180,"date":"2025-05-10T08:18:16","date_gmt":"2025-05-10T00:18:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201180\/"},"modified":"2025-05-10T08:18:16","modified_gmt":"2025-05-10T00:18:16","slug":"%e4%b8%80%e6%96%87%e8%af%a6%e8%a7%a3%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e5%ae%9e%e7%8e%b0%e5%88%86%e5%b8%83%e5%bc%8f%e9%94%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201180\/","title":{"rendered":"\u4e00\u6587\u8be6\u89e3\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4e00\u6587\u8be6\u89e3\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u4e00\u6587\u8be6\u89e3\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\u9501\u3001Redis\u5206\u5e03\u5f0f\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<h2>1. \u4ec0\u4e48\u662f\u5206\u5e03\u5f0f\u9501<\/h2>\n<p>\u5f53\u6211\u4eec\u5728\u7f16\u5199\u591a\u7ebf\u7a0b\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u4e0d\u540c\u7684\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u53d1\u751f\u8d44\u6e90\u7684\u4e89\u593a\uff0c\u4e3a\u4e86\u907f\u514d\u8d44\u6e90\u4e89\u593a\u9020\u6210\u7684\u9519\u8bef\uff0c\u6211\u4eec\u4f1a\u5bf9\u8d44\u6e90\u4e0a\u9501\uff0c\u53ea\u6709\u83b7\u5f97\u9501\u7684\u7ebf\u7a0b\u624d\u80fd\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002<\/p>\n<p>\u8fdb\u7a0b\u4e2d\u7684\u9501\uff0c\u672c\u8d28\u5c31\u662f\u5185\u5b58\u4e2d\u4e00\u4e2a\u53d8\u91cf\uff0c\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u67d0\u4e2a\u64cd\u4f5c\u7533\u8bf7\u52a0\u9501\u65f6\uff0c\u5982\u679c\u80fd\u6210\u529f\u628a\u4ee3\u8868\u9501\u7684\u53d8\u91cf\u503c\u8bbe\u7f6e\u4e3a1\uff0c\u5219\u8868\u793a\u83b7\u5f97\u4e86\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u60f3\u8981\u83b7\u5f97\u9501\u65f6\u4f1a\u963b\u585e\uff0c\u800c\u62e5\u6709\u9501\u7684\u7ebf\u7a0b\u6267\u884c\u5b8c\u64cd\u4f5c\u540e\uff0c\u518d\u628a\u9501\u7684\u503c\u8bbe\u7f6e\u4e3a0\uff0c\u5219\u8868\u793a\u91ca\u653e\u4e86\u9501\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167244661963af829b1ae11.png\" class=\"aligncenter\"><\/p>\n<p>\u4e0a\u9762\u6211\u4eec\u8bf4\u7684\u662f\u5728\u4e00\u53f0\u670d\u52a1\u5668\u7684\u8fdb\u7a0b\u5185\u4e0d\u540c\u7ebf\u7a0b\u4e4b\u95f4\u7684\u9501\uff0c\u8fd9\u4e2a\u9501\u662f\u653e\u5728\u5185\u5b58\u4e2d\u7684\uff0c\u800c\u5bf9\u4e8e\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0c\u4e0d\u540c\u7684\u5e94\u7528(\u8fdb\u7a0b\u6216\u7ebf\u7a0b)\u90e8\u7f72\u5728\u4e0d\u540c\u7684\u670d\u52a1\u5668\u4e0a\uff0c\u8fd9\u6837\u5c31\u4e0d\u80fd\u901a\u8fc7\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\u6765\u8868\u793a\u9501\u3002<\/p>\n<p>\u5373\u7136\u5728\u4e00\u53f0\u670d\u52a1\u5668\u4e0a\u53ef\u4ee5\u901a\u8fc7\u5185\u5b58\u8fd9\u5757\u5171\u4eab\u7684\u7a7a\u95f4\u6765\u8868\u793a\u9501\uff0c\u90a3\u4e48\u5bf9\u4e8e\u5206\u5e03\u5f0f\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0c\u53ef\u4ee5\u5171\u4eab\u5b58\u50a8\u7cfb\u7edf\u6765\u5b58\u50a8\u4e00\u4e2a\u5171\u4eab\u9501\uff0c\u8fd9\u5c31\u662f\u5206\u5e03\u5f0f\u9501\uff0c\u800c<code>Redis<\/code>\u4f5c\u4e3a\u5185\u5b58\u6570\u636e\u5e93\uff0c\u6267\u884c\u975e\u5e38\u5feb\uff0c\u5f88\u9002\u5408\u4f5c\u4e3a\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u5171\u4eab\u5b58\u50a8\u7cfb\u7edf\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167244661963af829b86fb4.png\" class=\"aligncenter\"><\/p>\n<h2>2. \u4f7f\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501<\/h2>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u9501\u6765\u8bf4\uff0c\u5176\u5b9e\u53ea\u6709\u4e24\u4e2a\u64cd\u4f5c\uff0c\u52a0\u9501\u548c\u91ca\u653e\u9501\uff0c\u4e0b\u9762\u6211\u4eec\u770b\u6765\u770b\u901a\u8fc7<code>Redis<\/code>\u8981\u600e\u4e48\u5b9e\u73b0\uff1f<\/p>\n<h3>2.1 \u52a0\u9501<\/h3>\n<p><code>Redis<\/code>\u7684<code>setnx<\/code>\u547d\u4ee4\u4f1a\u5224\u65ad\u952e\u503c\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\u5219\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c\uff0c\u5e76\u8fd4\u56de0\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u5e76\u8d4b\u503c\uff0c\u5e76\u8fd4\u56de1\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6267\u884c<code>setnx<\/code>\u4e3a\u4e00\u4e2a\u4ee3\u8868\u9501\u952e\u8bbe\u7f6e\u503c\uff0c\u5982\u679c\u80fd\u8bbe\u7f6e\u6210\u529f\uff0c\u5219\u8868\u793a\u83b7\u5f97\u9501\uff0c\u5931\u8d25\u5219\u65e0\u6cd5\u83b7\u5f97\u9501\u3002<\/p>\n<pre># \u4f7f\u7528key\u4e3alock\u6765\u8868\u793a\u4e00\u4e2a\u9501\nsetnx lock 1 <\/pre>\n<h3>2.2 \u91ca\u653e\u9501<\/h3>\n<p>\u5f53\u6267\u884c\u597d\u64cd\u4f5c\u4e4b\u540e\uff0c\u8981\u91ca\u653e\u9501\u7684\u65f6\u5019\u76f4\u63a5\u628a<code>Redis<\/code>\u91cc\u7684\u952e\u503c<code>lock<\/code>\u5220\u9664\u5c31\u53ef\u4ee5\u4e86\uff0c\u8fd9\u6837\u5176\u4ed6\u8fdb\u7a0b\u624d\u80fd\u901a\u8fc7<code>setnx<\/code>\u547d\u4ee4\u91cd\u65b0\u8bbe\u7f6e\u5e76\u83b7\u5f97\u8be5\u9501\u3002<\/p>\n<pre># \u91ca\u653e\u9501\ndel lock<\/pre>\n<p>\u901a\u8fc7\u4e0a\u9762\u4e24\u4e2a\u547d\u4ee4\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5206\u5e03\u5f0f\u9501\uff0c\u4f46\u8fd9\u91cc\u5c31\u51fa\u73b0\u4e86\u4e00\u4e2a\u95ee\u9898\uff1a\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u901a\u8fc7<code>setnx<\/code>\u547d\u4ee4\u52a0\u9501\u4e4b\u540e\uff0c\u5728\u6267\u884c\u5177\u4f53\u64cd\u4f5c\u51fa\u9519\u4e86\uff0c\u6ca1\u6709\u529e\u6cd5\u53ca\u65f6\u91ca\u653e\u9501\uff0c\u90a3\u4e48\u5176\u4ed6\u8fdb\u7a0b\u5c31\u65e0\u6cd5\u83b7\u5f97\u8be5\u9501\uff0c\u7cfb\u7edf\u4fbf\u65e0\u6cd5\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\uff0c\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u5c31\u662f\u4e3a\u9501\u8bbe\u7f6e\u4e00\u4e2a\u6709\u6548\u671f\uff0c\u5728\u8fd9\u4e2a\u6709\u6548\u671f\u4e4b\u540e\uff0c\u81ea\u52a8\u91ca\u653e\u9501\u3002<\/p>\n<h3>2.3 \u7ed9\u9501\u8bbe\u7f6e\u6709\u6548\u671f<\/h3>\n<p>\u7ed9\u9501\u8bbe\u7f6e\u6709\u6548\u671f\u975e\u5e38\u7b80\u5355\uff0c\u76f4\u63a5\u4f7f\u7528<code>Redis<\/code>\u7684<code>expire<\/code>\u547d\u4ee4\u5c31\u53ef\u4ee5\u4e86\uff0c\u5982\uff1a<\/p>\n<pre># \u52a0\u9501\nsetnx lock 1 \n# \u7ed9\u9501\u8bbe\u7f6e10s\u6709\u6548\u671f\nexpire lock 10 <\/pre>\n<p>\u4f46\u662f\uff0c\u73b0\u5728\u53c8\u51fa\u73b0\u53e6\u4e00\u4e2a\u95ee\u9898\u4e86\uff0c\u5982\u679c\u6211\u4eec\u5728\u8bbe\u7f6e\u4e86\u9501\u4e4b\u540e\uff0c\u6267\u884c<code>expire<\/code>\u547d\u4ee4\u4e4b\u524d\u8be5\u8fdb\u7a0b\u6302\u6389\u4e86\uff0c\u90a3\u4e48<code>expire<\/code>\u5c31\u6ca1\u6709\u6267\u884c\u6210\u529f\uff0c\u9501\u4e00\u6837\u662f\u6ca1\u6709\u88ab\u91ca\u653e\u6389\u7684\uff0c\u6240\u4ee5\u4e00\u5b9a\u8981\u4fdd\u8bc1\u4e0a\u9762\u4e24\u4e2a\u547d\u4ee4\u8981\u4e00\u8d77\u6267\u884c\uff0c\u600e\u4e48\u4fdd\u8bc1\u5462\uff1f<\/p>\n<p>\u6709\u4e24\u4e2a\u65b9\u6cd5\uff0c\u4e00\u4e2a\u662f\u4f7f\u7528<code>LUA<\/code>\u8bed\u8a00\u7f16\u5199\u7684\u811a\u672c\uff0c\u53e6\u4e00\u4e2a\u662f\u4f7f\u7528<code>Redis<\/code>\u7684<code>set<\/code>\u547d\u4ee4\uff0c<code>set<\/code>\u547d\u4ee4\u540e\u9762\u8ddf<code>nx<\/code>\u53c2\u6570\u540e\uff0c\u6267\u884c\u7684\u6548\u679c\u4e0e<code>setnx<\/code>\u4e00\u81f4\uff0c\u4e14<code>set<\/code>\u547d\u4ee4\u53ef\u4ee5\u8ddf<code>ex<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>set<\/code>\u547d\u4ee4\u628a<code>setnx<\/code>\u548c<code>expire<\/code>\u4e24\u4e2a\u5408\u5e76\u5728\u4e00\u8d77\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u8bc1\u6267\u884c\u7684\u539f\u5b50\u6027\u4e86\u3002<\/p>\n<pre># \u5224\u65ad\u662f\u5426\u952e\u503c\u662f\u5426\u5b58\u5728\uff0cex\u540e\u9762\u8ddf\u7740\u7684\u662f\u952e\u503c\u7684\u6709\u6548\u671f,10s\nset lock 1 nx ex 10 <\/pre>\n<p>\u89e3\u51b3\u4e86\u9501\u7684\u6709\u6548\u95ee\u9898\uff0c\u73b0\u5728\u6211\u4eec\u518d\u6765\u770b\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167244661963af829bb4808.png\" class=\"aligncenter\"><\/p>\n<p>\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u73b0\u5728\u6709<code>A<\/code>\uff0c<code>B<\/code>\uff0c<code>C<\/code>\u4e09\u4e2a\u4e0d\u540c\u670d\u52a1\u5668\u4e0a\u7684\u8fdb\u7a0b\u5728\u6267\u884c\u67d0\u4e2a\u64cd\u4f5c\u90fd\u9700\u8981\u83b7\u5f97\u9501\uff0c\u6267\u884c\u540e\u8981\u91ca\u653e\u9501\u3002<\/p>\n<p>\u73b0\u5728\u7684\u60c5\u51b5\u662f<code>\u8fdb\u7a0bA<\/code>\u6267\u884c\u7b2c2\u6b65\u65f6\u5361\u987f\u4e86(\u4e0a\u9762\u7eff\u8272\u533a\u57df\u6240\u793a)\uff0c\u4e14\u65f6\u95f4\u8d85\u51fa\u4e86\u9501\u6709\u6548\u671f\uff0c\u6240\u4ee5<code>\u8fdb\u7a0bA<\/code>\u8bbe\u7f6e\u7684\u9501\u81ea\u52a8\u91ca\u653e\u4e86\uff0c\u8fd9\u65f6\u5019<code>\u8fdb\u7a0bB<\/code>\u83b7\u5f97\u4e86\u9501\uff0c\u5e76\u5f00\u59cb\u6267\u884c\u64cd\u4f5c\uff0c\u4f46\u7531\u4e8e<code>\u8fdb\u7a0bA<\/code>\u53ea\u662f\u5361\u987f\u4e86\u800c\u5df2\uff0c\u6240\u4ee5\u4f1a\u7ee7\u7eed\u6267\u884c\u7684\u65f6\u5019\uff0c\u5728\u7b2c3\u6b65\u7684\u65f6\u5019\u4f1a\u624b\u52a8\u91ca\u653e\u9501\uff0c\u4f46\u662f\u8fd9\u4e2a\u65f6\u5019\uff0c\u9501\u7531<code>\u7ebf\u7a0bB<\/code>\u6240\u62e5\u6709\uff0c<strong>\u4e5f\u5c31\u662f\u8bf4\u8fdb\u7a0bA\u5220\u9664\u7684\u4e0d\u662f\u81ea\u5df1\u7684\u9501\uff0c\u800c\u8fdb\u7a0bB\u7684\u9501<\/strong>\uff0c\u8fd9\u65f6\u5019<code>\u8fdb\u7a0bB<\/code>\u8fd8\u6ca1\u6267\u884c\u5b8c\uff0c\u4f46\u9501\u88ab\u91ca\u653e\u540e\uff0c<code>\u8fdb\u7a0bC<\/code>\u53ef\u4ee5\u52a0\u9501\uff0c<strong>\u4e5f\u5c31\u662f\u8bf4\u7531\u4e8e\u8fdb\u7a0bA\u5361\u987f\u91ca\u653e\u9519\u4e86\u9501\uff0c\u5bfc\u81f4\u8fdb\u7a0bB\u548c\u8fdb\u7a0bC\u53ef\u4ee5\u540c\u65f6\u83b7\u5f97\u9501<\/strong>\u3002<\/p>\n<p>\u600e\u4e48\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\u5462\uff1f\u5982\u4f55\u533a\u5206\u5176\u4ed6\u8fdb\u7a0b\u7684\u9501\uff0c\u907f\u514d\u5220\u9664\u5176\u4ed6\u8fdb\u7a0b\u7684\u9501\u5462\uff1f\u7b54\u6848\u5c31\u662f\u6bcf\u4e2a\u8fdb\u7a0b\u5728\u52a0\u9501\u7684\u65f6\u5019\uff0c\u7ed9\u9501\u8bbe\u7f6e\u4e00\u4e2a\u552f\u4e00\u503c\uff0c\u5e76\u5728\u91ca\u653e\u9501\u7684\u65f6\u5019\uff0c\u5224\u65ad\u662f\u4e0d\u662f\u81ea\u5df1\u8bbe\u7f6e\u7684\u9501\u3002<\/p>\n<h3>2.4 \u7ed9\u9501\u8bbe\u7f6e\u552f\u4e00\u503c<\/h3>\n<p>\u7ed9\u9501\u8bbe\u7f6e\u552f\u4e00\u503c\u7684\u65f6\u5019\uff0c\u4e00\u6837\u662f\u4f7f\u7528<code>set<\/code>\u547d\u4ee4\uff0c\u552f\u4e00\u7684\u4e0d\u540c\u662f\u5c06\u952e\u503c1\u6539\u4e3a\u4e00\u4e2a\u968f\u673a\u751f\u6210\u7684\u552f\u4e00\u503c\uff0c\u6bd4\u5982uuid\u3002<\/p>\n<pre> # rand_uid\u8868\u793a\u552f\u4e00id\nset lock rand_id nx ex 10<\/pre>\n<p>\u5f53\u9501\u91cc\u7684\u503c\u7531\u8fdb\u7a0b\u8bbe\u7f6e\u540e\uff0c\u91ca\u653e\u9501\u7684\u65f6\u5019\uff0c\u5c31\u9700\u8981\u5224\u65ad\u9501\u662f\u4e0d\u662f\u81ea\u5df1\u7684\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u901a\u8fc7<code>Redis<\/code>\u7684<code>get<\/code>\u547d\u4ee4\u83b7\u5f97\u9501\u7684\u503c<\/li>\n<li>\u6839\u636e\u83b7\u5f97\u7684\u503c\uff0c\u5224\u65ad\u9501\u662f\u4e0d\u662f\u81ea\u5df1\u8bbe\u7f6e\u7684<\/li>\n<li>\u5982\u679c\u662f\uff0c\u901a\u8fc7<code>del<\/code>\u547d\u4ee4\u91ca\u653e\u9501\u3002<\/li>\n<\/ul>\n<p>\u6b64\u65f6\u6211\u4eec\u770b\u5230\uff0c\u91ca\u653e\u9501\u9700\u8981\u6267\u884c\u4e09\u4e2a\u64cd\u4f5c\uff0c\u5982\u679c\u4e09\u4e2a\u64cd\u4f5c\u4f9d\u6b21\u6267\u884c\u7684\u8bdd\uff0c\u662f\u6ca1\u6709\u529e\u6cd5\u4fdd\u8bc1\u539f\u5b50\u6027\u7684\uff0c\u6bd4\u5982<code>\u8fdb\u7a0bA<\/code>\u5728\u6267\u884c\u5230\u7b2c2\u6b65\u540e\uff0c\u51c6\u5907\u5f00\u59cb\u6267\u884c<code>del<\/code>\u547d\u4ee4\u65f6\uff0c\u800c\u9501\u7531\u65f6\u6709\u6548\u671f\u5230\u4e86\uff0c\u88ab\u81ea\u52a8\u91ca\u653e\u4e86\uff0c\u5e76\u88ab\u5176\u4ed6\u670d\u52a1\u5668\u4e0a\u7684<code>\u8fdb\u7a0bB<\/code>\u83b7\u5f97\u9501\uff0c\u4f46\u8fd9\u65f6\u5019<code>\u7ebf\u7a0bA<\/code>\u6267\u884c<code>del<\/code>\u8fd8\u662f\u628a<code>\u7ebf\u7a0bB<\/code>\u7684\u9501\u7ed9\u5220\u6389\u4e86\u3002<\/p>\n<p>\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u529e\u6cd5\u5c31\u662f\u4fdd\u8bc1\u4e0a\u8ff0\u4e09\u4e2a\u64cd\u4f5c\u6267\u884c\u7684\u539f\u5b50\u6027\uff0c\u5373\u5728\u6267\u884c\u91ca\u653e\u9501\u7684\u4e09\u4e2a\u64cd\u4f5c\u4e2d\uff0c\u5176\u4ed6\u8fdb\u7a0b\u4e0d\u53ef\u4ee5\u83b7\u5f97\u9501\uff0c\u60f3\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u9700\u8981\u4f7f\u7528\u5230LUA\u811a\u672c\u3002<\/p>\n<h3>2.5 \u901a\u8fc7LUA\u811a\u672c\u5b9e\u73b0\u91ca\u653e\u9501\u7684\u539f\u5b50\u6027<\/h3>\n<p><code>Redis<\/code>\u652f\u6301<code>LUA<\/code>\u811a\u672c\uff0c<code>LUA<\/code>\u811a\u91cc\u7684\u4ee3\u7801\u6267\u884c\u7684\u65f6\u5019\uff0c\u5176\u4ed6\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\u4e0d\u4f1a\u88ab\u6267\u884c\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u811a\u672c\u8fdb\u884c\u9501\u7684\u91ca\u653e\uff1a<\/p>\n<pre>if redis.call(\"get\",KEYS[1]) == ARGV[1] then \n  return redis.call(\"del\",KEYS[1])\nelse \n  return 0\nend<\/pre>\n<p>\u5c06\u4e0a\u8ff0\u811a\u672c\u4fdd\u5b58\u4e3a\u811a\u672c\u540e\uff0c\u53ef\u4ee5\u8c03\u7528<code>Redis<\/code>\u5ba2\u6237\u7aef\u547d\u4ee4<code>redis-cli<\/code>\u6765\u6267\u884c\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre># lock\u4e3akey,rand_id\u8868\u793akey\u91cc\u4fdd\u5b58\u7684\u503c\nredis-cli --eval unlock.lua lock , rand_id <\/pre>\n<h2>3. \u5c0f\u7ed3<\/h2>\n<p>\u65e0\u8bba\u662f\u672c\u5730\u9501\u8fd8\u662f\u5206\u5e03\u5f0f\u9501\uff0c\u9501\u7684\u672c\u8d28\u5c31\u662f\u4e00\u4e2a\u5171\u4eab\u7684\u53d8\u91cf\uff0c\u53ea\u662f\u5728\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u65f6\u5019\uff0c\u628a\u8fd9\u4e2a\u53d8\u91cf\u79fb\u5230\u4e86<code>Redis<\/code>\u670d\u52a1\u5668\u6240\u5728\u7684\u5185\u5b58\u4e2d\u3002<\/p>\n<p><strong>\u5728\u4e0a\u9762\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u8fc7\u7a0b\u4e2d\u6211\u4eec\u78b0\u5230\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u95ee\u9898\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u4f55\u4fdd\u8bc1\u52a0\u9501\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff1f<\/li>\n<li>\u5982\u4f55\u4fdd\u8bc1\u8fdb\u7a0b\u5d29\u6e83\u81ea\u52a8\u91ca\u653e\u9501\uff1f<\/li>\n<li>\u5982\u4f55\u907f\u514d\u5220\u9519\u5176\u4ed6\u8fdb\u7a0b\u7684\u9501\uff1f<\/li>\n<li>\u5982\u4f55\u4fdd\u8bc1\u91ca\u653e\u9501\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff1f<\/li>\n<\/ul>\n<p>\u5728\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u7684\u65f6\u5019\uff0c\u6211\u4eec\u4e5f\u4e00\u6b65\u6b65\u5b8c\u5584\u4e00\u4e2a\u53ef\u4ee5\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5e94\u7528\u7684<code>Redis<\/code>\u5206\u5e03\u5f0f\u9501\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u6587\u8be6\u89e3\u5982\u4f55\u4f7f\u7528Redis\u5b9e\u73b0\u5206&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201180","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201180"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201180\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}