{"id":184708,"date":"2024-11-10T15:28:18","date_gmt":"2024-11-10T07:28:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184708\/"},"modified":"2024-11-10T15:28:19","modified_gmt":"2024-11-10T07:28:19","slug":"%e4%bd%bf%e7%94%a8redis%e6%bc%8f%e6%96%97%e9%99%90%e6%b5%81%e8%aa%bf%e6%95%b4%e5%af%a6%e6%99%82%e6%80%a7%e8%83%bd%ef%bc%88redis%e6%bc%8f%e6%96%97%e9%99%90%e6%b5%81%e4%bd%bf%e7%94%a8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184708\/","title":{"rendered":"\u4f7f\u7528Redis\u6f0f\u6597\u9650\u6d41\u8abf\u6574\u5be6\u6642\u6027\u80fd\uff08redis\u6f0f\u6597\u9650\u6d41\u4f7f\u7528\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e6%bc%8f%e6%96%97%e9%99%90%e6%b5%81%e8%aa%bf%e6%95%b4%e5%af%a6%e6%99%82%e6%80%a7%e8%83%bd-EOkWijkSqh\">\u4f7f\u7528Redis\u6f0f\u6597\u9650\u6d41\u8abf\u6574\u5be6\u6642\u6027\u80fd<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u8acb\u6c42\u6d41\u91cf\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u7d50\u69cb\u548c\u529f\u80fd\uff0c\u5176\u4e2d\u6f0f\u6597\u9650\u6d41\uff08Leaky Bucket\uff09\u662f\u4e00\u7a2e\u6709\u6548\u7684\u6d41\u91cf\u63a7\u5236\u6280\u8853\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Redis\u5be6\u73fe\u6f0f\u6597\u9650\u6d41\uff0c\u4ee5\u8abf\u6574\u5be6\u6642\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%bc%8f%e6%96%97%e9%99%90%e6%b5%81%ef%bc%9f-EOkWijkSqh\">\u4ec0\u9ebc\u662f\u6f0f\u6597\u9650\u6d41\uff1f<\/h2>\n<p>\u6f0f\u6597\u9650\u6d41\u662f\u4e00\u7a2e\u6d41\u91cf\u63a7\u5236\u7b97\u6cd5\uff0c\u65e8\u5728\u5e73\u6ed1\u8acb\u6c42\u7684\u9032\u5165\u901f\u7387\u3002\u5b83\u7684\u5de5\u4f5c\u539f\u7406\u985e\u4f3c\u65bc\u4e00\u500b\u6f0f\u6597\uff0c\u8acb\u6c42\u4ee5\u4e00\u5b9a\u7684\u901f\u7387\u9032\u5165\u6f0f\u6597\uff0c\u800c\u6f0f\u6597\u7684\u5e95\u90e8\u5247\u4ee5\u56fa\u5b9a\u7684\u901f\u7387\u6392\u51fa\u8acb\u6c42\u3002\u9019\u6a23\u53ef\u4ee5\u9632\u6b62\u7cfb\u7d71\u56e0\u70ba\u77ac\u6642\u6d41\u91cf\u904e\u5927\u800c\u5d29\u6f70\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-EOkWijkSqh\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u500b\u9ad8\u6027\u80fd\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5177\u6709\u4ee5\u4e0b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>Redis\u7684\u6578\u64da\u64cd\u4f5c\u901f\u5ea6\u6975\u5feb\uff0c\u80fd\u5920\u652f\u6301\u6bcf\u79d2\u6578\u5341\u842c\u6b21\u7684\u8acb\u6c42\u3002<\/li>\n<li><strong>\u6301\u4e45\u6027\uff1a<\/strong>\u652f\u6301\u6578\u64da\u6301\u4e45\u5316\uff0c\u80fd\u5920\u5728\u91cd\u555f\u5f8c\u6062\u5fa9\u6578\u64da\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>\u63d0\u4f9b\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u65b9\u4fbf\u958b\u767c\u8005\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%af%a6%e7%8f%beredis%e6%bc%8f%e6%96%97%e9%99%90%e6%b5%81-EOkWijkSqh\">\u5982\u4f55\u5be6\u73feRedis\u6f0f\u6597\u9650\u6d41<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u5be6\u73fe\u6f0f\u6597\u9650\u6d41\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e5%ae%9a%e5%8f%83%e6%95%b8-EOkWijkSqh\">1. \u8a2d\u5b9a\u53c3\u6578<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u8a2d\u5b9a\u6f0f\u6597\u7684\u5bb9\u91cf\u548c\u6392\u51fa\u901f\u7387\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u7684\u6f0f\u6597\u5bb9\u91cf\u70ba10\u500b\u8acb\u6c42\uff0c\u6bcf\u79d2\u6392\u51fa1\u500b\u8acb\u6c42\u3002<\/p>\n<pre><code>const BUCKET_CAPACITY = 10; \/\/ \u6f0f\u6597\u5bb9\u91cf\nconst DRAIN_RATE = 1; \/\/ \u6bcf\u79d2\u6392\u51fa\u8acb\u6c42\u6578\n<\/code><\/pre>\n<h3 id=\"2-%e5%88%9d%e5%a7%8b%e5%8c%96redis-EOkWijkSqh\">2. \u521d\u59cb\u5316Redis<\/h3>\n<p>\u4f7f\u7528Redis\u7684\u9375\u4f86\u5b58\u5132\u7576\u524d\u7684\u8acb\u6c42\u6578\u91cf\u548c\u4e0a\u6b21\u8acb\u6c42\u7684\u6642\u9593\u6233\u3002<\/p>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\nclient.set('request_count', 0);\nclient.set('last_request_time', Date.now());\n<\/code><\/pre>\n<h3 id=\"3-%e8%ab%8b%e6%b1%82%e8%99%95%e7%90%86%e9%82%8f%e8%bc%af-EOkWijkSqh\">3. \u8acb\u6c42\u8655\u7406\u908f\u8f2f<\/h3>\n<p>\u6bcf\u7576\u6709\u8acb\u6c42\u9032\u4f86\u6642\uff0c\u9996\u5148\u6aa2\u67e5\u7576\u524d\u6642\u9593\u8207\u4e0a\u6b21\u8acb\u6c42\u6642\u9593\u7684\u5dee\u7570\uff0c\u6839\u64da\u6392\u51fa\u901f\u7387\u66f4\u65b0\u8acb\u6c42\u6578\u91cf\u3002<\/p>\n<pre><code>function handleRequest() {\n    client.get('last_request_time', (err, lastRequestTime) =&gt; {\n        const currentTime = Date.now();\n        const timeElapsed = (currentTime - lastRequestTime) \/ 1000; \/\/ \u8f49\u63db\u70ba\u79d2\n\n        \/\/ \u66f4\u65b0\u8acb\u6c42\u6578\u91cf\n        const newRequestCount = Math.max(0, BUCKET_CAPACITY - Math.floor(timeElapsed * DRAIN_RATE));\n        client.set('request_count', newRequestCount);\n        \n        \/\/ \u6aa2\u67e5\u662f\u5426\u53ef\u4ee5\u8655\u7406\u8acb\u6c42\n        client.get('request_count', (err, requestCount) =&gt; {\n            if (requestCount &lt; BUCKET_CAPACITY) {\n                \/\/ \u53ef\u4ee5\u8655\u7406\u8acb\u6c42\n                client.incr(&#039;request_count&#039;);\n                client.set(&#039;last_request_time&#039;, currentTime);\n                console.log(&#039;\u8acb\u6c42\u5df2\u8655\u7406&#039;);\n            } else {\n                console.log(&#039;\u8acb\u6c42\u88ab\u62d2\u7d55\uff0c\u8acb\u7a0d\u5f8c\u518d\u8a66&#039;);\n            }\n        });\n    });\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-EOkWijkSqh\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u5be6\u73fe\u6f0f\u6597\u9650\u6d41\u662f\u4e00\u7a2e\u6709\u6548\u7684\u6d41\u91cf\u63a7\u5236\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u9ad8\u6d41\u91cf\u60c5\u6cc1\u4e0b\u4fdd\u6301\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u3002\u901a\u904e\u5408\u7406\u8a2d\u7f6e\u6f0f\u6597\u7684\u5bb9\u91cf\u548c\u6392\u51fa\u901f\u7387\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u5be6\u969b\u9700\u6c42\u8abf\u6574\u7cfb\u7d71\u6027\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u9ad8\u6027\u80fd\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u7121\u8ad6\u662f\u9078\u64c7 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u5730\u5340\u7684\u670d\u52d9\uff0c\u78ba\u4fdd\u7cfb\u7d71\u80fd\u5920\u61c9\u5c0d\u4e0d\u65b7\u589e\u9577\u7684\u6d41\u91cf\u9700\u6c42\u662f\u6bcf\u4f4d\u958b\u767c\u8005\u7684\u8cac\u4efb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u6f0f\u6597\u9650\u6d41\u6280\u8853\uff0c\u5be6\u6642\u8abf\u6574\u7cfb\u7d71\u6027\u80fd\uff0c\u63d0\u5347\u61c9\u7528\u7a69\u5b9a\u6027\u8207\u97ff\u61c9\u901f\u5ea6\uff0c\u78ba\u4fdd\u8cc7\u6e90\u6709\u6548\u5206\u914d\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-184708","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184708","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=184708"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184708\/revisions"}],"predecessor-version":[{"id":184709,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184708\/revisions\/184709"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}