{"id":189286,"date":"2024-11-12T06:22:21","date_gmt":"2024-11-11T22:22:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/189286\/"},"modified":"2024-11-12T06:22:23","modified_gmt":"2024-11-11T22:22:23","slug":"%e9%80%9a%e9%81%8eredis%e5%af%a6%e7%8f%be%e9%99%90%e5%88%b6%e8%ab%8b%e6%b1%82%e6%ac%a1%e6%95%b8%ef%bc%88redis%e9%99%90%e5%88%b6%e8%ab%8b%e6%b1%82%e6%ac%a1%e6%95%b8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/189286\/","title":{"rendered":"\u901a\u904eRedis\u5be6\u73fe\u9650\u5236\u8acb\u6c42\u6b21\u6578\uff08redis\u9650\u5236\u8acb\u6c42\u6b21\u6578\uff09"},"content":{"rendered":"<h1 id=\"%e9%80%9a%e9%81%8eredis%e5%af%a6%e7%8f%be%e9%99%90%e5%88%b6%e8%ab%8b%e6%b1%82%e6%ac%a1%e6%95%b8-KoOfzbGLcf\">\u901a\u904eRedis\u5be6\u73fe\u9650\u5236\u8acb\u6c42\u6b21\u6578<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u9650\u5236\u8acb\u6c42\u6b21\u6578\u662f\u4e00\u500b\u91cd\u8981\u7684\u5b89\u5168\u63aa\u65bd\uff0c\u5c24\u5176\u662f\u5728\u9632\u6b62\u60e1\u610f\u653b\u64ca\u548c\u4fdd\u8b77\u4f3a\u670d\u5668\u8cc7\u6e90\u65b9\u9762\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u7c21\u55ae\u800c\u6709\u6548\u7684\u65b9\u5f0f\u4f86\u5be6\u73fe\u8acb\u6c42\u6b21\u6578\u7684\u9650\u5236\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e9%99%90%e5%88%b6%e8%ab%8b%e6%b1%82%e6%ac%a1%e6%95%b8%ef%bc%9f-KoOfzbGLcf\">\u70ba\u4ec0\u9ebc\u9700\u8981\u9650\u5236\u8acb\u6c42\u6b21\u6578\uff1f<\/h2>\n<p>\u9650\u5236\u8acb\u6c42\u6b21\u6578\u7684\u4e3b\u8981\u76ee\u7684\u662f\u9632\u6b62\u6feb\u7528\u548c\u4fdd\u8b77\u7cfb\u7d71\u8cc7\u6e90\u3002\u5e38\u898b\u7684\u61c9\u7528\u5834\u666f\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9632\u6b62\u66b4\u529b\u7834\u89e3\uff1a\u9650\u5236\u7528\u6236\u5728\u77ed\u6642\u9593\u5167\u7684\u767b\u9304\u5617\u8a66\u6b21\u6578\u3002<\/li>\n<li>\u4fdd\u8b77API\uff1a\u9632\u6b62\u55ae\u500b\u7528\u6236\u904e\u5ea6\u8abf\u7528API\uff0c\u5f71\u97ff\u5176\u4ed6\u7528\u6236\u7684\u4f7f\u7528\u9ad4\u9a57\u3002<\/li>\n<li>\u6e1b\u5c11DDoS\u653b\u64ca\u7684\u5f71\u97ff\uff1a\u901a\u904e\u9650\u5236\u8acb\u6c42\u983b\u7387\u4f86\u6e1b\u5c11\u653b\u64ca\u6d41\u91cf\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e7%9a%84%e5%84%aa%e5%8b%a2-KoOfzbGLcf\">Redis\u7684\u512a\u52e2<\/h2>\n<p>Redis\u662f\u4e00\u500b\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5177\u6709\u4ee5\u4e0b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li>\u5feb\u901f\uff1aRedis\u7684\u8b80\u5beb\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9069\u5408\u9ad8\u983b\u8acb\u6c42\u7684\u5834\u666f\u3002<\/li>\n<li>\u7c21\u55ae\uff1a\u4f7f\u7528Redis\u7684\u547d\u4ee4\u975e\u5e38\u7c21\u55ae\uff0c\u6613\u65bc\u5be6\u73fe\u8acb\u6c42\u9650\u5236\u908f\u8f2f\u3002<\/li>\n<li>\u6301\u4e45\u5316\uff1aRedis\u652f\u6301\u6578\u64da\u6301\u4e45\u5316\uff0c\u53ef\u4ee5\u5728\u91cd\u555f\u5f8c\u6062\u5fa9\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e7%8f%be%e8%ab%8b%e6%b1%82%e6%ac%a1%e6%95%b8%e9%99%90%e5%88%b6%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%80%9d%e8%b7%af-KoOfzbGLcf\">\u5be6\u73fe\u8acb\u6c42\u6b21\u6578\u9650\u5236\u7684\u57fa\u672c\u601d\u8def<\/h2>\n<p>\u901a\u904eRedis\u5be6\u73fe\u8acb\u6c42\u6b21\u6578\u9650\u5236\u7684\u57fa\u672c\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u6bcf\u7576\u7528\u6236\u767c\u9001\u8acb\u6c42\u6642\uff0c\u6aa2\u67e5\u8a72\u7528\u6236\u7684\u8acb\u6c42\u6b21\u6578\u3002<\/li>\n<li>\u5982\u679c\u8acb\u6c42\u6b21\u6578\u8d85\u904e\u9650\u5236\uff0c\u5247\u62d2\u7d55\u8a72\u8acb\u6c42\u3002<\/li>\n<li>\u5982\u679c\u8acb\u6c42\u6b21\u6578\u672a\u8d85\u904e\u9650\u5236\uff0c\u5247\u589e\u52a0\u8acb\u6c42\u6b21\u6578\u4e26\u5141\u8a31\u8acb\u6c42\u3002<\/li>\n<li>\u8a2d\u7f6e\u4e00\u500b\u904e\u671f\u6642\u9593\uff0c\u7576\u904e\u671f\u6642\u9593\u5230\u9054\u6642\uff0c\u81ea\u52d5\u91cd\u7f6e\u8acb\u6c42\u6b21\u6578\u3002<\/li>\n<\/ol>\n<h2 id=\"%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-KoOfzbGLcf\">\u4ee3\u78bc\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Node.js\u548cRedis\u5be6\u73fe\u8acb\u6c42\u6b21\u6578\u9650\u5236\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>const express = require('express');\nconst redis = require('redis');\nconst app = express();\nconst client = redis.createClient();\n\nconst RATE_LIMIT_WINDOW = 60; \/\/ \u7a97\u53e3\u6642\u9593\uff08\u79d2\uff09\nconst MAX_REQUESTS = 5; \/\/ \u6700\u5927\u8acb\u6c42\u6b21\u6578\n\napp.use((req, res, next) =&gt; {\n    const ip = req.ip; \/\/ \u7372\u53d6\u7528\u6236IP\n    const key = `rate_limit:${ip}`;\n\n    client.multi()\n        .incr(key) \/\/ \u589e\u52a0\u8acb\u6c42\u6b21\u6578\n        .expire(key, RATE_LIMIT_WINDOW) \/\/ \u8a2d\u7f6e\u904e\u671f\u6642\u9593\n        .exec((err, replies) =&gt; {\n            if (err) {\n                return res.status(500).send('Server error');\n            }\n\n            const requestCount = replies[0];\n            if (requestCount &gt; MAX_REQUESTS) {\n                return res.status(429).send('Too many requests');\n            }\n\n            next(); \/\/ \u5141\u8a31\u8acb\u6c42\n        });\n});\n\napp.get('\/', (req, res) =&gt; {\n    res.send('Hello, world!');\n});\n\napp.listen(3000, () =&gt; {\n    console.log('Server is running on port 3000');\n});<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-KoOfzbGLcf\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528Redis\u4f86\u9650\u5236\u8acb\u6c42\u6b21\u6578\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u8b77\u7cfb\u7d71\u8cc7\u6e90\uff0c\u9632\u6b62\u6feb\u7528\u884c\u70ba\u3002Redis\u7684\u9ad8\u6027\u80fd\u548c\u7c21\u55ae\u7684\u64cd\u4f5c\u4f7f\u5176\u6210\u70ba\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u7684\u7406\u60f3\u9078\u64c7\u3002\u7121\u8ad6\u662f\u9632\u6b62\u66b4\u529b\u7834\u89e3\u9084\u662f\u4fdd\u8b77API\uff0cRedis\u90fd\u80fd\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u7ba1\u7406\u60a8\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u6216 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\uff0cRedis\u7121\u7591\u662f\u4e00\u500b\u503c\u5f97\u8003\u616e\u7684\u9078\u64c7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u5be6\u73fe\u8acb\u6c42\u6b21\u6578\u9650\u5236\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u5b89\u5168\u6027\uff0c\u6709\u6548\u9632\u6b62\u6feb\u7528\u8207\u653b\u64ca\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7a69\u5b9a\u904b\u884c\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-189286","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189286","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=189286"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189286\/revisions"}],"predecessor-version":[{"id":189287,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189286\/revisions\/189287"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=189286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=189286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=189286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}