{"id":194245,"date":"2024-11-13T05:03:19","date_gmt":"2024-11-12T21:03:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/194245\/"},"modified":"2024-11-13T05:03:20","modified_gmt":"2024-11-12T21:03:20","slug":"%e4%bb%a5redis%e7%ae%a1%e7%90%86%e8%ab%8b%e6%b1%82%ef%bc%8c%e5%af%a6%e7%8f%be%e8%ab%8b%e6%b1%82%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%ef%bc%88redis%e8%ab%8b%e6%b1%82%e9%99%90%e5%88%b6%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/194245\/","title":{"rendered":"\u4ee5Redis\u7ba1\u7406\u8acb\u6c42\uff0c\u5be6\u73fe\u8acb\u6c42\u6d41\u91cf\u9650\u5236\uff08redis\u8acb\u6c42\u9650\u5236\uff09"},"content":{"rendered":"<h1 id=\"%e4%bb%a5redis%e7%ae%a1%e7%90%86%e8%ab%8b%e6%b1%82%ef%bc%8c%e5%af%a6%e7%8f%be%e8%ab%8b%e6%b1%82%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%ef%bc%88redis%e8%ab%8b%e6%b1%82%e9%99%90%e5%88%b6%ef%bc%89-ByUhHfXHBU\">\u4ee5Redis\u7ba1\u7406\u8acb\u6c42\uff0c\u5be6\u73fe\u8acb\u6c42\u6d41\u91cf\u9650\u5236\uff08redis\u8acb\u6c42\u9650\u5236\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u6d41\u91cf\u9650\u5236\u662f\u4e00\u500b\u81f3\u95dc\u91cd\u8981\u7684\u6280\u8853\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u6d41\u91cf\u7684\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002\u6d41\u91cf\u9650\u5236\u53ef\u4ee5\u9632\u6b62\u670d\u52d9\u5668\u904e\u8f09\uff0c\u4fdd\u8b77\u61c9\u7528\u7a0b\u5e8f\u7684\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u7d50\u69cb\u548c\u64cd\u4f5c\uff0c\u6210\u70ba\u5be6\u73fe\u8acb\u6c42\u6d41\u91cf\u9650\u5236\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e8%ab%8b%e6%b1%82%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%ef%bc%9f-ByUhHfXHBU\">\u4ec0\u9ebc\u662f\u8acb\u6c42\u6d41\u91cf\u9650\u5236\uff1f<\/h2>\n<p>\u8acb\u6c42\u6d41\u91cf\u9650\u5236\u662f\u6307\u5c0d\u7528\u6236\u5728\u7279\u5b9a\u6642\u9593\u5167\u767c\u9001\u7684\u8acb\u6c42\u6578\u91cf\u9032\u884c\u63a7\u5236\u3002\u9019\u7a2e\u6280\u8853\u901a\u5e38\u7528\u65bc\u9632\u6b62\u6feb\u7528\u3001\u4fdd\u8b77API\u548c\u78ba\u4fdd\u516c\u5e73\u4f7f\u7528\u8cc7\u6e90\u3002\u6d41\u91cf\u9650\u5236\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u7b56\u7565\u9032\u884c\u8a2d\u7f6e\uff0c\u4f8b\u5982\u6bcf\u5206\u9418\u3001\u6bcf\u5c0f\u6642\u6216\u6bcf\u5929\u7684\u8acb\u6c42\u6b21\u6578\u9650\u5236\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-ByUhHfXHBU\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u9032\u884c\u8acb\u6c42\u6d41\u91cf\u9650\u5236\u7684\u4e00\u4e9b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5176\u80fd\u5920\u5feb\u901f\u8b80\u53d6\u548c\u5beb\u5165\u6578\u64da\uff0c\u9069\u5408\u9ad8\u983b\u8acb\u6c42\u7684\u5834\u666f\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u7b49\uff0c\u65b9\u4fbf\u958b\u767c\u8005\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\u3002<\/li>\n<li><strong>\u652f\u6301\u904e\u671f\u6642\u9593\uff1a<\/strong>Redis\u5141\u8a31\u8a2d\u7f6e\u9375\u7684\u904e\u671f\u6642\u9593\uff0c\u9019\u5c0d\u65bc\u6d41\u91cf\u9650\u5236\u975e\u5e38\u6709\u7528\uff0c\u56e0\u70ba\u53ef\u4ee5\u81ea\u52d5\u6e05\u9664\u904e\u671f\u7684\u8acb\u6c42\u8a08\u6578\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e5%af%a6%e7%8f%be%e8%ab%8b%e6%b1%82%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6-ByUhHfXHBU\">\u5982\u4f55\u4f7f\u7528Redis\u5be6\u73fe\u8acb\u6c42\u6d41\u91cf\u9650\u5236<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Redis\u5be6\u73fe\u7c21\u55ae\u8acb\u6c42\u6d41\u91cf\u9650\u5236\u7684\u793a\u4f8b\u3002\u5047\u8a2d\u6211\u5011\u5e0c\u671b\u9650\u5236\u6bcf\u500b\u7528\u6236\u6bcf\u5206\u9418\u6700\u591a\u53ea\u80fd\u767c\u90015\u500b\u8acb\u6c42\u3002<\/p>\n<h3 id=\"%e6%ad%a5%e9%a9%9f1%ef%bc%9a%e8%a8%ad%e7%bd%aeredis%e9%80%a3%e6%8e%a5-ByUhHfXHBU\">\u6b65\u9a5f1\uff1a\u8a2d\u7f6eRedis\u9023\u63a5<\/h3>\n<pre><code>\nconst redis = require('redis');\nconst client = redis.createClient();\n<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f2%ef%bc%9a%e5%af%a6%e7%8f%be%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6%e9%82%8f%e8%bc%af-ByUhHfXHBU\">\u6b65\u9a5f2\uff1a\u5be6\u73fe\u6d41\u91cf\u9650\u5236\u908f\u8f2f<\/h3>\n<pre><code>\nfunction rateLimit(userId) {\n    const key = `rate_limit:${userId}`;\n    const currentTime = Math.floor(Date.now() \/ 1000);\n    const windowSize = 60; \/\/ 60\u79d2\n    const limit = 5; \/\/ \u9650\u5236\u8acb\u6c42\u6578\n\n    client.multi()\n        .zremrangebyscore(key, 0, currentTime - windowSize) \/\/ \u522a\u9664\u904e\u671f\u8acb\u6c42\n        .zcard(key) \/\/ \u7372\u53d6\u7576\u524d\u8acb\u6c42\u6578\n        .exec((err, replies) =&gt; {\n            if (err) {\n                console.error(err);\n                return;\n            }\n\n            const requestCount = replies[1];\n            if (requestCount &lt; limit) {\n                \/\/ \u5141\u8a31\u8acb\u6c42\n                client.zadd(key, currentTime, currentTime);\n                client.expire(key, windowSize); \/\/ \u8a2d\u7f6e\u904e\u671f\u6642\u9593\n                console.log(&#039;\u8acb\u6c42\u88ab\u5141\u8a31&#039;);\n            } else {\n                \/\/ \u62d2\u7d55\u8acb\u6c42\n                console.log(&#039;\u8acb\u6c42\u88ab\u62d2\u7d55\uff0c\u8d85\u904e\u9650\u5236&#039;);\n            }\n        });\n}\n<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f3%ef%bc%9a%e6%b8%ac%e8%a9%a6%e6%b5%81%e9%87%8f%e9%99%90%e5%88%b6-ByUhHfXHBU\">\u6b65\u9a5f3\uff1a\u6e2c\u8a66\u6d41\u91cf\u9650\u5236<\/h3>\n<p>\u53ef\u4ee5\u901a\u904e\u8abf\u7528rateLimit\u51fd\u6578\u4f86\u6e2c\u8a66\u6d41\u91cf\u9650\u5236\u529f\u80fd\uff0c\u4e26\u89c0\u5bdf\u63a7\u5236\u53f0\u8f38\u51fa\u7684\u7d50\u679c\u3002\u9019\u6a23\u5c31\u80fd\u5920\u6709\u6548\u5730\u63a7\u5236\u6bcf\u500b\u7528\u6236\u7684\u8acb\u6c42\u983b\u7387\uff0c\u9632\u6b62\u6feb\u7528\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-ByUhHfXHBU\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528Redis\u9032\u884c\u8acb\u6c42\u6d41\u91cf\u9650\u5236\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u7c21\u55ae\u7684\u8a2d\u7f6e\u548c\u4ee3\u78bc\u5be6\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u4fdd\u8b77\u61c9\u7528\u7a0b\u5e8f\uff0c\u78ba\u4fdd\u5176\u7a69\u5b9a\u6027\u548c\u53ef\u7528\u6027\u3002\u96a8\u8457\u7db2\u7d61\u6d41\u91cf\u7684\u589e\u9577\uff0c\u6d41\u91cf\u9650\u5236\u5c07\u6210\u70ba\u6bcf\u500b\u958b\u767c\u8005\u5fc5\u9808\u8003\u616e\u7684\u91cd\u8981\u6280\u8853\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ByUhHfXHBU\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u6d41\u91cf\u9650\u5236\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u6280\u8853\u3002\u4f7f\u7528Redis\u4f86\u7ba1\u7406\u8acb\u6c42\u6d41\u91cf\u9650\u5236\u4e0d\u50c5\u80fd\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u9632\u6b62\u6feb\u7528\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u5728\u60a8\u7684\u61c9\u7528\u4e2d\u5be6\u73fe\u9019\u4e00\u6280\u8853\uff0c\u6216\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u7ba1\u7406\u8acb\u6c42\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u8acb\u6c42\u6d41\u91cf\u9650\u5236\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7a69\u5b9a\u6027\u8207\u6027\u80fd\uff0c\u9632\u6b62\u904e\u8f09\u554f\u984c\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-194245","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194245","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=194245"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194245\/revisions"}],"predecessor-version":[{"id":194246,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/194245\/revisions\/194246"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=194245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=194245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=194245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}