{"id":132469,"date":"2024-10-30T21:53:17","date_gmt":"2024-10-30T13:53:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/132469\/"},"modified":"2024-10-30T21:53:18","modified_gmt":"2024-10-30T13:53:18","slug":"redis%e9%99%90%e6%b5%81%e5%a4%b1%e5%8e%bb%e4%bd%9c%e7%94%a8%ef%bc%88redis%e9%99%90%e6%b5%81%e7%84%a1%e6%95%88%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/132469\/","title":{"rendered":"Redis\u9650\u6d41\u5931\u53bb\u4f5c\u7528\uff08redis\u9650\u6d41\u7121\u6548\uff09"},"content":{"rendered":"<h1 id=\"redis%e9%99%90%e6%b5%81%e5%a4%b1%e5%8e%bb%e4%bd%9c%e7%94%a8%ef%bc%88redis%e9%99%90%e6%b5%81%e7%84%a1%e6%95%88%ef%bc%89-MFUXmujDIv\">Redis\u9650\u6d41\u5931\u53bb\u4f5c\u7528\uff08Redis\u9650\u6d41\u7121\u6548\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u9650\u6d41\u6280\u8853\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u4fdd\u8b77\u61c9\u7528\u7a0b\u5e8f\u548c\u670d\u52d9\u5668\uff0c\u9632\u6b62\u904e\u8f09\u548c\u6feb\u7528\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u5e38\u88ab\u7528\u4f86\u5be6\u73fe\u9650\u6d41\u529f\u80fd\u3002\u7136\u800c\uff0c\u7576Redis\u9650\u6d41\u5931\u53bb\u4f5c\u7528\u6642\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u7cfb\u7d71\u6027\u80fd\u4e0b\u964d\uff0c\u751a\u81f3\u5d29\u6f70\u3002\u672c\u6587\u5c07\u63a2\u8a0eRedis\u9650\u6d41\u5931\u6548\u7684\u539f\u56e0\u53ca\u5176\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"redis%e9%99%90%e6%b5%81%e7%9a%84%e5%9f%ba%e6%9c%ac%e5%8e%9f%e7%90%86-MFUXmujDIv\">Redis\u9650\u6d41\u7684\u57fa\u672c\u539f\u7406<\/h2>\n<p>Redis\u9650\u6d41\u901a\u5e38\u57fa\u65bc\u4ee4\u724c\u6876\u6216\u6f0f\u6876\u7b97\u6cd5\u3002\u9019\u4e9b\u7b97\u6cd5\u901a\u904e\u63a7\u5236\u8acb\u6c42\u7684\u901f\u7387\u4f86\u9650\u5236\u7528\u6236\u7684\u884c\u70ba\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee4\u724c\u6876\u7b97\u6cd5\u6642\uff0c\u7cfb\u7d71\u6703\u5728\u4e00\u5b9a\u6642\u9593\u5167\u751f\u6210\u56fa\u5b9a\u6578\u91cf\u7684\u4ee4\u724c\uff0c\u53ea\u6709\u6301\u6709\u4ee4\u724c\u7684\u8acb\u6c42\u624d\u80fd\u88ab\u8655\u7406\u3002\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u8acb\u6c42\u7684\u983b\u7387\u3002<\/p>\n<h3 id=\"%e4%bb%a4%e7%89%8c%e6%a1%b6%e7%ae%97%e6%b3%95%e7%a4%ba%e4%be%8b-MFUXmujDIv\">\u4ee4\u724c\u6876\u7b97\u6cd5\u793a\u4f8b<\/h3>\n<pre><code>\n# \u5047\u8a2d\u6bcf\u79d2\u751f\u62105\u500b\u4ee4\u724c\nrate_limit = 5\ncurrent_tokens = 0\nlast_time = current_time()\n\ndef request():\n    global current_tokens, last_time\n    now = current_time()\n    elapsed = now - last_time\n    current_tokens = min(rate_limit, current_tokens + elapsed * rate_limit)\n    last_time = now\n\n    if current_tokens &gt; 0:\n        current_tokens -= 1\n        return \"Request processed\"\n    else:\n        return \"Rate limit exceeded\"\n<\/code><\/pre>\n<h2 id=\"redis%e9%99%90%e6%b5%81%e5%a4%b1%e6%95%88%e7%9a%84%e5%8e%9f%e5%9b%a0-MFUXmujDIv\">Redis\u9650\u6d41\u5931\u6548\u7684\u539f\u56e0<\/h2>\n<p>\u5118\u7ba1Redis\u5728\u9650\u6d41\u65b9\u9762\u8868\u73fe\u51fa\u8272\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u9650\u6d41\u6a5f\u5236\u53ef\u80fd\u6703\u5931\u6548\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u539f\u56e0\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u4f75\u767c\u8acb\u6c42\uff1a<\/strong>\u7576\u8acb\u6c42\u91cf\u8d85\u904eRedis\u7684\u8655\u7406\u80fd\u529b\u6642\uff0c\u9650\u6d41\u6a5f\u5236\u53ef\u80fd\u7121\u6cd5\u53ca\u6642\u97ff\u61c9\uff0c\u5c0e\u81f4\u8acb\u6c42\u88ab\u904e\u5ea6\u8655\u7406\u3002<\/li>\n<li><strong>\u6578\u64da\u4e00\u81f4\u6027\u554f\u984c\uff1a<\/strong>\u5728\u5206\u4f48\u5f0f\u7cfb\u7d71\u4e2d\uff0c\u82e5\u591a\u500b\u5be6\u4f8b\u540c\u6642\u8a2a\u554fRedis\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\uff0c\u5f9e\u800c\u5f71\u97ff\u9650\u6d41\u6548\u679c\u3002<\/li>\n<li><strong>\u904e\u671f\u7b56\u7565\uff1a<\/strong>\u5982\u679c\u9650\u6d41\u7684\u9375\u503c\u8a2d\u7f6e\u4e86\u904e\u671f\u6642\u9593\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u5728\u9ad8\u983b\u8acb\u6c42\u4e0b\uff0c\u9375\u503c\u904e\u671f\u800c\u7121\u6cd5\u6b63\u78ba\u9650\u6d41\u3002<\/li>\n<li><strong>\u7db2\u7d61\u5ef6\u9072\uff1a<\/strong>\u7db2\u7d61\u5ef6\u9072\u53ef\u80fd\u5c0e\u81f4\u8acb\u6c42\u672a\u80fd\u53ca\u6642\u5230\u9054Redis\uff0c\u5f9e\u800c\u5f71\u97ff\u9650\u6d41\u7684\u5be6\u6642\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e8%a7%a3%e6%b1%baredis%e9%99%90%e6%b5%81%e5%a4%b1%e6%95%88%e7%9a%84%e6%96%b9%e6%b3%95-MFUXmujDIv\">\u89e3\u6c7aRedis\u9650\u6d41\u5931\u6548\u7684\u65b9\u6cd5<\/h2>\n<p>\u70ba\u4e86\u78ba\u4fddRedis\u9650\u6d41\u6a5f\u5236\u7684\u6709\u6548\u6027\uff0c\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u7a2e\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528Lua\u8173\u672c\uff1a<\/strong>\u901a\u904e\u5728Redis\u4e2d\u57f7\u884cLua\u8173\u672c\uff0c\u53ef\u4ee5\u78ba\u4fdd\u64cd\u4f5c\u7684\u539f\u5b50\u6027\uff0c\u5f9e\u800c\u907f\u514d\u6578\u64da\u4e0d\u4e00\u81f4\u7684\u554f\u984c\u3002<\/li>\n<li><strong>\u589e\u52a0Redis\u5be6\u4f8b\uff1a<\/strong>\u901a\u904e\u589e\u52a0Redis\u7684\u5be6\u4f8b\u6578\u91cf\uff0c\u53ef\u4ee5\u5206\u64d4\u8acb\u6c42\u8ca0\u8f09\uff0c\u63d0\u9ad8\u7cfb\u7d71\u7684\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<li><strong>\u8abf\u6574\u904e\u671f\u6642\u9593\uff1a<\/strong>\u6839\u64da\u5be6\u969b\u9700\u6c42\u8abf\u6574\u9650\u6d41\u9375\u7684\u904e\u671f\u6642\u9593\uff0c\u4ee5\u78ba\u4fdd\u5728\u9ad8\u983b\u8acb\u6c42\u4e0b\u4e0d\u6703\u904e\u671f\u3002<\/li>\n<li><strong>\u76e3\u63a7\u548c\u8abf\u6574\uff1a<\/strong>\u5b9a\u671f\u76e3\u63a7Redis\u7684\u6027\u80fd\u6307\u6a19\uff0c\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u8abf\u6574\u9650\u6d41\u7b56\u7565\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-MFUXmujDIv\">\u7d50\u8ad6<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u5728\u9650\u6d41\u65b9\u9762\u5177\u6709\u5f88\u5927\u7684\u6f5b\u529b\u3002\u7136\u800c\uff0c\u7576\u9650\u6d41\u6a5f\u5236\u5931\u6548\u6642\uff0c\u53ef\u80fd\u6703\u5c0d\u7cfb\u7d71\u9020\u6210\u56b4\u91cd\u5f71\u97ff\u3002\u901a\u904e\u4e86\u89e3\u9650\u6d41\u7684\u57fa\u672c\u539f\u7406\u3001\u5931\u6548\u539f\u56e0\u53ca\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7ba1\u7406\u548c\u512a\u5316\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u9650\u6d41\u5931\u53bb\u4f5c\u7528\u53ef\u80fd\u7531\u914d\u7f6e\u9519\u8bef\u3001\u7f51\u7edc\u5ef6\u8fdf\u6216\u5ba2\u6237\u7aef\u95ee\u9898\u5f15\u8d77\uff0c\u5bfc\u81f4\u6d41\u91cf\u63a7\u5236\u5931\u6548\uff0c\u5f71\u54cd\u7cfb\u7edf\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-132469","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/132469","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=132469"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/132469\/revisions"}],"predecessor-version":[{"id":132470,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/132469\/revisions\/132470"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=132469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=132469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=132469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}