{"id":83197,"date":"2022-04-08T12:30:09","date_gmt":"2022-04-08T04:30:09","guid":{"rendered":"http:\/\/www.idc.net\/help\/83197\/"},"modified":"2022-04-08T12:30:09","modified_gmt":"2022-04-08T04:30:09","slug":"%e8%84%9a%e6%9c%acredis%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%8a%a8%e5%8c%96%e8%b0%83%e7%94%a8lua%e8%84%9a%e6%9c%ac%ef%bc%88redis%e8%87%aa%e5%8a%a8%e6%89%a7%e8%a1%8clua%ef%bc%89","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/83197\/","title":{"rendered":"\u811a\u672cRedis\u5b9e\u73b0\u81ea\u52a8\u5316\u8c03\u7528Lua\u811a\u672c\uff08redis\u81ea\u52a8\u6267\u884clua\uff09"},"content":{"rendered":"<p>Redis\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5185\u5b58\u6570\u636e\u5e93\uff0c\u5e38\u7528\u4e8e\u7f13\u5b58\u548c\u6d88\u606f\u961f\u5217\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u5b83\u8fd8\u652f\u6301\u811a\u672c\u8bed\u8a00Lua\u7684\u7f16\u5199\u548c\u6267\u884c\u3002\u5728Redis\u4e2d\uff0c\u4f7f\u7528Lua\u811a\u672c\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e9b\u81ea\u52a8\u5316\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u5b9a\u65f6\u4efb\u52a1\u3001\u76d1\u63a7\u7b49\u3002<\/p>\n<\/p>\n<p>Lua\u811a\u672c\u7684\u8fd0\u884c\u65b9\u5f0f\u7c7b\u4f3c\u4e8e\u51fd\u6570\u8c03\u7528\u3002\u9700\u8981\u5c06\u811a\u672c\u4ee3\u7801\u5b58\u5165Redis\u6570\u636e\u5e93\u4e2d\uff0c\u7136\u540e\u901a\u8fc7\u7279\u5b9a\u7684\u547d\u4ee4\u8c03\u7528\u811a\u672c\u5e76\u4f20\u5165\u53c2\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Lua\u811a\u672c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p>\u201c`lua<\/p>\n<p>local key1 = KEYS[1]<\/p>\n<p>local key2 = KEYS[2]<\/p>\n<p>local value1 = ARGV[1]<\/p>\n<p>local value2 = ARGV[2]<\/p>\n<\/p>\n<p>redis.call(\u2018SET\u2019, key1, value1)<\/p>\n<p>redis.call(\u2018SET\u2019, key2, value2)<\/p>\n<pre><br>\u8be5\u811a\u672c\u901a\u8fc7\u63a5\u6536\u4e24\u4e2a\u952e\u548c\u4e24\u4e2a\u503c\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u8fd9\u4e24\u7ec4\u952e\u503c\u5b58\u5165Redis\u6570\u636e\u5e93\u4e2d\u3002<br><br>\u5728Redis\u4e2d\u8c03\u7528\u8be5\u811a\u672c\u7684\u65b9\u5f0f\u662f\u4f7f\u7528`EVAL`\u547d\u4ee4\u3002\u8be5\u547d\u4ee4\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<br><br>```redis<br>EVAL script numkeys key [key ...] arg [arg ...]<br><\/pre>\n<\/p>\n<p>\u5176\u4e2d\uff0c`script`\u4e3a\u5f85\u6267\u884c\u7684Lua\u811a\u672c\uff0c`numkeys`\u5219\u8868\u793a\u63a5\u4e0b\u6765\u7684\u952e\u503c\u5bf9\u6570\u91cf\uff0c`key`\u5219\u4ee3\u8868\u9700\u8981\u4f20\u5165\u7684\u952e\u540d\uff0c`arg`\u8868\u793a\u9700\u8981\u4f20\u5165\u7684\u952e\u503c\u3002\u4ee5\u524d\u9762\u7684\u793a\u4f8b\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8c03\u7528\u8be5\u811a\u672c\uff1a<\/p>\n<\/p>\n<p>\u201c`redis<\/p>\n<p>EVAL \u201clocal key1 = KEYS[1]\\nlocal key2 = KEYS[2]\\nlocal value1 = ARGV[1]\\nlocal value2 = ARGV[2]\\n\\nredis.call(\u2018SET\u2019, key1, value1)\\nredis.call(\u2018SET\u2019, key2, value2)\u201d 2 key1 value1 key2 value2<\/p>\n<pre><br>\u4e0a\u8ff0\u547d\u4ee4\u4e2d\uff0c`2`\u4ee3\u8868\u63a5\u4e0b\u6765\u6709\u4e24\u7ec4\u952e\u503c\u5bf9\u9700\u8981\u4f20\u5165\uff0c`key1`\u548c`key2`\u5219\u5206\u522b\u4ee3\u8868\u4f20\u5165\u7684\u4e24\u4e2a\u952e\u540d\uff0c`value1`\u548c`value2`\u5219\u5206\u522b\u4ee3\u8868\u4f20\u5165\u7684\u4e24\u4e2a\u503c\u3002<br><br>\u9664\u4e86\u5e38\u89c4\u7684`SET`\u64cd\u4f5c\uff0cLua\u811a\u672c\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e9b\u9ad8\u7ea7\u7684\u81ea\u52a8\u5316\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Lua\u811a\u672c\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u73b0\u6bcf\u79d2\u949f\u81ea\u589e1\u7684\u8ba1\u6570\u5668\u7684Lua\u811a\u672c\u793a\u4f8b\uff1a<br><br>```lua<br>local currentValue = tonumber(redis.call('GET', KEYS[1])) or 0<br>local newValue = currentValue + 1<br><br>redis.call('SET', KEYS[1], newValue)<br><br>return newValue<br><\/pre>\n<\/p>\n<p>\u8be5\u811a\u672c\u4e2d\uff0c\u9996\u5148\u4f7f\u7528`GET`\u64cd\u4f5c\u83b7\u53d6\u5f53\u524d\u8ba1\u6570\u5668\u7684\u503c\uff0c\u7136\u540e\u5c06\u8be5\u503c\u81ea\u589e1\uff0c\u5e76\u4f7f\u7528`SET`\u64cd\u4f5c\u5c06\u65b0\u503c\u5b58\u5165Redis\u6570\u636e\u5e93\u3002\u811a\u672c\u8fd4\u56de\u65b0\u7684\u8ba1\u6570\u5668\u503c\u3002<\/p>\n<\/p>\n<p>\u5728Redis\u4e2d\u8c03\u7528\u8be5\u811a\u672c\u7684\u65b9\u5f0f\u4e0e\u524d\u9762\u7684\u793a\u4f8b\u76f8\u4f3c\uff0c\u53ea\u9700\u5728\u547d\u4ee4\u4e2d\u4f20\u5165\u8ba1\u6570\u5668\u7684\u952e\u540d\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u547d\u4ee4\u5c06\u6bcf\u79d2\u949f\u81ea\u589e1\u7684\u8ba1\u6570\u5668\u5b58\u5165\u540d\u4e3a`counter`\u7684\u952e\u4e2d\uff1a<\/p>\n<\/p>\n<p>\u201c`redis<\/p>\n<p>EVAL \u201clocal currentValue = tonumber(redis.call(\u2018GET\u2019, KEYS[1])) or 0\\nlocal newValue = currentValue + 1\\n\\nredis.call(\u2018SET\u2019, KEYS[1], newValue)\\n\\nreturn newValue\u201d 1 counter<\/p>\n<pre><br>\u4ee5\u4e0a\u662f\u4f7f\u7528Lua\u811a\u672c\u5b9e\u73b0\u81ea\u52a8\u5316\u64cd\u4f5c\u7684\u57fa\u672c\u6b65\u9aa4\u548c\u793a\u4f8b\u3002\u901a\u8fc7\u6df1\u5165\u5b66\u4e60Lua\u8bed\u8a00\u548cRedis\u547d\u4ee4\u7684\u4f7f\u7528\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u548c\u7075\u6d3b\u7684\u81ea\u52a8\u5316\u64cd\u4f5c\u3002<br><\/pre>\n<p><a href=\"http:https:\/\/idc.net\/\">\u9999\u6e2f\u670d\u52a1\u5668<\/a>\u9996\u9009\u540e\u6d6a\u4e91\uff0c2H2G\u9996\u670810\u5143\u5f00\u901a\u3002<br \/>\u540e\u6d6a\u4e91\uff08www.IDC.Net\uff09\u63d0\u4f9b\u7b80\u5355\u597d\u7528\uff0c\u4ef7\u683c\u539a\u9053\u7684\u9999\u6e2f\/\u7f8e\u56fd\u4e91\u670d\u52a1\u5668\u548c\u72ec\u7acb\u670d\u52a1\u5668\u3002IDC+ISP+ICP\u8d44\u8d28\u3002ARIN\u548cAPNIC\u4f1a\u5458\u3002\u6210\u719f\u6280\u672f\u56e2\u961f15\u5e74\u884c\u4e1a\u7ecf\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5185\u5b58\u6570\u636e\u5e93\uff0c\u5e38\u7528\u4e8e\u7f13\u5b58\u548c\u6d88\u606f\u961f\u5217\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u5b83\u8fd8\u652f\u6301\u811a\u672c\u8bed\u8a00Lua\u7684\u7f16\u5199\u548c\u6267\u884c\u3002\u5728Red [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7038],"tags":[],"class_list":["post-83197","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/83197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=83197"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/83197\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=83197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=83197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=83197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}