{"id":189104,"date":"2024-11-12T04:40:21","date_gmt":"2024-11-11T20:40:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/189104\/"},"modified":"2024-11-12T04:40:22","modified_gmt":"2024-11-11T20:40:22","slug":"%e6%8f%90%e5%8d%87%e7%99%bb%e9%8c%84%e6%95%88%e7%8e%87redis%e5%ad%98%e5%84%b2%e7%94%a8%e6%88%b6%e4%bf%a1%e6%81%af%ef%bc%88%e7%99%bb%e9%8c%84%e6%99%82%e7%94%a8%e6%88%b6%e4%bf%a1%e6%81%af%e5%ad%98","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/189104\/","title":{"rendered":"\u63d0\u5347\u767b\u9304\u6548\u7387Redis\u5b58\u5132\u7528\u6236\u4fe1\u606f\uff08\u767b\u9304\u6642\u7528\u6236\u4fe1\u606f\u5b58\u653e\u5230redis\uff09"},"content":{"rendered":"<h1 id=\"%e6%8f%90%e5%8d%87%e7%99%bb%e9%8c%84%e6%95%88%e7%8e%87%ef%bc%9aredis%e5%ad%98%e5%84%b2%e7%94%a8%e6%88%b6%e4%bf%a1%e6%81%af-hBOxflKpDF\">\u63d0\u5347\u767b\u9304\u6548\u7387\uff1aRedis\u5b58\u5132\u7528\u6236\u4fe1\u606f<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u767b\u9304\u6548\u7387\u5c0d\u65bc\u7528\u6236\u9ad4\u9a57\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u5feb\u901f\u3001\u6709\u6548\u5730\u7ba1\u7406\u7528\u6236\u4fe1\u606f\u6210\u70ba\u4e86\u8a31\u591a\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u9762\u81e8\u7684\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u4e00\u7a2e\u7406\u60f3\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u5b58\u5132\u548c\u7ba1\u7406\u7528\u6236\u767b\u9304\u4fe1\u606f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u63d0\u5347\u767b\u9304\u6548\u7387\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-hBOxflKpDF\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\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\u5728\u5167\u5b58\u4e2d\u64cd\u4f5c\u6578\u64da\u7684\u7279\u6027\uff0cRedis\u80fd\u5920\u63d0\u4f9b\u6975\u5feb\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u5b58\u5132\u7528\u6236\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%e5%ad%98%e5%84%b2%e7%94%a8%e6%88%b6%e4%bf%a1%e6%81%af%ef%bc%9f-hBOxflKpDF\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f86\u5b58\u5132\u7528\u6236\u4fe1\u606f\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>Redis\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u767b\u9304\u7684\u97ff\u61c9\u6642\u9593\u3002<\/li>\n<li><strong>\u53ef\u64f4\u5c55\u6027\uff1a<\/strong>Redis\u652f\u6301\u5206\u4f48\u5f0f\u67b6\u69cb\uff0c\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\u4ee5\u61c9\u5c0d\u4e0d\u65b7\u589e\u9577\u7684\u7528\u6236\u6578\u91cf\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\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\u6578\u64da\u7d50\u69cb\u4f86\u5b58\u5132\u7528\u6236\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e5%ad%98%e5%84%b2%e7%94%a8%e6%88%b6%e4%bf%a1%e6%81%af-hBOxflKpDF\">\u5982\u4f55\u4f7f\u7528Redis\u5b58\u5132\u7528\u6236\u4fe1\u606f<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u5b58\u5132\u7528\u6236\u4fe1\u606f\u6642\uff0c\u901a\u5e38\u6703\u5c07\u7528\u6236\u7684\u767b\u9304\u4fe1\u606f\uff08\u5982\u7528\u6236\u540d\u3001\u5bc6\u78bc\u54c8\u5e0c\u3001\u767b\u9304\u6642\u9593\u7b49\uff09\u5b58\u5132\u70ba\u9375\u503c\u5c0d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Node.js\u74b0\u5883\u4e2d\u4f7f\u7528Redis\u4f86\u5b58\u5132\u548c\u6aa2\u7d22\u7528\u6236\u4fe1\u606f\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dredis%e5%92%8c%e7%9b%b8%e9%97%9c%e5%ba%ab-hBOxflKpDF\">\u5b89\u88ddRedis\u548c\u76f8\u95dc\u5eab<\/h3>\n<pre><code>npm install redis<\/code><\/pre>\n<h3 id=\"%e9%80%a3%e6%8e%a5%e5%88%b0redis-hBOxflKpDF\">\u9023\u63a5\u5230Redis<\/h3>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\nclient.on('error', (err) =&gt; {\n    console.log('Error ' + err);\n});<\/code><\/pre>\n<h3 id=\"%e5%ad%98%e5%84%b2%e7%94%a8%e6%88%b6%e4%bf%a1%e6%81%af-hBOxflKpDF\">\u5b58\u5132\u7528\u6236\u4fe1\u606f<\/h3>\n<pre><code>function storeUserInfo(username, passwordHash) {\n    const userKey = `user:${username}`;\n    client.hmset(userKey, 'password', passwordHash, 'lastLogin', new Date().toISOString(), (err, res) =&gt; {\n        if (err) {\n            console.error('Error storing user info:', err);\n        } else {\n            console.log('User info stored:', res);\n        }\n    });\n}<\/code><\/pre>\n<h3 id=\"%e6%aa%a2%e7%b4%a2%e7%94%a8%e6%88%b6%e4%bf%a1%e6%81%af-hBOxflKpDF\">\u6aa2\u7d22\u7528\u6236\u4fe1\u606f<\/h3>\n<pre><code>function getUserInfo(username) {\n    const userKey = `user:${username}`;\n    client.hgetall(userKey, (err, userInfo) =&gt; {\n        if (err) {\n            console.error('Error retrieving user info:', err);\n        } else {\n            console.log('User info retrieved:', userInfo);\n        }\n    });\n}<\/code><\/pre>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-hBOxflKpDF\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u5b58\u5132\u7528\u6236\u4fe1\u606f\u6642\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5be6\u8e10\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u5b89\u5168\u7684\u5bc6\u78bc\u54c8\u5e0c\uff1a<\/strong>\u5728\u5b58\u5132\u7528\u6236\u5bc6\u78bc\u6642\uff0c\u61c9\u4f7f\u7528\u5b89\u5168\u7684\u54c8\u5e0c\u7b97\u6cd5\uff08\u5982bcrypt\uff09\u4f86\u4fdd\u8b77\u7528\u6236\u4fe1\u606f\u3002<\/li>\n<li><strong>\u8a2d\u7f6e\u904e\u671f\u6642\u9593\uff1a<\/strong>\u5c0d\u65bc\u4e0d\u6d3b\u8e8d\u7684\u7528\u6236\u4fe1\u606f\uff0c\u53ef\u4ee5\u8a2d\u7f6e\u904e\u671f\u6642\u9593\uff0c\u4ee5\u91cb\u653e\u5167\u5b58\u8cc7\u6e90\u3002<\/li>\n<li><strong>\u5b9a\u671f\u5099\u4efd\uff1a<\/strong>\u96d6\u7136Redis\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b9a\u671f\u5099\u4efd\u6578\u64da\u4ecd\u7136\u662f\u5fc5\u8981\u7684\uff0c\u4ee5\u9632\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-hBOxflKpDF\">\u7d50\u8ad6<\/h2>\n<p>\u5229\u7528Redis\u4f86\u5b58\u5132\u7528\u6236\u4fe1\u606f\u4e0d\u50c5\u80fd\u5920\u63d0\u5347\u767b\u9304\u6548\u7387\uff0c\u9084\u80fd\u5920\u63d0\u4f9b\u9748\u6d3b\u7684\u6578\u64da\u7ba1\u7406\u65b9\u6848\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u65bd\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u986f\u8457\u6539\u5584\u7528\u6236\u9ad4\u9a57\uff0c\u4e26\u70ba\u672a\u4f86\u7684\u64f4\u5c55\u6253\u4e0b\u826f\u597d\u7684\u57fa\u790e\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u9078\u64c7Redis\u4f5c\u70ba\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u7121\u7591\u662f\u4e00\u500b\u660e\u667a\u7684\u9078\u64c7\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c\u5176\u4ed6\u670d\u52d9\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347\u767b\u9304\u6548\u7387\uff0c\u5c07\u7528\u6236\u4fe1\u606f\u5b58\u5132\u65bcRedis\uff0c\u5be6\u73fe\u5feb\u901f\u767b\u9304\uff0c\u512a\u5316\u7528\u6236\u9ad4\u9a57\uff0c\u6e1b\u5c11\u5ef6\u9072\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-189104","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189104","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=189104"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189104\/revisions"}],"predecessor-version":[{"id":189105,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189104\/revisions\/189105"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=189104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=189104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=189104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}