{"id":593,"date":"2021-04-05T12:23:08","date_gmt":"2021-04-05T04:23:08","guid":{"rendered":"https:\/\/www.chenqing.work\/?p=593"},"modified":"2021-09-01T18:55:10","modified_gmt":"2021-09-01T10:55:10","slug":"%e5%9c%a8-redis-%e9%87%8c%e6%89%a7%e8%a1%8c-lua-%e8%84%9a%e6%9c%ac%e7%9a%84%e8%b7%b3%e5%9d%91%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.chenqing.work\/?p=593","title":{"rendered":"\u5728 redis \u91cc\u6267\u884c lua \u811a\u672c\u7684\u8df3\u5751\u6307\u5357"},"content":{"rendered":"\n<p>\u6700\u8fd1\u57fa\u4e8e Kong \u505a\u4e00\u4e9b\u4e1c\u897f\uff0c\u5176\u4e2d\u4e00\u4e2a\u63d2\u4ef6\u7684\u7b97\u6cd5\u8bbe\u8ba1\u9700\u8981\u53bb redis \u505a\u591a\u6b21\u8bfb\u53d6\u5224\u65ad\uff0c\u4e8e\u662f\u51c6\u5907\u628a\u8fd9\u90e8\u5206\u5b9e\u73b0\u903b\u8f91\u653e\u5230 redis \u4fa7\uff0c\u7701\u53bb\u591a\u6b21\u8bbf\u95ee redis \u7684\u7f51\u7edc\u5f00\u9500\u3002<\/p>\n\n\n\n<p>redis \u7684\u4e00\u822c\u7528\u6cd5\u662f\u5bf9\u5176\u57fa\u7840\u6570\u636e\u7ed3\u6784\u7684\u8bfb\u5199\uff0c\u56e0\u4e3a\u6570\u636e\u90fd\u653e\u5728\u5185\u5b58\uff0c\u8bfb\u5199\u975e\u5e38\u8fc5\u901f\uff0c\u8fdb\u4e00\u6b65\u8fd8\u652f\u6301\u6267\u884c lua \u811a\u672c\uff0credis \u63d0\u4f9b\u4e86\u4e00\u4e2a lua \u7684\u6267\u884c\u73af\u5883\uff08\u6ce8\u610f\u662f 5.1\uff09\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5728 lua \u811a\u672c\u4e2d\u8c03\u7528 redis \u547d\u4ee4\u7684\u65b9\u6cd5\uff08redis.call\u3001redis.pcall\u3001redis.log\u3001\u2026)\uff0c\u53ef\u4ee5\u901a\u8fc7 script load \u6216 eval \u547d\u4ee4\u6765\u8f7d\u5165 lua \u811a\u672c\uff0c\u8f7d\u5165\u540e\u5f97\u5230\u4e00\u4e2a sha1 \u503c\uff0c\u5ba2\u6237\u7aef\u5c31\u53ef\u4ee5\u901a\u8fc7 evalsha &lt;sha1&gt; \u6765\u8c03\u7528\u8fd9\u4e2a\u811a\u672c<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nevalsha &amp;lt;sha1&gt; &amp;lt;key_num&gt; &#x5B;&amp;lt;key1&gt; &amp;lt;key2&gt; ...] &amp;lt;arg1&gt; &amp;lt;arg2&gt; ...\n<\/pre><\/div>\n\n\n<p>\u4ece\u7f51\u53cb\u7684\u5171\u8bc6\u6765\u770b\uff0c\u6700\u6743\u5a01\u7684\u4ecb\u7ecd lua \u7684\u4e66\u7c4d\u662f \u300aProgramming in Lua <s>3<\/s>4rd\u300b\uff0c\u5199\u7684\u8fc7\u7a0b\u4e2d\u5bf9\u7167\u7740\u770b\u5c31\u884c\u3002<\/p>\n\n\n\n<p>\u5728\u811a\u672c\u653e\u5230 redis \u540e\u53d1\u73b0\u6709 3 \u4e2a\u5751\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u4e2a\u5751\u5c31\u662f redis \u6240\u652f\u6301\u7684 lua \u7248\u672c\u8fc7\u4f4e\u7684\u95ee\u9898\uff0c\u8fd8\u8bb0\u5f97\u4e00\u5f00\u59cb\u6211\u63d0\u5230\uff0credis \u5185\u7f6e\u7684 lua \u6267\u884c\u73af\u5883\u7248\u672c\u662f5.1\uff0c\u800c 5.1 \u548c 5.2+ \u7684\u5176\u4e2d\u4e00\u4e2a\u533a\u522b\u662f\u540e\u8005\u589e\u52a0\u4e86\u5bf9 <a href=\"http:\/\/lua-users.org\/wiki\/GotoStatement\" data-type=\"URL\" data-id=\"http:\/\/lua-users.org\/wiki\/GotoStatement\">goto <\/a> \u7684\u8bed\u6cd5\u652f\u6301\uff08\u867d\u7136 goto \u662f\u4e07\u6076\u4e4b\u6e90\uff0c\u5f53\u5e74\u5199\u6c47\u7f16\u4f5c\u4e1a\u7684\u65f6\u5019\u4e5f\u662f goto \u5230\u4e0a\u5934\uff09\u3002\u800c\u6211\u5199\u7684\u65f6\u5019\u5148\u7528 java \u5b9e\u73b0\u4e86\u4e00\u904d\uff08\u987a\u624b\u7528\u4e86 continue\uff09\uff0c\u518d\u7ffb\u8bd1\u4e3a lua \u4ee3\u7801\uff0c\u56e0\u4e3a\u53d1\u73b0 lua \u8bed\u6cd5\u6ca1\u6709 continue\uff0c\u6240\u4ee5\u6309\u7167\u7f51\u4e0a\u65b9\u6848\u7528\u4e86 goto \u6765\u66ff\u4ee3\uff08repeat \u7684\u65b9\u6848\u591a\u5957\u4e86\u4e00\u5c42\u5faa\u73af\uff0c\u611f\u89c9\u4e0d\u591f\u76f4\u89c2\uff09\uff0c\u4e0d\u5e78\u7684\u662f\u8c03\u8bd5\u7684\u65f6\u5019\u7528\u7684 lua5.3 \u6709 goto\uff0c\u8fd9\u4e0b\u5c31\u6709\u70b9\u5c34\u5c2c\u3002<\/p>\n\n\n\n<p>\u7ffb\u4e86 so\u3001github\uff0c\u5728 2011 \u5e74\u7684\u65f6\u5019\u5c31\u6709\u7f51\u53cb\u63d0\u4e86 PR\uff0c\u8bf7\u793e\u533a\u5c06 redis \u7684 lua \u7248\u672c\u5347\u7ea7\u5230 5.2\uff0c\u4f46\u7ef4\u62a4\u8005\u8ba4\u4e3a 5.1 \u5c31\u591f\u7528\u4e86\uff08\u4e00\u4e2a\u4e2a\u7684\uff0c\u8981\u4ec0\u4e48\u81ea\u884c\u8f66\uff09\uff0c\u4e8e\u662f\u5728\u6700\u65b0\u7684 redis 6.2.1 \u91cc\u8fd8\u662f lua5.1\u3002\u6700\u540e\u8c03\u6574\u4e86\u5206\u652f\u903b\u8f91\uff0c\u4e0a\u5cb8\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u4e2a\u5751\u662f lua \u7684\u53d8\u91cf\u4f5c\u7528\u8303\u56f4\uff0c\u653e\u5230 redis \u4ee5\u540e\u53d1\u73b0 redis \u7684 lua \u73af\u5883\u7981\u6b62\u64cd\u4f5c\u5168\u5c40\u53d8\u91cf\uff0c\u53ea\u80fd\u6539\u4e4b\uff0c\u4f20\u4e2a table \u5f15\u7528\u5f97\u4e86\u3002\u770b\u8d77\u6765\u5728 lua \u91cc\uff0c\u5168\u5c40\u53d8\u91cf\u662f\u4e2a\u5371\u9669\u7684\u7070\u8272\u5730\u5e26\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e09\u4e2a\u5751\u662f lua \u91cc\u7684\u6570\u7ec4\uff08\u6362\u4e86\u4e2a\u9a6c\u7532\u5176\u5b9e\u8fd8\u662f table\uff09\u8d77\u59cb\u4e0b\u6807\u4e3a 1\uff0c\u6539\u4e86\u51e0\u4e2a\u5faa\u73af\u4f53\uff0c\u5fcd\u4e0d\u4f4f\u505c\u4e0b\u6765\u60f3\u2014\u2014\u6211\u662f\u8c01\uff1f\u6211\u5728\u54ea\uff1f<\/p>\n\n\n\n<p>\u4f5c\u4e3a\u4e00\u4e2a\u5f31\u7c7b\u578b\u7684\u811a\u672c\u8bed\u8a00\uff0c\u5199\u7684\u65f6\u5019\u8fd8\u662f\u5f88\u723d\u7684\uff0c\u7279\u522b\u662f\u5bf9\u6bd4 java \u91cc\u8fd4\u56de\u591a\u503c\u7684\u90a3\u79cd\u626d\u634f\u7f9e\u6da9\u3002<\/p>\n\n\n\n<p>update\uff1a2021.9.1<\/p>\n\n\n\n<p>\u8fd8\u6709\u4e2a\u5730\u65b9\u8981\u6ce8\u610f\u4e0b\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u52a0\u8f7d\u65f6\uff0c\u4e0d\u80fd\u76f4\u63a5\u628a\u6587\u4ef6\u4f5c\u4e3a\u53c2\u6570\uff0c\u5f97\u628a\u5185\u5bb9\u53d6\u51fa\u6765\u4f5c\u4e3a\u8f93\u5165<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>redis-cli script load \"$(cat xxx.lua)\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u57fa\u4e8e Kong \u505a\u4e00\u4e9b\u4e1c\u897f\uff0c\u5176\u4e2d\u4e00\u4e2a\u63d2\u4ef6\u7684\u7b97\u6cd5\u8bbe\u8ba1\u9700\u8981\u53bb redis \u505a\u591a\u6b21\u8bfb\u53d6\u5224\u65ad\uff0c\u4e8e\u662f\u51c6\u5907\u628a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[33,34],"class_list":["post-593","post","type-post","status-publish","format-standard","hentry","category-code","tag-lua","tag-redis"],"_links":{"self":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=593"}],"version-history":[{"count":7,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/593\/revisions"}],"predecessor-version":[{"id":1039,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=\/wp\/v2\/posts\/593\/revisions\/1039"}],"wp:attachment":[{"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenqing.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}