{"id":185102,"date":"2024-11-10T18:45:17","date_gmt":"2024-11-10T10:45:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/185102\/"},"modified":"2024-11-10T18:45:18","modified_gmt":"2024-11-10T10:45:18","slug":"%e4%bd%bf%e7%94%a8redis%e5%ae%8c%e5%96%84%e5%af%86%e7%a2%bc%e8%a8%ad%e7%bd%ae%e4%bf%9d%e9%9a%9c%e5%ae%89%e5%85%a8%ef%bc%88redis%e9%9b%86%e6%88%90%e5%af%86%e7%a2%bc%e8%a8%ad%e7%bd%ae%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/185102\/","title":{"rendered":"\u4f7f\u7528Redis\u5b8c\u5584\u5bc6\u78bc\u8a2d\u7f6e\u4fdd\u969c\u5b89\u5168\uff08redis\u96c6\u6210\u5bc6\u78bc\u8a2d\u7f6e\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e5%ae%8c%e5%96%84%e5%af%86%e7%a2%bc%e8%a8%ad%e7%bd%ae%e4%bf%9d%e9%9a%9c%e5%ae%89%e5%85%a8%ef%bc%88redis%e9%9b%86%e6%88%90%e5%af%86%e7%a2%bc%e8%a8%ad%e7%bd%ae%ef%bc%89-LYjmGsvTFa\">\u4f7f\u7528Redis\u5b8c\u5584\u5bc6\u78bc\u8a2d\u7f6e\u4fdd\u969c\u5b89\u5168\uff08redis\u96c6\u6210\u5bc6\u78bc\u8a2d\u7f6e\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u4f4d\u6642\u4ee3\uff0c\u6578\u64da\u5b89\u5168\u5df2\u6210\u70ba\u4f01\u696d\u548c\u500b\u4eba\u7528\u6236\u6700\u95dc\u6ce8\u7684\u8b70\u984c\u4e4b\u4e00\u3002\u96a8\u8457\u7db2\u7d61\u653b\u64ca\u7684\u65e5\u76ca\u589e\u591a\uff0c\u5982\u4f55\u6709\u6548\u5730\u4fdd\u8b77\u7528\u6236\u7684\u654f\u611f\u4fe1\u606f\uff0c\u7279\u5225\u662f\u5bc6\u78bc\uff0c\u6210\u70ba\u4e86\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u7684\u91cd\u8981\u4efb\u52d9\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u4e0d\u50c5\u80fd\u5920\u63d0\u4f9b\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\uff0c\u9084\u80fd\u901a\u904e\u96c6\u6210\u5bc6\u78bc\u8a2d\u7f6e\u4f86\u589e\u5f37\u5b89\u5168\u6027\u3002<\/p>\n<h2 id=\"redis%e7%b0%a1%e4%bb%8b-LYjmGsvTFa\">Redis\u7c21\u4ecb<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\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\u5206\u6790\u7b49\u5834\u666f\u3002<\/p>\n<h2 id=\"%e5%af%86%e7%a2%bc%e5%ae%89%e5%85%a8%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7-LYjmGsvTFa\">\u5bc6\u78bc\u5b89\u5168\u7684\u91cd\u8981\u6027<\/h2>\n<p>\u5bc6\u78bc\u662f\u7528\u6236\u8eab\u4efd\u9a57\u8b49\u7684\u7b2c\u4e00\u9053\u9632\u7dda\u3002\u96a8\u8457\u7db2\u7d61\u653b\u64ca\u624b\u6bb5\u7684\u591a\u6a23\u5316\uff0c\u50b3\u7d71\u7684\u5bc6\u78bc\u5b58\u5132\u65b9\u5f0f\uff08\u5982\u660e\u6587\u5b58\u5132\uff09\u5df2\u7d93\u7121\u6cd5\u6eff\u8db3\u5b89\u5168\u9700\u6c42\u3002\u6839\u64da\u7814\u7a76\uff0c\u8d85\u904e80%\u7684\u6578\u64da\u6d29\u9732\u4e8b\u4ef6\u8207\u5bc6\u78bc\u5b89\u5168\u6709\u95dc\u3002\u56e0\u6b64\uff0c\u63a1\u7528\u5b89\u5168\u7684\u5bc6\u78bc\u5b58\u5132\u548c\u7ba1\u7406\u7b56\u7565\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2 id=\"redis%e9%9b%86%e6%88%90%e5%af%86%e7%a2%bc%e8%a8%ad%e7%bd%ae%e7%9a%84%e5%84%aa%e5%8b%a2-LYjmGsvTFa\">Redis\u96c6\u6210\u5bc6\u78bc\u8a2d\u7f6e\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528Redis\u4f86\u7ba1\u7406\u5bc6\u78bc\u8a2d\u7f6e\u6709\u4ee5\u4e0b\u5e7e\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\u80fd\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5176\u80fd\u5920\u5feb\u901f\u8b80\u53d6\u548c\u5beb\u5165\u6578\u64da\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u983b\u7e41\u9a57\u8b49\u7528\u6236\u8eab\u4efd\u7684\u61c9\u7528\u5c24\u70ba\u91cd\u8981\u3002<\/li>\n<li><strong>\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9748\u6d3b\u8a2d\u8a08\u5bc6\u78bc\u5b58\u5132\u65b9\u6848\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u901a\u904e\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u548c\u52a0\u5bc6\u6280\u8853\uff0cRedis\u53ef\u4ee5\u6709\u6548\u5730\u4fdd\u8b77\u7528\u6236\u5bc6\u78bc\uff0c\u9632\u6b62\u672a\u7d93\u6388\u6b0a\u7684\u8a2a\u554f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8redis%e4%b8%ad%e8%a8%ad%e7%bd%ae%e5%ae%89%e5%85%a8%e7%9a%84%e5%af%86%e7%a2%bc%e5%ad%98%e5%84%b2-LYjmGsvTFa\">\u5982\u4f55\u5728Redis\u4e2d\u8a2d\u7f6e\u5b89\u5168\u7684\u5bc6\u78bc\u5b58\u5132<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u9032\u884c\u5bc6\u78bc\u8a2d\u7f6e\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%af%86%e7%a2%bc%e5%93%88%e5%b8%8c%e5%8c%96-LYjmGsvTFa\">1. \u5bc6\u78bc\u54c8\u5e0c\u5316<\/h3>\n<p>\u5728\u5c07\u5bc6\u78bc\u5b58\u5132\u5230Redis\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5c0d\u5176\u9032\u884c\u54c8\u5e0c\u8655\u7406\u3002\u9019\u53ef\u4ee5\u4f7f\u7528\u5982bcrypt\u3001argon2\u7b49\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Node.js\u548cbcrypt\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nconst bcrypt = require('bcrypt');\nconst saltRounds = 10;\n\nconst password = 'user_password';\nbcrypt.hash(password, saltRounds, function(err, hash) {\n    \/\/ \u5c07hash\u5b58\u5132\u5230Redis\n});\n<\/code><\/pre>\n<h3 id=\"2-%e5%ad%98%e5%84%b2%e5%93%88%e5%b8%8c%e5%af%86%e7%a2%bc-LYjmGsvTFa\">2. \u5b58\u5132\u54c8\u5e0c\u5bc6\u78bc<\/h3>\n<p>\u5c07\u54c8\u5e0c\u5f8c\u7684\u5bc6\u78bc\u5b58\u5132\u5230Redis\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>\nSET user:1:password hash_value\n<\/code><\/pre>\n<h3 id=\"3-%e9%a9%97%e8%ad%89%e5%af%86%e7%a2%bc-LYjmGsvTFa\">3. \u9a57\u8b49\u5bc6\u78bc<\/h3>\n<p>\u7576\u7528\u6236\u767b\u9304\u6642\uff0c\u9700\u5c07\u8f38\u5165\u7684\u5bc6\u78bc\u8207\u5b58\u5132\u5728Redis\u4e2d\u7684\u54c8\u5e0c\u5bc6\u78bc\u9032\u884c\u6bd4\u5c0d\uff1a<\/p>\n<pre><code>\nbcrypt.compare(inputPassword, storedHash, function(err, result) {\n    if(result) {\n        \/\/ \u5bc6\u78bc\u6b63\u78ba\n    } else {\n        \/\/ \u5bc6\u78bc\u932f\u8aa4\n    }\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-LYjmGsvTFa\">\u7d50\u8ad6<\/h2>\n<p>\u5728\u7576\u524d\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u5bc6\u78bc\u5b89\u5168\u4e0d\u5bb9\u5ffd\u8996\u3002\u901a\u904e\u4f7f\u7528Redis\u96c6\u6210\u5bc6\u78bc\u8a2d\u7f6e\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u7cfb\u7d71\u7684\u5b89\u5168\u6027\uff0c\u4fdd\u8b77\u7528\u6236\u7684\u654f\u611f\u4fe1\u606f\u3002\u9078\u64c7\u5408\u9069\u7684\u54c8\u5e0c\u7b97\u6cd5\u548c\u5b58\u5132\u7b56\u7565\uff0c\u5c07\u6709\u52a9\u65bc\u6e1b\u5c11\u6578\u64da\u6d29\u9732\u7684\u98a8\u96aa\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc\u65bc\u5982\u4f55\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u63d0\u5347\u60a8\u7684\u61c9\u7528\u5b89\u5168\u6027\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u96c6\u6210\u5bc6\u78bc\u8a2d\u7f6e\uff0c\u63d0\u5347\u7cfb\u7d71\u5b89\u5168\u6027\uff0c\u78ba\u4fdd\u7528\u6236\u6578\u64da\u4fdd\u8b77\uff0c\u9632\u6b62\u672a\u6388\u6b0a\u8a2a\u554f\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-185102","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185102","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=185102"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185102\/revisions"}],"predecessor-version":[{"id":185103,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185102\/revisions\/185103"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=185102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=185102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=185102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}