{"id":128397,"date":"2024-10-26T13:50:17","date_gmt":"2024-10-26T05:50:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/128397\/"},"modified":"2024-10-26T13:50:18","modified_gmt":"2024-10-26T05:50:18","slug":"redis-%e5%af%a6%e7%8f%be%e7%99%bb%e9%8c%84%e6%ac%a1%e6%95%b8%e9%99%90%e5%88%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/128397\/","title":{"rendered":"Redis \u5be6\u73fe\u767b\u9304\u6b21\u6578\u9650\u5236"},"content":{"rendered":"<h1 id=\"redis-%e5%af%a6%e7%8f%be%e7%99%bb%e9%8c%84%e6%ac%a1%e6%95%b8%e9%99%90%e5%88%b6-aZKsDbWnQL\">Redis \u5be6\u73fe\u767b\u9304\u6b21\u6578\u9650\u5236<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u5b89\u5168\u6027\u662f\u6bcf\u500b\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u7684\u9996\u8981\u8003\u91cf\u4e4b\u4e00\u3002\u767b\u9304\u6b21\u6578\u9650\u5236\u662f\u4e00\u7a2e\u6709\u6548\u7684\u5b89\u5168\u63aa\u65bd\uff0c\u53ef\u4ee5\u9632\u6b62\u66b4\u529b\u7834\u89e3\u653b\u64ca\u3002Redis \u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u7c21\u55ae\u800c\u5f37\u5927\u7684\u5de5\u5177\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 Redis \u4f86\u5be6\u73fe\u767b\u9304\u6b21\u6578\u9650\u5236\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%e7%99%bb%e9%8c%84%e6%ac%a1%e6%95%b8%e9%99%90%e5%88%b6%ef%bc%9f-aZKsDbWnQL\">\u70ba\u4ec0\u9ebc\u9700\u8981\u767b\u9304\u6b21\u6578\u9650\u5236\uff1f<\/h2>\n<p>\u767b\u9304\u6b21\u6578\u9650\u5236\u7684\u4e3b\u8981\u76ee\u7684\u662f\u9632\u6b62\u672a\u7d93\u6388\u6b0a\u7684\u8a2a\u554f\u3002\u7576\u653b\u64ca\u8005\u8a66\u5716\u901a\u904e\u66b4\u529b\u7834\u89e3\u4f86\u7372\u53d6\u7528\u6236\u7684\u767b\u9304\u6191\u8b49\u6642\uff0c\u767b\u9304\u6b21\u6578\u9650\u5236\u53ef\u4ee5\u6709\u6548\u5730\u6e1b\u5c11\u9019\u7a2e\u653b\u64ca\u7684\u6210\u529f\u7387\u3002\u901a\u904e\u9650\u5236\u6bcf\u500b\u7528\u6236\u5728\u4e00\u5b9a\u6642\u9593\u5167\u7684\u767b\u9304\u5617\u8a66\u6b21\u6578\uff0c\u53ef\u4ee5\u4fdd\u8b77\u7528\u6236\u7684\u5e33\u6236\u5b89\u5168\u3002<\/p>\n<h2 id=\"redis-%e7%9a%84%e5%84%aa%e5%8b%a2-aZKsDbWnQL\">Redis \u7684\u512a\u52e2<\/h2>\n<p>Redis \u662f\u4e00\u500b\u958b\u6e90\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5177\u6709\u4ee5\u4e0b\u5e7e\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis \u63d0\u4f9b\u6beb\u79d2\u7d1a\u7684\u97ff\u61c9\u6642\u9593\uff0c\u9069\u5408\u9ad8\u983b\u7387\u7684\u8b80\u5beb\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis \u7684 API \u7c21\u55ae\u660e\u77ad\uff0c\u6613\u65bc\u96c6\u6210\u5230\u5404\u7a2e\u61c9\u7528\u4e2d\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u9664\u4e86\u9375\u503c\u5c0d\uff0cRedis \u9084\u652f\u6301\u5217\u8868\u3001\u96c6\u5408\u3001\u6709\u5e8f\u96c6\u5408\u7b49\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9748\u6d3b\u6027\u9ad8\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e7%8f%be%e7%99%bb%e9%8c%84%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-aZKsDbWnQL\">\u5be6\u73fe\u767b\u9304\u6b21\u6578\u9650\u5236\u7684\u57fa\u672c\u601d\u8def<\/h2>\n<p>\u4f7f\u7528 Redis \u5be6\u73fe\u767b\u9304\u6b21\u6578\u9650\u5236\u7684\u57fa\u672c\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u6bcf\u7576\u7528\u6236\u5617\u8a66\u767b\u9304\u6642\uff0c\u6aa2\u67e5\u5176\u767b\u9304\u6b21\u6578\u3002<\/li>\n<li>\u5982\u679c\u767b\u9304\u6b21\u6578\u8d85\u904e\u9650\u5236\uff0c\u5247\u62d2\u7d55\u767b\u9304\u8acb\u6c42\u3002<\/li>\n<li>\u5982\u679c\u767b\u9304\u6210\u529f\uff0c\u5247\u91cd\u7f6e\u767b\u9304\u6b21\u6578\u3002<\/li>\n<li>\u5982\u679c\u767b\u9304\u5931\u6557\uff0c\u5247\u589e\u52a0\u767b\u9304\u6b21\u6578\u4e26\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u3002<\/li>\n<\/ol>\n<h2 id=\"%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-aZKsDbWnQL\">\u4ee3\u78bc\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 Redis \u5be6\u73fe\u767b\u9304\u6b21\u6578\u9650\u5236\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\nimport time\n\n# \u9023\u63a5\u5230 Redis\nr = redis.StrictRedis(host='localhost', port=6379, db=0)\n\ndef login(username, password):\n    # \u8a2d\u5b9a\u6700\u5927\u767b\u9304\u6b21\u6578\u548c\u904e\u671f\u6642\u9593\n    max_attempts = 5\n    block_time = 300  # 5 \u5206\u9418\n\n    # \u69cb\u5efa Redis \u9375\n    key = f\"login_attempts:{username}\"\n\n    # \u7372\u53d6\u7576\u524d\u767b\u9304\u6b21\u6578\n    attempts = r.get(key)\n\n    if attempts and int(attempts) &gt;= max_attempts:\n        print(\"\u5e33\u6236\u5df2\u88ab\u9396\u5b9a\uff0c\u8acb\u7a0d\u5f8c\u518d\u8a66\u3002\")\n        return False\n\n    # \u5047\u8a2d\u9019\u88e1\u6709\u4e00\u500b\u9a57\u8b49\u7528\u6236\u7684\u51fd\u6578\n    if authenticate(username, password):\n        # \u767b\u9304\u6210\u529f\uff0c\u91cd\u7f6e\u767b\u9304\u6b21\u6578\n        r.delete(key)\n        print(\"\u767b\u9304\u6210\u529f\uff01\")\n        return True\n    else:\n        # \u767b\u9304\u5931\u6557\uff0c\u589e\u52a0\u767b\u9304\u6b21\u6578\n        r.incr(key)\n        if attempts is None:\n            r.expire(key, block_time)  # \u8a2d\u7f6e\u904e\u671f\u6642\u9593\n        print(\"\u767b\u9304\u5931\u6557\uff01\")\n        return False\n\ndef authenticate(username, password):\n    # \u9019\u88e1\u61c9\u8a72\u6709\u5be6\u969b\u7684\u7528\u6236\u9a57\u8b49\u908f\u8f2f\n    return username == \"admin\" and password == \"password\"\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-aZKsDbWnQL\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528 Redis \u5be6\u73fe\u767b\u9304\u6b21\u6578\u9650\u5236\u662f\u4e00\u7a2e\u6709\u6548\u7684\u5b89\u5168\u63aa\u65bd\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002\u901a\u904e\u7c21\u55ae\u7684\u4ee3\u78bc\u5be6\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u96c6\u6210\u9019\u4e00\u529f\u80fd\uff0c\u4fdd\u8b77\u7528\u6236\u5e33\u6236\u4e0d\u53d7\u66b4\u529b\u7834\u89e3\u653b\u64ca\u7684\u5a01\u8105\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u9ad8\u53ef\u7528\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\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Redis \u5be6\u73fe\u767b\u9304\u6b21\u6578\u9650\u5236\uff0c\u9632\u6b62\u66b4\u529b\u7834\u89e3\uff0c\u63d0\u5347\u7cfb\u7d71\u5b89\u5168\u6027\uff0c\u78ba\u4fdd\u7528\u6236\u8cc7\u6599\u5b89\u5168\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-128397","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128397","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=128397"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128397\/revisions"}],"predecessor-version":[{"id":128398,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128397\/revisions\/128398"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=128397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=128397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=128397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}