{"id":128585,"date":"2024-10-26T15:15:39","date_gmt":"2024-10-26T07:15:39","guid":{"rendered":"https:\/\/server.hk\/cnblog\/128585\/"},"modified":"2024-10-26T15:15:39","modified_gmt":"2024-10-26T07:15:39","slug":"redis%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e9%99%90%e6%b5%81%e8%a8%88%e6%95%b8%e5%8a%9f%e8%83%bd%ef%bc%88redis-%e9%99%90%e6%b5%81%e8%a8%88%e6%95%b8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/128585\/","title":{"rendered":"Redis\u5be6\u73fe\u9ad8\u6548\u9650\u6d41\u8a08\u6578\u529f\u80fd\uff08redis \u9650\u6d41\u8a08\u6578\uff09"},"content":{"rendered":"<h1 id=\"redis%e5%af%a6%e7%8f%be%e9%ab%98%e6%95%88%e9%99%90%e6%b5%81%e8%a8%88%e6%95%b8%e5%8a%9f%e8%83%bd%ef%bc%88redis-%e9%99%90%e6%b5%81%e8%a8%88%e6%95%b8%ef%bc%89-MVBpTnWjEV\">Redis\u5be6\u73fe\u9ad8\u6548\u9650\u6d41\u8a08\u6578\u529f\u80fd\uff08redis \u9650\u6d41\u8a08\u6578\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u9650\u6d41\u6280\u8853\u5df2\u6210\u70ba\u4fdd\u8b77\u61c9\u7528\u7a0b\u5e8f\u548c\u670d\u52d9\u7684\u91cd\u8981\u624b\u6bb5\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u8acb\u6c42\u6d41\u91cf\uff0c\u9632\u6b62\u670d\u52d9\u904e\u8f09\uff0c\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u7d50\u69cb\u548c\u64cd\u4f5c\uff0c\u7279\u5225\u9069\u5408\u7528\u65bc\u5be6\u73fe\u9650\u6d41\u8a08\u6578\u529f\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%99%90%e6%b5%81%ef%bc%9f-MVBpTnWjEV\">\u4ec0\u9ebc\u662f\u9650\u6d41\uff1f<\/h2>\n<p>\u9650\u6d41\u662f\u6307\u5c0d\u67d0\u4e00\u8cc7\u6e90\u7684\u8a2a\u554f\u9032\u884c\u63a7\u5236\uff0c\u4ee5\u9632\u6b62\u904e\u591a\u7684\u8acb\u6c42\u5c0e\u81f4\u7cfb\u7d71\u5d29\u6f70\u6216\u6027\u80fd\u4e0b\u964d\u3002\u9650\u6d41\u53ef\u4ee5\u6839\u64da\u6642\u9593\u7a97\u53e3\u3001\u8acb\u6c42\u6578\u91cf\u7b49\u591a\u7a2e\u65b9\u5f0f\u9032\u884c\u8a2d\u7f6e\u3002\u5e38\u898b\u7684\u9650\u6d41\u7b56\u7565\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u56fa\u5b9a\u7a97\u53e3\u9650\u6d41\uff1a\u5728\u56fa\u5b9a\u7684\u6642\u9593\u7a97\u53e3\u5167\u9650\u5236\u8acb\u6c42\u6578\u91cf\u3002<\/li>\n<li>\u6ed1\u52d5\u7a97\u53e3\u9650\u6d41\uff1a\u6839\u64da\u6700\u8fd1\u7684\u8acb\u6c42\u6642\u9593\u52d5\u614b\u8abf\u6574\u9650\u6d41\u3002<\/li>\n<li>\u4ee4\u724c\u6876\u7b97\u6cd5\uff1a\u901a\u904e\u4ee4\u724c\u7684\u751f\u6210\u548c\u6d88\u8017\u4f86\u63a7\u5236\u8acb\u6c42\u901f\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-MVBpTnWjEV\">\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\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\u985e\u578b\uff0c\u9748\u6d3b\u6027\u9ad8\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e9%99%90%e6%b5%81%e8%a8%88%e6%95%b8%e7%9a%84%e5%af%a6%e7%8f%be-MVBpTnWjEV\">Redis\u9650\u6d41\u8a08\u6578\u7684\u5be6\u73fe<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u5be6\u73fe\u9650\u6d41\u8a08\u6578\u7684\u4e00\u500b\u7c21\u55ae\u793a\u4f8b\uff0c\u9019\u88e1\u6211\u5011\u4f7f\u7528\u56fa\u5b9a\u7a97\u53e3\u9650\u6d41\u7684\u65b9\u5f0f\uff1a<\/p>\n<pre><code>import redis\nimport time\n\n# \u9023\u63a5\u5230Redis\nr = redis.StrictRedis(host='localhost', port=6379, db=0)\n\ndef is_request_allowed(user_id, limit, window):\n    current_time = int(time.time())\n    key = f\"rate_limit:{user_id}:{current_time \/\/ window}\"\n\n    # \u589e\u52a0\u8acb\u6c42\u8a08\u6578\n    current_count = r.incr(key)\n\n    # \u8a2d\u7f6e\u904e\u671f\u6642\u9593\n    if current_count == 1:\n        r.expire(key, window)\n\n    return current_count &lt;= limit\n\n# \u4f7f\u7528\u793a\u4f8b\nuser_id = &quot;user123&quot;\nlimit = 5  # \u6bcf\u500b\u7a97\u53e3\u5141\u8a31\u7684\u8acb\u6c42\u6578\nwindow = 60  # \u7a97\u53e3\u6642\u9593\uff08\u79d2\uff09\n\nif is_request_allowed(user_id, limit, window):\n    print(&quot;\u8acb\u6c42\u88ab\u5141\u8a31&quot;)\nelse:\n    print(&quot;\u8acb\u6c42\u88ab\u62d2\u7d55\uff0c\u8d85\u904e\u9650\u6d41\u9650\u5236&quot;)\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u6578 <code>is_request_allowed<\/code>\uff0c\u5b83\u63a5\u53d7\u7528\u6236ID\u3001\u8acb\u6c42\u9650\u5236\u548c\u6642\u9593\u7a97\u53e3\u4f5c\u70ba\u53c3\u6578\u3002\u6bcf\u7576\u7528\u6236\u767c\u9001\u8acb\u6c42\u6642\uff0c\u6211\u5011\u6703\u6aa2\u67e5\u7576\u524d\u7684\u8acb\u6c42\u8a08\u6578\u662f\u5426\u8d85\u904e\u9650\u5236\u3002\u5982\u679c\u8d85\u904e\u9650\u5236\uff0c\u5247\u62d2\u7d55\u8acb\u6c42\uff1b\u5426\u5247\uff0c\u5141\u8a31\u8acb\u6c42\u4e26\u66f4\u65b0\u8a08\u6578\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-MVBpTnWjEV\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u8f15\u9b06\u5be6\u73fe\u9ad8\u6548\u7684\u9650\u6d41\u8a08\u6578\u529f\u80fd\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u8acb\u6c42\u6d41\u91cf\uff0c\u4fdd\u8b77\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\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\u5c07\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u7121\u8ad6\u662f\u4f7f\u7528Redis\u9084\u662f\u5176\u4ed6\u6280\u8853\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7684\u7a69\u5b9a\u904b\u884c\u90fd\u662f\u6bcf\u4f4d\u958b\u767c\u8005\u7684\u8cac\u4efb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u5be6\u73fe\u9ad8\u6548\u7684\u9650\u6d41\u8a08\u6578\u529f\u80fd\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u8207\u7a69\u5b9a\u6027\uff0c\u78ba\u4fdd\u6d41\u91cf\u7ba1\u7406\u7684\u6700\u4f73\u5be6\u8e10\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-128585","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128585","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=128585"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128585\/revisions"}],"predecessor-version":[{"id":128586,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128585\/revisions\/128586"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=128585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=128585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=128585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}