{"id":184020,"date":"2024-11-10T09:40:19","date_gmt":"2024-11-10T01:40:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184020\/"},"modified":"2024-11-10T09:40:20","modified_gmt":"2024-11-10T01:40:20","slug":"%e4%bd%bf%e7%94%a8redis%e5%ae%89%e5%85%a8%e9%a9%97%e8%ad%89%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e5%af%86%e7%a2%bc%ef%bc%88redis-%e9%a9%97%e8%ad%89%e7%99%bb%e9%8c%84%e5%af%86%e7%a2%bc%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184020\/","title":{"rendered":"\u4f7f\u7528Redis\u5b89\u5168\u9a57\u8b49\u7528\u6236\u767b\u9304\u5bc6\u78bc\uff08redis \u9a57\u8b49\u767b\u9304\u5bc6\u78bc\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e5%ae%89%e5%85%a8%e9%a9%97%e8%ad%89%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e5%af%86%e7%a2%bc%ef%bc%88redis-%e9%a9%97%e8%ad%89%e7%99%bb%e9%8c%84%e5%af%86%e7%a2%bc%ef%bc%89-rIzfQcuIzZ\">\u4f7f\u7528Redis\u5b89\u5168\u9a57\u8b49\u7528\u6236\u767b\u9304\u5bc6\u78bc\uff08redis \u9a57\u8b49\u767b\u9304\u5bc6\u78bc\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u5b89\u5168\u6027\u662f\u6bcf\u500b\u61c9\u7528\u7a0b\u5e8f\u548c\u7cfb\u7d71\u8a2d\u8a08\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u96a8\u8457\u6578\u64da\u6d29\u9732\u4e8b\u4ef6\u7684\u983b\u7e41\u767c\u751f\uff0c\u958b\u767c\u8005\u9700\u8981\u5c0b\u627e\u6709\u6548\u7684\u65b9\u6cd5\u4f86\u4fdd\u8b77\u7528\u6236\u7684\u767b\u9304\u4fe1\u606f\u3002Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u529f\u80fd\u4f86\u5e6b\u52a9\u958b\u767c\u8005\u5be6\u73fe\u5b89\u5168\u7684\u7528\u6236\u767b\u9304\u9a57\u8b49\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Redis\u4f86\u5b89\u5168\u5730\u9a57\u8b49\u7528\u6236\u767b\u9304\u5bc6\u78bc\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-rIzfQcuIzZ\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u8655\u7406\u7b49\u5834\u666f\u3002\u5176\u5feb\u901f\u7684\u8b80\u5beb\u901f\u5ea6\u4f7f\u5f97Redis\u6210\u70ba\u5b58\u5132\u7528\u6236\u767b\u9304\u4fe1\u606f\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e4%be%86%e9%a9%97%e8%ad%89%e7%99%bb%e9%8c%84%e5%af%86%e7%a2%bc%ef%bc%9f-rIzfQcuIzZ\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f86\u9a57\u8b49\u767b\u9304\u5bc6\u78bc\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5176\u80fd\u5920\u5feb\u901f\u8655\u7406\u5927\u91cf\u7684\u8acb\u6c42\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u9ad8\u983b\u7387\u767b\u9304\u9a57\u8b49\u7684\u61c9\u7528\u5c24\u70ba\u91cd\u8981\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\u5730\u5c07\u5176\u96c6\u6210\u5230\u73fe\u6709\u7684\u61c9\u7528\u7a0b\u5e8f\u4e2d\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u5b58\u5132\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e9%80%b2%e8%a1%8c%e5%ae%89%e5%85%a8%e7%9a%84%e7%94%a8%e6%88%b6%e7%99%bb%e9%8c%84%e9%a9%97%e8%ad%89-rIzfQcuIzZ\">\u5982\u4f55\u4f7f\u7528Redis\u9032\u884c\u5b89\u5168\u7684\u7528\u6236\u767b\u9304\u9a57\u8b49<\/h2>\n<p>\u4f7f\u7528Redis\u9032\u884c\u7528\u6236\u767b\u9304\u9a57\u8b49\u7684\u57fa\u672c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>\u7528\u6236\u8a3b\u518a\uff1a<\/strong>\u5728\u7528\u6236\u8a3b\u518a\u6642\uff0c\u5c07\u7528\u6236\u7684\u5bc6\u78bc\u9032\u884c\u54c8\u5e0c\u8655\u7406\uff0c\u7136\u5f8c\u5c07\u54c8\u5e0c\u503c\u5b58\u5132\u5230Redis\u4e2d\u3002\u9019\u6a23\uff0c\u5373\u4f7fRedis\u6578\u64da\u5eab\u88ab\u653b\u64ca\uff0c\u653b\u64ca\u8005\u4e5f\u7121\u6cd5\u7372\u53d6\u7528\u6236\u7684\u660e\u6587\u5bc6\u78bc\u3002<\/li>\n<li><strong>\u7528\u6236\u767b\u9304\uff1a<\/strong>\u7576\u7528\u6236\u767b\u9304\u6642\uff0c\u7cfb\u7d71\u6703\u5c07\u7528\u6236\u8f38\u5165\u7684\u5bc6\u78bc\u9032\u884c\u540c\u6a23\u7684\u54c8\u5e0c\u8655\u7406\uff0c\u7136\u5f8c\u8207Redis\u4e2d\u5b58\u5132\u7684\u54c8\u5e0c\u503c\u9032\u884c\u6bd4\u5c0d\u3002\u5982\u679c\u5169\u8005\u76f8\u540c\uff0c\u5247\u9a57\u8b49\u6210\u529f\u3002<\/li>\n<\/ol>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-rIzfQcuIzZ\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Python\u548cRedis\u9032\u884c\u7528\u6236\u767b\u9304\u9a57\u8b49\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\nimport hashlib\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\ndef hash_password(password):\n    return hashlib.sha256(password.encode()).hexdigest()\n\ndef register_user(username, password):\n    hashed_password = hash_password(password)\n    r.set(username, hashed_password)\n\ndef login_user(username, password):\n    hashed_password = hash_password(password)\n    stored_password = r.get(username)\n    \n    if stored_password and stored_password.decode() == hashed_password:\n        return \"\u767b\u9304\u6210\u529f\"\n    else:\n        return \"\u767b\u9304\u5931\u6557\"\n\n# \u8a3b\u518a\u7528\u6236\nregister_user('user1', 'mypassword')\n\n# \u767b\u9304\u7528\u6236\nprint(login_user('user1', 'mypassword'))  # \u767b\u9304\u6210\u529f\nprint(login_user('user1', 'wrongpassword'))  # \u767b\u9304\u5931\u6557\n<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-rIzfQcuIzZ\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u9032\u884c\u7528\u6236\u767b\u9304\u9a57\u8b49\u6642\uff0c\u9084\u9700\u8981\u8003\u616e\u4ee5\u4e0b\u5b89\u5168\u6027\u554f\u984c\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528SSL\/TLS\u52a0\u5bc6\uff1a<\/strong>\u78ba\u4fdd\u8207Redis\u7684\u9023\u63a5\u662f\u52a0\u5bc6\u7684\uff0c\u4ee5\u9632\u6b62\u6578\u64da\u5728\u50b3\u8f38\u904e\u7a0b\u4e2d\u88ab\u7aca\u53d6\u3002<\/li>\n<li><strong>\u8a2d\u7f6e\u8a2a\u554f\u63a7\u5236\uff1a<\/strong>\u9650\u5236\u5c0dRedis\u7684\u8a2a\u554f\u6b0a\u9650\uff0c\u50c5\u5141\u8a31\u6388\u6b0a\u7684\u61c9\u7528\u7a0b\u5e8f\u548c\u7528\u6236\u8a2a\u554f\u3002<\/li>\n<li><strong>\u5b9a\u671f\u66f4\u65b0\u5bc6\u78bc\uff1a<\/strong>\u9f13\u52f5\u7528\u6236\u5b9a\u671f\u66f4\u6539\u5bc6\u78bc\uff0c\u4ee5\u63d0\u9ad8\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-rIzfQcuIzZ\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u9032\u884c\u7528\u6236\u767b\u9304\u5bc6\u78bc\u7684\u5b89\u5168\u9a57\u8b49\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u53ef\u9760\u7684\u65b9\u6cd5\u3002\u901a\u904e\u5c07\u5bc6\u78bc\u9032\u884c\u54c8\u5e0c\u8655\u7406\u4e26\u5b58\u5132\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u8b77\u7528\u6236\u7684\u654f\u611f\u4fe1\u606f\u3002\u96a8\u8457\u7db2\u7d61\u5b89\u5168\u5a01\u8105\u7684\u589e\u52a0\uff0c\u958b\u767c\u8005\u5fc5\u9808\u63a1\u53d6\u5fc5\u8981\u7684\u63aa\u65bd\u4f86\u4fdd\u8b77\u7528\u6236\u6578\u64da\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\uff0c\u5982 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u6216 <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a>\uff0c\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u6574\u9ad4\u7cfb\u7d71\u7684\u5b89\u5168\u6027\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u9032\u884c\u5b89\u5168\u9a57\u8b49\uff0c\u4fdd\u8b77\u7528\u6236\u767b\u9304\u5bc6\u78bc\uff0c\u78ba\u4fdd\u6578\u64da\u5b89\u5168\u8207\u96b1\u79c1\uff0c\u63d0\u5347\u7cfb\u7d71\u5b89\u5168\u6027\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-184020","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184020","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=184020"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184020\/revisions"}],"predecessor-version":[{"id":184021,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184020\/revisions\/184021"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}