{"id":128969,"date":"2024-10-26T18:06:19","date_gmt":"2024-10-26T10:06:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/128969\/"},"modified":"2024-10-26T18:06:20","modified_gmt":"2024-10-26T10:06:20","slug":"redis%e5%af%a6%e7%8f%be%e9%a9%97%e8%ad%89%e7%a2%bc%e5%8a%9f%e8%83%bd%ef%bc%88%e9%a9%97%e8%ad%89%e7%a2%bc%e6%94%be%e5%85%a5redis%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/128969\/","title":{"rendered":"Redis\u5be6\u73fe\u9a57\u8b49\u78bc\u529f\u80fd\uff08\u9a57\u8b49\u78bc\u653e\u5165redis\uff09"},"content":{"rendered":"<h1 id=\"redis%e5%af%a6%e7%8f%be%e9%a9%97%e8%ad%89%e7%a2%bc%e5%8a%9f%e8%83%bd%ef%bc%88%e9%a9%97%e8%ad%89%e7%a2%bc%e6%94%be%e5%85%a5redis%ef%bc%89-PHwthwSjQi\">Redis\u5be6\u73fe\u9a57\u8b49\u78bc\u529f\u80fd\uff08\u9a57\u8b49\u78bc\u653e\u5165Redis\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u9a57\u8b49\u78bc\uff08CAPTCHA\uff09\u5df2\u6210\u70ba\u4fdd\u8b77\u7db2\u7ad9\u514d\u53d7\u6a5f\u5668\u4eba\u653b\u64ca\u7684\u91cd\u8981\u5de5\u5177\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u5b58\u5132\u9a57\u8b49\u78bc\u6210\u70ba\u4e86\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\u80fd\u529b\uff0c\u975e\u5e38\u9069\u5408\u7528\u65bc\u5b58\u5132\u9a57\u8b49\u78bc\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Redis\u5be6\u73fe\u9a57\u8b49\u78bc\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u76f8\u61c9\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-PHwthwSjQi\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u5177\u6709\u4ee5\u4e0b\u5e7e\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>Redis\u7684\u6578\u64da\u5b58\u53d6\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u80fd\u5920\u5728\u6beb\u79d2\u7d1a\u5225\u5167\u5b8c\u6210\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6301\u4e45\u6027\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u6578\u64da\u6301\u4e45\u5316\uff0c\u80fd\u5920\u5c07\u6578\u64da\u4fdd\u5b58\u5230\u78c1\u789f\u4e2d\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u4e0a\u624b\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\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=\"%e9%a9%97%e8%ad%89%e7%a2%bc%e7%9a%84%e7%94%9f%e6%88%90%e8%88%87%e5%ad%98%e5%84%b2-PHwthwSjQi\">\u9a57\u8b49\u78bc\u7684\u751f\u6210\u8207\u5b58\u5132<\/h2>\n<p>\u5728\u5be6\u73fe\u9a57\u8b49\u78bc\u529f\u80fd\u6642\uff0c\u9996\u5148\u9700\u8981\u751f\u6210\u4e00\u500b\u96a8\u6a5f\u7684\u9a57\u8b49\u78bc\uff0c\u7136\u5f8c\u5c07\u5176\u5b58\u5132\u5230Redis\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684Python\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u751f\u6210\u548c\u5b58\u5132\u9a57\u8b49\u78bc\uff1a<\/p>\n<pre><code>import redis\nimport random\nimport string\n\n# \u9023\u63a5\u5230Redis\nr = redis.StrictRedis(host='localhost', port=6379, db=0)\n\n# \u751f\u6210\u96a8\u6a5f\u9a57\u8b49\u78bc\ndef generate_captcha(length=6):\n    return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length))\n\n# \u5b58\u5132\u9a57\u8b49\u78bc\ndef store_captcha(user_id, captcha):\n    r.setex(user_id, 300, captcha)  # \u8a2d\u7f6e\u904e\u671f\u6642\u9593\u70ba300\u79d2\n\n# \u4f7f\u7528\u793a\u4f8b\nuser_id = 'user123'\ncaptcha = generate_captcha()\nstore_captcha(user_id, captcha)\nprint(f'\u751f\u6210\u7684\u9a57\u8b49\u78bc\u70ba: {captcha}')\n<\/code><\/pre>\n<h2 id=\"%e9%a9%97%e8%ad%89%e7%a2%bc%e7%9a%84%e9%a9%97%e8%ad%89-PHwthwSjQi\">\u9a57\u8b49\u78bc\u7684\u9a57\u8b49<\/h2>\n<p>\u7576\u7528\u6236\u63d0\u4ea4\u9a57\u8b49\u78bc\u6642\uff0c\u6211\u5011\u9700\u8981\u5f9eRedis\u4e2d\u6aa2\u7d22\u4e26\u9a57\u8b49\u8a72\u9a57\u8b49\u78bc\u3002\u4ee5\u4e0b\u662f\u9a57\u8b49\u78bc\u9a57\u8b49\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>def verify_captcha(user_id, input_captcha):\n    stored_captcha = r.get(user_id)\n    if stored_captcha is None:\n        return False  # \u9a57\u8b49\u78bc\u5df2\u904e\u671f\u6216\u4e0d\u5b58\u5728\n    return stored_captcha.decode('utf-8') == input_captcha\n\n# \u4f7f\u7528\u793a\u4f8b\ninput_captcha = '\u7528\u6236\u8f38\u5165\u7684\u9a57\u8b49\u78bc'\nif verify_captcha(user_id, input_captcha):\n    print('\u9a57\u8b49\u6210\u529f')\nelse:\n    print('\u9a57\u8b49\u5931\u6557')\n<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-PHwthwSjQi\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u5be6\u73fe\u9a57\u8b49\u78bc\u529f\u80fd\u6642\uff0c\u5b89\u5168\u6027\u662f\u4e0d\u53ef\u5ffd\u8996\u7684\u56e0\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5efa\u8b70\uff1a<\/p>\n<ul>\n<li><strong>\u904e\u671f\u6642\u9593\uff1a<\/strong>\u8a2d\u7f6e\u5408\u7406\u7684\u904e\u671f\u6642\u9593\uff0c\u9632\u6b62\u9a57\u8b49\u78bc\u88ab\u91cd\u8907\u4f7f\u7528\u3002<\/li>\n<li><strong>\u9650\u5236\u8acb\u6c42\u983b\u7387\uff1a<\/strong>\u5c0d\u540c\u4e00\u7528\u6236\u7684\u8acb\u6c42\u9032\u884c\u983b\u7387\u9650\u5236\uff0c\u9632\u6b62\u66b4\u529b\u7834\u89e3\u3002<\/li>\n<li><strong>\u4f7f\u7528HTTPS\uff1a<\/strong>\u78ba\u4fdd\u6578\u64da\u5728\u50b3\u8f38\u904e\u7a0b\u4e2d\u7684\u5b89\u5168\u6027\uff0c\u9632\u6b62\u4e2d\u9593\u4eba\u653b\u64ca\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-PHwthwSjQi\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u5be6\u73fe\u9a57\u8b49\u78bc\u529f\u80fd\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7cfb\u7d71\u7684\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u5730\u7ba1\u7406\u9a57\u8b49\u78bc\u7684\u5b58\u5132\u548c\u904e\u671f\u3002\u901a\u904e\u7c21\u55ae\u7684API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u751f\u6210\u3001\u5b58\u5132\u548c\u9a57\u8b49\u9a57\u8b49\u78bc\uff0c\u5f9e\u800c\u589e\u5f37\u7db2\u7ad9\u7684\u5b89\u5168\u6027\u3002\u82e5\u60a8\u9700\u8981\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u642d\u5efa\u60a8\u7684Redis\u670d\u52d9\u5668\uff0c\u9032\u4e00\u6b65\u63d0\u5347\u60a8\u7684\u61c9\u7528\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Redis\u5be6\u73fe\u9a57\u8b49\u78bc\u529f\u80fd\uff0c\u5c07\u9a57\u8b49\u78bc\u5b58\u5132\u65bcRedis\u4e2d\uff0c\u63d0\u5347\u5b89\u5168\u6027\u8207\u6548\u7387\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-128969","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128969","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=128969"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128969\/revisions"}],"predecessor-version":[{"id":128970,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/128969\/revisions\/128970"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=128969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=128969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=128969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}