{"id":263,"date":"2023-02-24T06:19:45","date_gmt":"2023-02-23T22:19:45","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=263"},"modified":"2023-04-30T15:17:41","modified_gmt":"2023-04-30T07:17:41","slug":"redis-script","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/redis-script\/","title":{"rendered":"Redis \u811a\u672c"},"content":{"rendered":"<p>Redis \u811a\u672c\u4f7f\u7528 Lua \u89e3\u91ca\u5668\u6765\u6267\u884c\u811a\u672c\u3002Redis 2.6 \u7248\u672c\u901a\u8fc7\u5185\u5d4c\u652f\u6301 Lua \u73af\u5883\u3002\u6267\u884c\u811a\u672c\u7684\u5e38\u7528\u547d\u4ee4\u4e3a EVAL\u3002<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre><code class=\"language-bash\">EVAL script numkeys key [key ...] arg [arg ...]<\/code><\/pre>\n<p><!-- more --><\/p>\n<p><strong>\u5b9e\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a\u4e86 Redis \u811a\u672c\u5de5\u4f5c\u8fc7\u7a0b\uff1a<\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; EVAL &quot;return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}&quot; 2 key1 key2 first second\n\n1) &quot;key1&quot;\n2) &quot;key2&quot;\n3) &quot;first&quot;\n4) &quot;second&quot;<\/code><\/pre>\n<h2>Redis \u811a\u672c\u547d\u4ee4<\/h2>\n<h3>EVAL script numkeys key [key &#8230;] arg [arg &#8230;]<\/h3>\n<p>\u6267\u884c Lua \u811a\u672c<\/p>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; EVAL script numkeys key [key ...] arg [arg ...] <\/code><\/pre>\n<ul>\n<li><code>script<\/code>\uff1a\u53c2\u6570\u662f\u4e00\u6bb5 Lua 5.1 \u811a\u672c\u7a0b\u5e8f\u3002\u811a\u672c\u4e0d\u5fc5(\u4e5f\u4e0d\u5e94\u8be5)\u5b9a\u4e49\u4e3a\u4e00\u4e2a Lua \u51fd\u6570<\/li>\n<li><code>numkeys<\/code>\uff1a\u7528\u4e8e\u6307\u5b9a\u952e\u540d\u53c2\u6570\u7684\u4e2a\u6570<\/li>\n<li><code>key [key ...]<\/code>\uff1a\u4ece EVAL \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u5f00\u59cb\u7b97\u8d77\uff0c\u8868\u793a\u5728\u811a\u672c\u4e2d\u6240\u7528\u5230\u7684\u90a3\u4e9b Redis \u952e(key)\uff0c\u8fd9\u4e9b\u952e\u540d\u53c2\u6570\u53ef\u4ee5\u5728 Lua \u4e2d\u901a\u8fc7\u5168\u5c40\u53d8\u91cf KEYS \u6570\u7ec4\uff0c\u7528 1 \u4e3a\u57fa\u5740\u7684\u5f62\u5f0f\u8bbf\u95ee( KEYS[1]\uff0cKEYS[2]\uff0c\u4ee5\u6b64\u7c7b\u63a8)<\/li>\n<li><code>arg [arg ...]<\/code>\uff1a\u9644\u52a0\u53c2\u6570\uff0c\u5728 Lua \u4e2d\u901a\u8fc7\u5168\u5c40\u53d8\u91cf ARGV \u6570\u7ec4\u8bbf\u95ee\uff0c\u8bbf\u95ee\u7684\u5f62\u5f0f\u548c KEYS \u53d8\u91cf\u7c7b\u4f3c(ARGV[1]\u3001ARGV[2]\uff0c\u8bf8\u5982\u6b64\u7c7b)<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; eval &quot;return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}&quot; 2 key1 key2 first second\n1) &quot;key1&quot;\n2) &quot;key2&quot;\n3) &quot;first&quot;\n4) &quot;second&quot;<\/code><\/pre>\n<h3>EVALSHA sha1 numkeys key [key &#8230;] arg [arg &#8230;]<\/h3>\n<p>\u6839\u636e\u7ed9\u5b9a\u7684 sha1 \u6821\u9a8c\u7801\uff0c\u6267\u884c\u7f13\u5b58\u5728\u670d\u52a1\u5668\u4e2d\u7684\u811a\u672c<\/p>\n<ul>\n<li>\u5c06\u811a\u672c\u7f13\u5b58\u5230\u670d\u52a1\u5668\u7684\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7 SCRIPT LOAD \u547d\u4ee4\u8fdb\u884c<\/li>\n<li>\u8fd9\u4e2a\u547d\u4ee4\u7684\u5176\u4ed6\u5730\u65b9\uff0c\u6bd4\u5982\u53c2\u6570\u7684\u4f20\u5165\u65b9\u5f0f\uff0c\u90fd\u548c EVAL \u547d\u4ee4\u4e00\u6837<\/li>\n<\/ul>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; EVALSHA sha1 numkeys key [key ...] arg [arg ...]<\/code><\/pre>\n<ul>\n<li>sha1\uff1a\u901a\u8fc7 SCRIPT LOAD \u751f\u6210\u7684 sha1 \u6821\u9a8c\u7801<\/li>\n<li>numkeys\uff1a\u7528\u4e8e\u6307\u5b9a\u952e\u540d\u53c2\u6570\u7684\u4e2a\u6570<\/li>\n<li>key [key &#8230;]\uff1a\u4ece EVAL \u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u5f00\u59cb\u7b97\u8d77\uff0c\u8868\u793a\u5728\u811a\u672c\u4e2d\u6240\u7528\u5230\u7684\u90a3\u4e9b Redis \u952e(key)\uff0c\u8fd9\u4e9b\u952e\u540d\u53c2\u6570\u53ef\u4ee5\u5728 Lua \u4e2d\u901a\u8fc7\u5168\u5c40\u53d8\u91cf KEYS \u6570\u7ec4\uff0c\u7528 1 \u4e3a\u57fa\u5740\u7684\u5f62\u5f0f\u8bbf\u95ee( KEYS[1]\uff0cKEYS[2]\uff0c\u4ee5\u6b64\u7c7b\u63a8)<\/li>\n<li>arg [arg &#8230;]\uff1a\u9644\u52a0\u53c2\u6570\uff0c\u5728 Lua \u4e2d\u901a\u8fc7\u5168\u5c40\u53d8\u91cf ARGV \u6570\u7ec4\u8bbf\u95ee\uff0c\u8bbf\u95ee\u7684\u5f62\u5f0f\u548c KEYS \u53d8\u91cf\u7c7b\u4f3c( ARGV[1]\u3001ARGV[2]\uff0c\u8bf8\u5982\u6b64\u7c7b)<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; SCRIPT LOAD &quot;return &#039;hello moto&#039;&quot;\n&quot;232fd51614574cf0867b83d384a5e898cfd24e5a&quot;\n\nredis 127.0.0.1:6379&gt; EVALSHA &quot;232fd51614574cf0867b83d384a5e898cfd24e5a&quot; 0\n&quot;hello moto&quot;<\/code><\/pre>\n<h3>SCRIPT EXISTS sha1 [sha1 &#8230;]<\/h3>\n<p>\u67e5\u770b\u6307\u5b9a\u7684\u811a\u672c\u662f\u5426\u5df2\u7ecf\u88ab\u4fdd\u5b58\u5728\u7f13\u5b58\u5f53\u4e2d<\/p>\n<ul>\n<li>\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b 0 \u548c 1\uff0c\u524d\u8005\u8868\u793a\u811a\u672c\u4e0d\u5b58\u5728\u4e8e\u7f13\u5b58\uff0c\u540e\u8005\u8868\u793a\u811a\u672c\u5df2\u7ecf\u5b58\u5728\u7f13\u5b58\u91cc<\/li>\n<li>\u5217\u8868\u4e2d\u7684\u5143\u7d20\u548c\u7ed9\u5b9a\u7684 SHA1 \u6821\u9a8c\u548c\u4fdd\u6301\u5bf9\u5e94\u5173\u7cfb\uff0c\u6bd4\u5982\u5217\u8868\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u7684\u503c\u5c31\u8868\u793a\u7b2c\u4e09\u4e2a SHA1 \u6821\u9a8c\u548c\u6240\u6307\u5b9a\u7684\u811a\u672c\u5728\u7f13\u5b58\u4e2d\u7684\u72b6\u6001<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; SCRIPT LOAD &quot;return &#039;hello moto&#039;&quot;    # \u8f7d\u5165\u4e00\u4e2a\u811a\u672c\n&quot;232fd51614574cf0867b83d384a5e898cfd24e5a&quot;\n\nredis 127.0.0.1:6379&gt; SCRIPT EXISTS 232fd51614574cf0867b83d384a5e898cfd24e5a\n1) (integer) 1\n\nredis 127.0.0.1:6379&gt; SCRIPT FLUSH     # \u6e05\u7a7a\u7f13\u5b58\nOK\n\nredis 127.0.0.1:6379&gt; SCRIPT EXISTS 232fd51614574cf0867b83d384a5e898cfd24e5a\n1) (integer) 0<\/code><\/pre>\n<h3>SCRIPT FLUSH<\/h3>\n<p>\u4ece Lua \u811a\u672c\u7f13\u5b58\u4e2d\u79fb\u9664\u6240\u6709\u7f13\u5b58<\/p>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; SCRIPT FLUSH\nOK<\/code><\/pre>\n<h3>SCRIPT KILL<\/h3>\n<p>\u6740\u6b7b\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684 Lua \u811a\u672c<\/p>\n<ul>\n<li>Script kill \u547d\u4ee4\u7528\u4e8e\u6740\u6b7b\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684 Lua \u811a\u672c\uff0c\u5f53\u4e14\u4ec5\u5f53\u8fd9\u4e2a\u811a\u672c\u6ca1\u6709\u6267\u884c\u8fc7\u4efb\u4f55\u5199\u64cd\u4f5c\u65f6\uff0c\u8fd9\u4e2a\u547d\u4ee4\u624d\u751f\u6548<\/li>\n<li>\u8fd9\u4e2a\u547d\u4ee4\u4e3b\u8981\u7528\u4e8e\u7ec8\u6b62\u8fd0\u884c\u65f6\u95f4\u8fc7\u957f\u7684\u811a\u672c\uff0c\u6bd4\u5982\u4e00\u4e2a\u56e0\u4e3a BUG \u800c\u53d1\u751f\u65e0\u9650\u5faa\u73af\u7684\u811a\u672c<\/li>\n<li>SCRIPT KILL \u6267\u884c\u4e4b\u540e\uff0c\u5f53\u524d\u6b63\u5728\u8fd0\u884c\u7684\u811a\u672c\u4f1a\u88ab\u6740\u6b7b\uff0c\u6267\u884c\u8fd9\u4e2a\u811a\u672c\u7684\u5ba2\u6237\u7aef\u4f1a\u4ece EVAL \u547d\u4ee4\u7684\u963b\u585e\u5f53\u4e2d\u9000\u51fa\uff0c\u5e76\u6536\u5230\u4e00\u4e2a\u9519\u8bef\u4f5c\u4e3a\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; SCRIPT KILL\nOK<\/code><\/pre>\n<h3>SCRIPT LOAD script<\/h3>\n<p>\u5c06\u811a\u672c script \u6dfb\u52a0\u5230\u811a\u672c\u7f13\u5b58\u4e2d\uff0c\u4f46\u5e76\u4e0d\u7acb\u5373\u6267\u884c\u8fd9\u4e2a\u811a\u672c<\/p>\n<ul>\n<li>Script Load \u547d\u4ee4\u7528\u4e8e\u5c06\u811a\u672c script \u6dfb\u52a0\u5230\u811a\u672c\u7f13\u5b58\u4e2d\uff0c\u4f46\u5e76\u4e0d\u7acb\u5373\u6267\u884c\u8fd9\u4e2a\u811a\u672c<\/li>\n<li>EVAL \u547d\u4ee4\u4e5f\u4f1a\u5c06\u811a\u672c\u6dfb\u52a0\u5230\u811a\u672c\u7f13\u5b58\u4e2d\uff0c\u4f46\u662f\u5b83\u4f1a\u7acb\u5373\u5bf9\u8f93\u5165\u7684\u811a\u672c\u8fdb\u884c\u6c42\u503c<\/li>\n<li>\u5982\u679c\u7ed9\u5b9a\u7684\u811a\u672c\u5df2\u7ecf\u5728\u7f13\u5b58\u91cc\uff0c\u90a3\u4e48\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c<\/li>\n<li>\u5728\u811a\u672c\u88ab\u52a0\u5165\u5230\u7f13\u5b58\u4e4b\u540e\uff0c\u901a\u8fc7 EVALSHA \u547d\u4ee4\uff0c\u53ef\u4ee5\u4f7f\u7528\u811a\u672c\u7684 SHA1 \u6821\u9a8c\u548c\u6765\u8c03\u7528\u8fd9\u4e2a\u811a\u672c<\/li>\n<li>\u811a\u672c\u53ef\u4ee5\u5728\u7f13\u5b58\u4e2d\u4fdd\u7559\u65e0\u9650\u957f\u7684\u65f6\u95f4\uff0c\u76f4\u5230\u6267\u884c SCRIPT FLUSH \u4e3a\u6b62<\/li>\n<\/ul>\n<pre><code class=\"language-bash\">redis 127.0.0.1:6379&gt; SCRIPT LOAD &quot;return 1&quot;\n&quot;e0e1f9fabfc9d4800c877a703b823ac0578ff8db&quot;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u811a\u672c\u4f7f\u7528 Lua \u89e3\u91ca\u5668\u6765\u6267\u884c\u811a\u672c\u3002Redis 2.6 \u7248\u672c\u901a\u8fc7\u5185\u5d4c\u652f\u6301 Lua \u73af\u5883\u3002\u6267\u884c\u811a\u672c\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[88],"class_list":["post-263","post","type-post","status-publish","format-standard","hentry","category-redis","tag-lua"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}