{"id":167635,"date":"2024-11-06T03:12:21","date_gmt":"2024-11-05T19:12:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167635\/"},"modified":"2024-11-06T03:12:22","modified_gmt":"2024-11-05T19:12:22","slug":"%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%be%e9%9a%8a%e5%88%97%e9%99%90%e6%b5%81%ef%bc%88redis%e9%9a%8a%e5%88%97%e9%99%90%e6%b5%81%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167635\/","title":{"rendered":"\u5229\u7528Redis\u5be6\u73fe\u968a\u5217\u9650\u6d41\uff08redis\u968a\u5217\u9650\u6d41\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%be%e9%9a%8a%e5%88%97%e9%99%90%e6%b5%81%ef%bc%88redis%e9%9a%8a%e5%88%97%e9%99%90%e6%b5%81%ef%bc%89-VNnYLnzUKp\">\u5229\u7528Redis\u5be6\u73fe\u968a\u5217\u9650\u6d41\uff08redis\u968a\u5217\u9650\u6d41\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6d41\u91cf\u7ba1\u7406\u662f\u4e00\u500b\u81f3\u95dc\u91cd\u8981\u7684\u8ab2\u984c\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u63a7\u5236\u8acb\u6c42\u7684\u6578\u91cf\uff0c\u907f\u514d\u7cfb\u7d71\u904e\u8f09\uff0c\u6210\u70ba\u4e86\u958b\u767c\u8005\u9700\u8981\u9762\u5c0d\u7684\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\u64cd\u4f5c\uff0c\u7279\u5225\u9069\u5408\u7528\u65bc\u5be6\u73fe\u968a\u5217\u9650\u6d41\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u4f86\u5be6\u73fe\u968a\u5217\u9650\u6d41\u7684\u6280\u8853\u7d30\u7bc0\u548c\u5be6\u969b\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%9a%8a%e5%88%97%e9%99%90%e6%b5%81%ef%bc%9f-VNnYLnzUKp\">\u4ec0\u9ebc\u662f\u968a\u5217\u9650\u6d41\uff1f<\/h2>\n<p>\u968a\u5217\u9650\u6d41\u662f\u4e00\u7a2e\u63a7\u5236\u8acb\u6c42\u901f\u7387\u7684\u6280\u8853\uff0c\u65e8\u5728\u9632\u6b62\u7cfb\u7d71\u56e0\u904e\u591a\u8acb\u6c42\u800c\u5d29\u6f70\u3002\u901a\u904e\u8a2d\u7f6e\u8acb\u6c42\u7684\u6700\u5927\u6578\u91cf\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u78ba\u4fdd\u7cfb\u7d71\u5728\u9ad8\u6d41\u91cf\u60c5\u6cc1\u4e0b\u4ecd\u7136\u80fd\u5920\u7a69\u5b9a\u904b\u884c\u3002\u9650\u6d41\u7684\u7b56\u7565\u6709\u591a\u7a2e\uff0c\u5305\u62ec\u56fa\u5b9a\u7a97\u53e3\u3001\u6ed1\u52d5\u7a97\u53e3\u548c\u4ee4\u724c\u6876\u7b49\uff0c\u800cRedis\u5247\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u9019\u4e9b\u7b56\u7565\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-VNnYLnzUKp\">\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\u64cd\u4f5c\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u80fd\u5920\u5728\u6beb\u79d2\u7d1a\u5225\u5167\u5b8c\u6210\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>\u8c50\u5bcc\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u652f\u6301\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9748\u6d3b\u6027\u9ad8\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e5%af%a6%e7%8f%be%e9%9a%8a%e5%88%97%e9%99%90%e6%b5%81%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%80%9d%e8%b7%af-VNnYLnzUKp\">Redis\u5be6\u73fe\u968a\u5217\u9650\u6d41\u7684\u57fa\u672c\u601d\u8def<\/h2>\n<p>\u4f7f\u7528Redis\u5be6\u73fe\u968a\u5217\u9650\u6d41\u7684\u57fa\u672c\u601d\u8def\u662f\u5229\u7528\u5176\u539f\u5b50\u64cd\u4f5c\u4f86\u63a7\u5236\u8acb\u6c42\u7684\u6578\u91cf\u3002\u4ee5\u4e0b\u662f\u5be6\u73fe\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u8a2d\u7f6e\u4e00\u500b\u9375\u4f86\u8a18\u9304\u8acb\u6c42\u7684\u6642\u9593\u6233\u3002<\/li>\n<li>\u6bcf\u7576\u6709\u8acb\u6c42\u9032\u4f86\u6642\uff0c\u6aa2\u67e5\u8a72\u9375\u7684\u503c\u3002<\/li>\n<li>\u5982\u679c\u8acb\u6c42\u6578\u91cf\u8d85\u904e\u9650\u5236\uff0c\u5247\u62d2\u7d55\u8a72\u8acb\u6c42\uff1b\u5426\u5247\uff0c\u5141\u8a31\u8acb\u6c42\u4e26\u66f4\u65b0\u9375\u7684\u503c\u3002<\/li>\n<\/ol>\n<h2 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-VNnYLnzUKp\">\u793a\u4f8b\u4ee3\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Redis\u5be6\u73fe\u7c21\u55ae\u9650\u6d41\u7684Python\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\nimport time\n\n# \u9023\u63a5\u5230Redis\nr = redis.StrictRedis(host='localhost', port=6379, db=0)\n\n# \u9650\u6d41\u51fd\u6578\ndef rate_limiter(user_id, limit, period):\n    current_time = int(time.time())\n    key = f\"rate_limit:{user_id}\"\n\n    # \u4f7f\u7528Redis\u7684\u7ba1\u9053\u4f86\u57f7\u884c\u539f\u5b50\u64cd\u4f5c\n    with r.pipeline() as pipe:\n        pipe.zremrangebyscore(key, 0, current_time - period)  # \u522a\u9664\u904e\u671f\u8acb\u6c42\n        pipe.zcard(key)  # \u7372\u53d6\u7576\u524d\u8acb\u6c42\u6578\u91cf\n        pipe.zadd(key, {current_time: current_time})  # \u6dfb\u52a0\u7576\u524d\u8acb\u6c42\n        pipe.expire(key, period)  # \u8a2d\u7f6e\u904e\u671f\u6642\u9593\n        current_count = pipe.execute()[1]  # \u7372\u53d6\u8acb\u6c42\u6578\u91cf\n\n    if current_count &gt; limit:\n        return False  # \u8d85\u904e\u9650\u5236\uff0c\u62d2\u7d55\u8acb\u6c42\n    return True  # \u5141\u8a31\u8acb\u6c42\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528Redis\u7684\u6709\u5e8f\u96c6\u5408\u4f86\u5b58\u5132\u8acb\u6c42\u7684\u6642\u9593\u6233\uff0c\u4e26\u901a\u904e\u539f\u5b50\u64cd\u4f5c\u4f86\u78ba\u4fdd\u8acb\u6c42\u6578\u91cf\u7684\u6e96\u78ba\u6027\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VNnYLnzUKp\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u5be6\u73fe\u968a\u5217\u9650\u6d41\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u6709\u6548\u5730\u63a7\u5236\u7cfb\u7d71\u7684\u8acb\u6c42\u901f\u7387\uff0c\u9632\u6b62\u904e\u8f09\u3002\u901a\u904e\u7c21\u55ae\u7684\u4ee3\u78bc\u5be6\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u9650\u6d41\u529f\u80fd\u96c6\u6210\u5230\u81ea\u5df1\u7684\u61c9\u7528\u4e2d\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u5728\u9ad8\u6d41\u91cf\u60c5\u6cc1\u4e0b\u7cfb\u7d71\u7684\u7a69\u5b9a\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528Redis\u5be6\u73fe\u968a\u5217\u9650\u6d41\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\uff0c\u9632\u6b62\u904e\u8f09\uff0c\u78ba\u4fdd\u7a69\u5b9a\u6027\u8207\u9ad8\u6548\u8655\u7406\u8acb\u6c42\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-167635","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167635","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=167635"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167635\/revisions"}],"predecessor-version":[{"id":167636,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167635\/revisions\/167636"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}