{"id":163003,"date":"2024-11-04T19:50:21","date_gmt":"2024-11-04T11:50:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/163003\/"},"modified":"2024-11-04T19:50:23","modified_gmt":"2024-11-04T11:50:23","slug":"%e6%8e%a5%e5%8f%a3%e9%99%90%e6%b5%81%e5%9f%ba%e6%96%bcredis%e7%9a%84%e5%af%a6%e8%b8%90%ef%bc%88%e6%8e%a5%e5%8f%a3%e9%99%90%e6%b5%81redis%e5%af%a6%e7%8f%be%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/163003\/","title":{"rendered":"\u63a5\u53e3\u9650\u6d41\u57fa\u65bcRedis\u7684\u5be6\u8e10\uff08\u63a5\u53e3\u9650\u6d41Redis\u5be6\u73fe\uff09"},"content":{"rendered":"<h1 id=\"%e6%8e%a5%e5%8f%a3%e9%99%90%e6%b5%81%e5%9f%ba%e6%96%bcredis%e7%9a%84%e5%af%a6%e8%b8%90%ef%bc%88%e6%8e%a5%e5%8f%a3%e9%99%90%e6%b5%81redis%e5%af%a6%e7%8f%be%ef%bc%89-DQvoPZirKY\">\u63a5\u53e3\u9650\u6d41\u57fa\u65bcRedis\u7684\u5be6\u8e10\uff08\u63a5\u53e3\u9650\u6d41Redis\u5be6\u73fe\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u63a5\u53e3\u9650\u6d41\u662f\u4e00\u500b\u81f3\u95dc\u91cd\u8981\u7684\u6280\u8853\uff0c\u5c24\u5176\u662f\u5728\u9ad8\u6d41\u91cf\u7684\u61c9\u7528\u4e2d\u3002\u63a5\u53e3\u9650\u6d41\u7684\u4e3b\u8981\u76ee\u7684\u662f\u9632\u6b62\u7cfb\u7d71\u904e\u8f09\uff0c\u4fdd\u8b77\u5f8c\u7aef\u670d\u52d9\u7684\u7a69\u5b9a\u6027\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u8b80\u5beb\u6027\u80fd\u548c\u7c21\u55ae\u7684\u6578\u64da\u7d50\u69cb\uff0c\u6210\u70ba\u5be6\u73fe\u63a5\u53e3\u9650\u6d41\u7684\u7406\u60f3\u9078\u64c7\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u57fa\u65bcRedis\u5be6\u73fe\u63a5\u53e3\u9650\u6d41\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%8e%a5%e5%8f%a3%e9%99%90%e6%b5%81%ef%bc%9f-DQvoPZirKY\">\u4ec0\u9ebc\u662f\u63a5\u53e3\u9650\u6d41\uff1f<\/h2>\n<p>\u63a5\u53e3\u9650\u6d41\u662f\u6307\u5c0dAPI\u8acb\u6c42\u7684\u6578\u91cf\u9032\u884c\u9650\u5236\uff0c\u4ee5\u9632\u6b62\u904e\u591a\u7684\u8acb\u6c42\u5c0e\u81f4\u670d\u52d9\u5668\u904e\u8f09\u3002\u9019\u7a2e\u6280\u8853\u901a\u5e38\u7528\u65bc\u4fdd\u8b77\u5f8c\u7aef\u670d\u52d9\uff0c\u78ba\u4fdd\u7cfb\u7d71\u80fd\u5920\u7a69\u5b9a\u904b\u884c\u3002\u5e38\u898b\u7684\u9650\u6d41\u7b56\u7565\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u56fa\u5b9a\u7a97\u53e3\u9650\u6d41\uff1a<\/strong>\u5728\u56fa\u5b9a\u7684\u6642\u9593\u7a97\u53e3\u5167\u9650\u5236\u8acb\u6c42\u6578\u91cf\u3002<\/li>\n<li><strong>\u6ed1\u52d5\u7a97\u53e3\u9650\u6d41\uff1a<\/strong>\u6839\u64da\u8acb\u6c42\u7684\u6642\u9593\u6233\u52d5\u614b\u8a08\u7b97\u8acb\u6c42\u6578\u91cf\u3002<\/li>\n<li><strong>\u6f0f\u6876\u7b97\u6cd5\uff1a<\/strong>\u4ee5\u56fa\u5b9a\u7684\u901f\u7387\u8655\u7406\u8acb\u6c42\uff0c\u8d85\u904e\u901f\u7387\u7684\u8acb\u6c42\u5c07\u88ab\u4e1f\u68c4\u3002<\/li>\n<li><strong>\u4ee4\u724c\u6876\u7b97\u6cd5\uff1a<\/strong>\u4ee5\u4e00\u5b9a\u7684\u901f\u7387\u751f\u6210\u4ee4\u724c\uff0c\u8acb\u6c42\u9700\u8981\u6d88\u8017\u4ee4\u724c\u624d\u80fd\u901a\u904e\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-DQvoPZirKY\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\uff1f<\/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><strong>\u9ad8\u6548\u7684\u6027\u80fd\uff1a<\/strong>Redis\u7684\u8b80\u5beb\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9069\u5408\u9ad8\u983b\u6b21\u7684\u8acb\u6c42\u8655\u7406\u3002<\/li>\n<li><strong>\u7c21\u55ae\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u7b49\uff0c\u65b9\u4fbf\u5b58\u5132\u548c\u7ba1\u7406\u9650\u6d41\u6578\u64da\u3002<\/li>\n<li><strong>\u539f\u5b50\u64cd\u4f5c\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u591a\u7a2e\u539f\u5b50\u64cd\u4f5c\uff0c\u80fd\u5920\u4fdd\u8b49\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%9f%ba%e6%96%bcredis%e7%9a%84%e6%8e%a5%e5%8f%a3%e9%99%90%e6%b5%81%e5%af%a6%e7%8f%be-DQvoPZirKY\">\u57fa\u65bcRedis\u7684\u63a5\u53e3\u9650\u6d41\u5be6\u73fe<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u57fa\u65bcRedis\u7684\u7c21\u55ae\u63a5\u53e3\u9650\u6d41\u5be6\u73fe\u793a\u4f8b\uff0c\u4f7f\u7528Python\u548cFlask\u6846\u67b6\u9032\u884c\u8aaa\u660e\u3002<\/p>\n<h3 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-DQvoPZirKY\">\u74b0\u5883\u6e96\u5099<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u5df2\u5b89\u88ddFlask\u548cRedis\u7684Python\u5ba2\u6236\u7aef\uff1a<\/p>\n<pre><code>pip install Flask redis<\/code><\/pre>\n<h3 id=\"%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-DQvoPZirKY\">\u4ee3\u78bc\u793a\u4f8b<\/h3>\n<pre><code>from flask import Flask, request, jsonify\nimport redis\nimport time\n\napp = Flask(__name__)\nr = redis.StrictRedis(host='localhost', port=6379, db=0)\n\n# \u8a2d\u5b9a\u9650\u6d41\u53c3\u6578\nRATE_LIMIT = 5  # \u6bcf\u5206\u9418\u5141\u8a31\u7684\u8acb\u6c42\u6578\nTIME_WINDOW = 60  # \u6642\u9593\u7a97\u53e3\uff08\u79d2\uff09\n\n@app.route('\/api\/resource', methods=['GET'])\ndef resource():\n    client_ip = request.remote_addr\n    current_time = int(time.time())\n    \n    # \u8a08\u7b97\u7576\u524d\u6642\u9593\u7a97\u53e3\u7684\u8acb\u6c42\u6578\n    request_count = r.get(client_ip)\n    \n    if request_count is None:\n        # \u5982\u679c\u6c92\u6709\u8acb\u6c42\u8a18\u9304\uff0c\u521d\u59cb\u5316\u8acb\u6c42\u6578\n        r.set(client_ip, 1, ex=TIME_WINDOW)\n        return jsonify({\"message\": \"Request successful!\"}), 200\n    elif int(request_count) &lt; RATE_LIMIT:\n        # \u5982\u679c\u8acb\u6c42\u6578\u5c0f\u65bc\u9650\u6d41\u503c\uff0c\u589e\u52a0\u8acb\u6c42\u6578\n        r.incr(client_ip)\n        return jsonify({&quot;message&quot;: &quot;Request successful!&quot;}), 200\n    else:\n        # \u8d85\u904e\u9650\u6d41\u503c\uff0c\u8fd4\u56de\u932f\u8aa4\n        return jsonify({&quot;message&quot;: &quot;Too many requests, please try again later.&quot;}), 429\n\nif __name__ == &#039;__main__&#039;:\n    app.run(debug=True)<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u5ba2\u6236\u7aef\u7684IP\u5730\u5740\u4f5c\u70ba\u9375\uff0c\u4e26\u5c07\u8acb\u6c42\u6578\u91cf\u5b58\u5132\u5728Redis\u4e2d\u3002\u6bcf\u7576\u8acb\u6c42\u5230\u9054\u6642\uff0c\u6211\u5011\u6aa2\u67e5\u8a72IP\u7684\u8acb\u6c42\u6578\u91cf\uff0c\u5982\u679c\u672a\u8d85\u904e\u9650\u5236\uff0c\u5247\u5141\u8a31\u8acb\u6c42\u4e26\u589e\u52a0\u8a08\u6578\uff1b\u5982\u679c\u8d85\u904e\u9650\u5236\uff0c\u5247\u8fd4\u56de429\u932f\u8aa4\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-DQvoPZirKY\">\u7e3d\u7d50<\/h2>\n<p>\u57fa\u65bcRedis\u7684\u63a5\u53e3\u9650\u6d41\u5be6\u73fe\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u7c21\u55ae\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u6709\u6548\u4fdd\u8b77\u5f8c\u7aef\u670d\u52d9\u7684\u7a69\u5b9a\u6027\u3002\u901a\u904e\u5408\u7406\u8a2d\u7f6e\u9650\u6d41\u53c3\u6578\uff0c\u53ef\u4ee5\u5728\u9ad8\u6d41\u91cf\u60c5\u6cc1\u4e0b\u4fdd\u6301\u7cfb\u7d71\u7684\u7a69\u5b9a\u904b\u884c\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u9ad8\u6027\u80fd\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u670d\u52d9\u5668\u67b6\u69cb\u81f3\u95dc\u91cd\u8981\u3002\u82e5\u60a8\u5c0d\u65bc<em><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/em>\u6216<em><a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a><\/em>\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u57fa\u65bcRedis\u7684\u63a5\u53e3\u9650\u6d41\u5be6\u8e10\uff0c\u63d0\u5347\u7cfb\u7d71\u7a69\u5b9a\u6027\uff0c\u9632\u6b62\u904e\u8f09\uff0c\u78ba\u4fddAPI\u9ad8\u6548\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-163003","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163003","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=163003"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163003\/revisions"}],"predecessor-version":[{"id":163004,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163003\/revisions\/163004"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=163003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=163003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=163003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}