{"id":162783,"date":"2024-11-04T17:58:16","date_gmt":"2024-11-04T09:58:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/162783\/"},"modified":"2024-11-04T17:58:17","modified_gmt":"2024-11-04T09:58:17","slug":"%e8%85%b3%e6%9c%ac%e8%a7%a3%e6%b1%ba%e4%b8%a6%e7%99%bc%e5%9c%a8redis%e4%b8%ad%e7%94%a8lua%e8%85%b3%e6%9c%ac%e8%a7%a3%e6%b1%ba%e4%b8%a6%e7%99%bc%e5%95%8f%e9%a1%8c%ef%bc%88redis%e7%94%a8lua%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/162783\/","title":{"rendered":"\u8173\u672c\u89e3\u6c7a\u4e26\u767c\u5728Redis\u4e2d\u7528Lua\u8173\u672c\u89e3\u6c7a\u4e26\u767c\u554f\u984c\uff08Redis\u7528Lua\uff09"},"content":{"rendered":"<h1 id=\"%e8%85%b3%e6%9c%ac%e8%a7%a3%e6%b1%ba%e4%b8%a6%e7%99%bc%e5%9c%a8redis%e4%b8%ad%e7%94%a8lua%e8%85%b3%e6%9c%ac%e8%a7%a3%e6%b1%ba%e4%b8%a6%e7%99%bc%e5%95%8f%e9%a1%8c%ef%bc%88redis%e7%94%a8lua%ef%bc%89-eMiVYHTgzG\">\u8173\u672c\u89e3\u6c7a\u4e26\u767c\u5728Redis\u4e2d\u7528Lua\u8173\u672c\u89e3\u6c7a\u4e26\u767c\u554f\u984c\uff08Redis\u7528Lua\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u4e26\u767c\u8655\u7406\u662f\u4e00\u500b\u91cd\u8981\u7684\u8b70\u984c\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u591a\u500b\u8acb\u6c42\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u89e3\u6c7a\u65b9\u6848\u4f86\u8655\u7406\u4e26\u767c\u554f\u984c\uff0c\u5176\u4e2d\u4f7f\u7528Lua\u8173\u672c\u662f\u4e00\u500b\u975e\u5e38\u6709\u6548\u7684\u65b9\u5f0f\u3002<\/p>\n<h2 id=\"redis%e8%88%87%e4%b8%a6%e7%99%bc%e5%95%8f%e9%a1%8c-eMiVYHTgzG\">Redis\u8207\u4e26\u767c\u554f\u984c<\/h2>\n<p>Redis\u662f\u4e00\u500b\u57fa\u65bc\u5167\u5b58\u7684\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u9ad8\u6027\u80fd\u548c\u4f4e\u5ef6\u9072\u7684\u7279\u6027\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u9700\u8981\u5feb\u901f\u6578\u64da\u5b58\u53d6\u7684\u5834\u666f\u3002\u7136\u800c\uff0c\u7576\u591a\u500b\u5ba2\u6236\u7aef\u540c\u6642\u5c0dRedis\u9032\u884c\u64cd\u4f5c\u6642\uff0c\u53ef\u80fd\u6703\u51fa\u73fe\u6578\u64da\u4e0d\u4e00\u81f4\u7684\u554f\u984c\uff0c\u9019\u5c31\u662f\u4e26\u767c\u554f\u984c\u3002<\/p>\n<h2 id=\"lua%e8%85%b3%e6%9c%ac%e7%9a%84%e5%84%aa%e5%8b%a2-eMiVYHTgzG\">Lua\u8173\u672c\u7684\u512a\u52e2<\/h2>\n<p>\u70ba\u4e86\u89e3\u6c7aRedis\u4e2d\u7684\u4e26\u767c\u554f\u984c\uff0c\u4f7f\u7528Lua\u8173\u672c\u662f\u4e00\u500b\u7406\u60f3\u7684\u9078\u64c7\u3002Lua\u662f\u4e00\u7a2e\u8f15\u91cf\u7d1a\u7684\u8173\u672c\u8a9e\u8a00\uff0cRedis\u5167\u5efa\u4e86Lua\u89e3\u91cb\u5668\uff0c\u5141\u8a31\u7528\u6236\u5728\u670d\u52d9\u5668\u7aef\u57f7\u884c\u8173\u672c\u3002\u9019\u6a23\u505a\u7684\u597d\u8655\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u539f\u5b50\u6027\uff1a<\/strong>Lua\u8173\u672c\u5728Redis\u4e2d\u662f\u539f\u5b50\u57f7\u884c\u7684\uff0c\u9019\u610f\u5473\u8457\u5728\u8173\u672c\u57f7\u884c\u671f\u9593\uff0c\u5176\u4ed6\u8acb\u6c42\u7121\u6cd5\u5c0d\u6578\u64da\u9032\u884c\u4fee\u6539\uff0c\u5f9e\u800c\u907f\u514d\u4e86\u6578\u64da\u7af6\u722d\u7684\u554f\u984c\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u7db2\u7d61\u5ef6\u9072\uff1a<\/strong>\u5c07\u908f\u8f2f\u653e\u5728\u670d\u52d9\u5668\u7aef\u57f7\u884c\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5ba2\u6236\u7aef\u8207\u670d\u52d9\u5668\u4e4b\u9593\u7684\u901a\u4fe1\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528Lua\u8173\u672c\u53ef\u4ee5\u5c07\u591a\u500bRedis\u547d\u4ee4\u5c01\u88dd\u5728\u4e00\u8d77\uff0c\u7c21\u5316\u4e86\u4ee3\u78bc\u7d50\u69cb\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8lua%e8%85%b3%e6%9c%ac%e8%a7%a3%e6%b1%ba%e4%b8%a6%e7%99%bc%e5%95%8f%e9%a1%8c%e7%9a%84%e7%a4%ba%e4%be%8b-eMiVYHTgzG\">\u4f7f\u7528Lua\u8173\u672c\u89e3\u6c7a\u4e26\u767c\u554f\u984c\u7684\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Lua\u8173\u672c\u4f86\u89e3\u6c7a\u4e26\u767c\u554f\u984c\u7684\u7c21\u55ae\u793a\u4f8b\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u8a08\u6578\u5668\uff0c\u4e26\u5e0c\u671b\u5728\u591a\u500b\u5ba2\u6236\u7aef\u540c\u6642\u5c0d\u5176\u9032\u884c\u905e\u589e\u64cd\u4f5c\u3002<\/p>\n<pre><code>\n-- Lua\u8173\u672c\uff1a\u539f\u5b50\u905e\u589e\u8a08\u6578\u5668\nlocal current = redis.call('GET', KEYS[1])\nif not current then\n    current = 0\nelse\n    current = tonumber(current)\nend\ncurrent = current + 1\nredis.call('SET', KEYS[1], current)\nreturn current\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u8173\u672c\u4e2d\uff0c\u6211\u5011\u9996\u5148\u7372\u53d6\u8a08\u6578\u5668\u7684\u7576\u524d\u503c\uff0c\u7136\u5f8c\u5c07\u5176\u905e\u589e\uff0c\u6700\u5f8c\u5c07\u65b0\u7684\u503c\u5beb\u56deRedis\u3002\u9019\u6574\u500b\u904e\u7a0b\u662f\u539f\u5b50\u6027\u7684\uff0c\u78ba\u4fdd\u4e86\u5373\u4f7f\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\uff0c\u8a08\u6578\u5668\u7684\u503c\u4e5f\u4e0d\u6703\u51fa\u73fe\u932f\u8aa4\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-eMiVYHTgzG\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528Lua\u8173\u672c\u4f86\u89e3\u6c7aRedis\u4e2d\u7684\u4e26\u767c\u554f\u984c\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u53ef\u9760\u7684\u65b9\u6cd5\u3002\u901a\u904e\u5c07\u908f\u8f2f\u653e\u5728\u670d\u52d9\u5668\u7aef\u57f7\u884c\uff0c\u6211\u5011\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\uff0c\u4e26\u63d0\u9ad8\u7cfb\u7d71\u7684\u6027\u80fd\u3002\u96a8\u8457\u61c9\u7528\u7a0b\u5f0f\u9700\u6c42\u7684\u589e\u9577\uff0c\u638c\u63e1\u9019\u7a2e\u6280\u8853\u5c07\u5c0d\u958b\u767c\u8005\u7684\u5de5\u4f5c\u5927\u6709\u88e8\u76ca\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u8655\u7406\u4e26\u767c\u554f\u984c\uff0c\u8003\u616e\u4f7f\u7528Redis\u548cLua\u8173\u672c\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u670d\u52d9\uff0c\u8b93\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e2d\u904b\u884c\u5f97\u66f4\u52a0\u9806\u66a2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Lua\u8173\u672c\u5728Redis\u4e2d\u6709\u6548\u89e3\u6c7a\u4e26\u767c\u554f\u984c\uff0c\u63d0\u5347\u6578\u64da\u4e00\u81f4\u6027\u8207\u6027\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-162783","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162783","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=162783"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162783\/revisions"}],"predecessor-version":[{"id":162784,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162783\/revisions\/162784"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=162783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=162783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=162783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}