{"id":184290,"date":"2024-11-10T11:59:19","date_gmt":"2024-11-10T03:59:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184290\/"},"modified":"2024-11-10T11:59:20","modified_gmt":"2024-11-10T03:59:20","slug":"%e4%bd%bf%e7%94%a8redis%e7%ae%a1%e7%90%86%e7%94%a8%e6%88%b6%e5%ae%89%e5%85%a8%e5%af%86%e7%a2%bc%ef%bc%88redis%e8%a8%aa%e5%95%8f%e7%94%a8%e6%88%b6%e5%af%86%e7%a2%bc%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184290\/","title":{"rendered":"\u4f7f\u7528Redis\u7ba1\u7406\u7528\u6236\u5b89\u5168\u5bc6\u78bc\uff08redis\u8a2a\u554f\u7528\u6236\u5bc6\u78bc\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e7%ae%a1%e7%90%86%e7%94%a8%e6%88%b6%e5%ae%89%e5%85%a8%e5%af%86%e7%a2%bc%ef%bc%88redis%e8%a8%aa%e5%95%8f%e7%94%a8%e6%88%b6%e5%af%86%e7%a2%bc%ef%bc%89-GOorkJmbFq\">\u4f7f\u7528Redis\u7ba1\u7406\u7528\u6236\u5b89\u5168\u5bc6\u78bc\uff08redis\u8a2a\u554f\u7528\u6236\u5bc6\u78bc\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u5b57\u6642\u4ee3\uff0c\u5b89\u5168\u6027\u662f\u6bcf\u500b\u61c9\u7528\u7a0b\u5e8f\u548c\u7cfb\u7d71\u7684\u9996\u8981\u8003\u91cf\u3002\u7528\u6236\u5bc6\u78bc\u7684\u7ba1\u7406\u5c24\u70ba\u91cd\u8981\uff0c\u56e0\u70ba\u5b83\u5011\u662f\u4fdd\u8b77\u7528\u6236\u6578\u64da\u548c\u96b1\u79c1\u7684\u7b2c\u4e00\u9053\u9632\u7dda\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\u9069\u5408\u7528\u65bc\u7ba1\u7406\u7528\u6236\u5b89\u5168\u5bc6\u78bc\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528Redis\u4f86\u5b89\u5168\u5730\u7ba1\u7406\u7528\u6236\u5bc6\u78bc\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-GOorkJmbFq\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u7ba1\u7406\u7528\u6236\u5bc6\u78bc\u7684\u4e00\u4e9b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u80fd\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\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u5feb\u901f\u9a57\u8b49\u7528\u6236\u8eab\u4efd\u7684\u61c9\u7528\u7a0b\u5e8f\u81f3\u95dc\u91cd\u8981\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6301\u4e45\u5316\u6a5f\u5236\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u5f0f\u4f86\u4fdd\u5b58\u6578\u64da\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u7684API\u7c21\u55ae\u660e\u77ad\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u4e0a\u624b\uff0c\u5feb\u901f\u5be6\u73fe\u7528\u6236\u5bc6\u78bc\u7684\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%ae%89%e5%85%a8%e5%ad%98%e5%84%b2%e7%94%a8%e6%88%b6%e5%af%86%e7%a2%bc-GOorkJmbFq\">\u5b89\u5168\u5b58\u5132\u7528\u6236\u5bc6\u78bc<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u5b58\u5132\u7528\u6236\u5bc6\u78bc\u6642\uff0c\u6700\u91cd\u8981\u7684\u662f\u78ba\u4fdd\u5bc6\u78bc\u7684\u5b89\u5168\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<h3 id=\"1-%e5%af%86%e7%a2%bc%e5%93%88%e5%b8%8c-GOorkJmbFq\">1. \u5bc6\u78bc\u54c8\u5e0c<\/h3>\n<p>\u76f4\u63a5\u5b58\u5132\u7528\u6236\u7684\u660e\u6587\u5bc6\u78bc\u662f\u975e\u5e38\u5371\u96aa\u7684\u3002\u61c9\u8a72\u4f7f\u7528\u54c8\u5e0c\u7b97\u6cd5\u4f86\u52a0\u5bc6\u5bc6\u78bc\u3002\u5e38\u7528\u7684\u54c8\u5e0c\u7b97\u6cd5\u5305\u62ecbcrypt\u3001Argon2\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528bcrypt\u9032\u884c\u5bc6\u78bc\u54c8\u5e0c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const bcrypt = require('bcrypt');\nconst saltRounds = 10;\n\nfunction hashPassword(password) {\n    return bcrypt.hash(password, saltRounds);\n}\n<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8redis%e5%ad%98%e5%84%b2%e5%93%88%e5%b8%8c%e5%af%86%e7%a2%bc-GOorkJmbFq\">2. \u4f7f\u7528Redis\u5b58\u5132\u54c8\u5e0c\u5bc6\u78bc<\/h3>\n<p>\u4e00\u65e6\u5bc6\u78bc\u88ab\u54c8\u5e0c\uff0c\u53ef\u4ee5\u5c07\u5176\u5b58\u5132\u5728Redis\u4e2d\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5c07\u54c8\u5e0c\u5bc6\u78bc\u5b58\u5132\u5230Redis\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\nasync function storePassword(username, password) {\n    const hashedPassword = await hashPassword(password);\n    client.set(username, hashedPassword, redis.print);\n}\n<\/code><\/pre>\n<h3 id=\"3-%e9%a9%97%e8%ad%89%e7%94%a8%e6%88%b6%e5%af%86%e7%a2%bc-GOorkJmbFq\">3. \u9a57\u8b49\u7528\u6236\u5bc6\u78bc<\/h3>\n<p>\u7576\u7528\u6236\u767b\u9304\u6642\uff0c\u9700\u8981\u9a57\u8b49\u5176\u8f38\u5165\u7684\u5bc6\u78bc\u3002\u9019\u53ef\u4ee5\u901a\u904e\u5f9eRedis\u4e2d\u6aa2\u7d22\u54c8\u5e0c\u5bc6\u78bc\u4e26\u8207\u7528\u6236\u8f38\u5165\u7684\u5bc6\u78bc\u9032\u884c\u6bd4\u8f03\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code>async function verifyPassword(username, inputPassword) {\n    client.get(username, async (err, hashedPassword) =&gt; {\n        if (err) throw err;\n        const match = await bcrypt.compare(inputPassword, hashedPassword);\n        if (match) {\n            console.log('\u5bc6\u78bc\u6b63\u78ba\uff01');\n        } else {\n            console.log('\u5bc6\u78bc\u932f\u8aa4\uff01');\n        }\n    });\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-GOorkJmbFq\">\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528Redis\u4f86\u7ba1\u7406\u7528\u6236\u5b89\u5168\u5bc6\u78bc\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u5b89\u5168\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u54c8\u5e0c\u5bc6\u78bc\u4e26\u5c07\u5176\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\u63a1\u7528\u9019\u4e9b\u6700\u4f73\u5be6\u8e10\u5c07\u6709\u52a9\u65bc\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u548c\u5b89\u5168\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u5b89\u5168\u7ba1\u7406\u7528\u6236\u5bc6\u78bc\uff0c\u78ba\u4fdd\u6578\u64da\u5b89\u5168\u8207\u9ad8\u6548\u8a2a\u554f\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u7528\u6236\u9ad4\u9a57\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-184290","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184290","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=184290"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184290\/revisions"}],"predecessor-version":[{"id":184291,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184290\/revisions\/184291"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}