{"id":202058,"date":"2025-05-10T09:51:51","date_gmt":"2025-05-10T01:51:51","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202058\/"},"modified":"2025-05-10T09:51:51","modified_gmt":"2025-05-10T01:51:51","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8redis%e5%92%8cnode-js%e5%ae%9e%e7%8e%b0%e5%88%86%e5%b8%83%e5%bc%8f%e5%ad%98%e5%82%a8%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202058\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Redis\u548cNode.js\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8\u529f\u80fd"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5229\u7528Redis\u548cNode.js\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8\u529f\u80fd<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300a\u5982\u4f55\u5229\u7528Redis\u548cNode.js\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8\u529f\u80fd\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6570\u636e\u5e93\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7golang\u5b66\u4e60\u7f51\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p>\u5982\u4f55\u5229\u7528Redis\u548cNode.js\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8\u529f\u80fd<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728\u73b0\u4ee3\u5927\u6570\u636e\u65f6\u4ee3\uff0c\u5206\u5e03\u5f0f\u5b58\u50a8\u7cfb\u7edf\u6210\u4e3a\u89e3\u51b3\u6d77\u91cf\u6570\u636e\u5b58\u50a8\u548c\u5feb\u901f\u8bbf\u95ee\u7684\u5173\u952e\u6280\u672f\u4e4b\u4e00\u3002Redis\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5185\u5b58\u6570\u636e\u5e93\uff0c\u800cNode.js\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u670d\u52a1\u5668\u7aefJavaScript\u8fd0\u884c\u65f6\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Redis\u548cNode.js\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001Redis\u6982\u8ff0<br \/>Redis\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5f00\u6e90\u5185\u5b58\u6570\u636e\u5e93\u7cfb\u7edf\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\u548c\u7075\u6d3b\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u5f0f\u3002\u901a\u8fc7\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0cRedis\u80fd\u591f\u5feb\u901f\u5730\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u7684\u573a\u666f\u3002Redis\u652f\u6301\u7684\u6570\u636e\u7ed3\u6784\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u8868\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\u7b49\u3002<\/p>\n<p>\u4e8c\u3001Node.js\u4e0eRedis\u7684\u8fde\u63a5<br \/>Node.js\u63d0\u4f9b\u4e86\u8bb8\u591aRedis\u5ba2\u6237\u7aef\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0eRedis\u5efa\u7acb\u8fde\u63a5\u5e76\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528node-redis\u8fd9\u4e2a\u6d41\u884c\u7684Redis\u5ba2\u6237\u7aef\u5e93\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728Node.js\u9879\u76ee\u4e2d\u5b89\u88c5node-redis\uff0c\u53ef\u4ee5\u4f7f\u7528npm\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre>npm install redis<\/pre>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Node.js\u4e2d\u5f15\u5165\u5e76\u8fde\u63a5Redis\uff1a<\/p>\n<pre>const redis = require(\"redis\");\nconst client = redis.createClient();\n\nclient.on(\"error\", function (err) {\n    console.error(\"Redis\u8fde\u63a5\u9519\u8bef: \" + err);\n});<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7redis\u6a21\u5757\u521b\u5efa\u4e86\u4e00\u4e2aRedis\u5ba2\u6237\u7aef\uff0c\u5e76\u6dfb\u52a0\u4e86\u4e00\u4e2a\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002<\/p>\n<p>\u4e09\u3001\u5229\u7528Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8<br \/>\u5728\u5206\u5e03\u5f0f\u5b58\u50a8\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u5c06\u6570\u636e\u5206\u6563\u5b58\u50a8\u5728\u4e0d\u540c\u7684\u8282\u70b9\u4e0a\uff0c\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u7684\u6269\u5c55\u6027\u548c\u5bb9\u9519\u6027\u3002\u5229\u7528Redis\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u522b\u5b58\u50a8\u5728\u591a\u4e2aRedis\u5b9e\u4f8b\u4e0a\uff0c\u5e76\u901a\u8fc7\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u6765\u786e\u5b9a\u6570\u636e\u5e94\u8be5\u5b58\u50a8\u5728\u54ea\u4e2a\u8282\u70b9\u4e0a\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528Redis\u8fdb\u884c\u5206\u5e03\u5f0f\u5b58\u50a8\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>const redis = require(\"redis\");\nconst crypto = require(\"crypto\");\n\nconst nodes = [\"redis:\/\/127.0.0.1:6379\", \"redis:\/\/127.0.0.1:6380\", \"redis:\/\/127.0.0.1:6381\"]; \/\/ Redis\u8282\u70b9\nconst ring = []; \/\/ \u4e00\u81f4\u6027\u54c8\u5e0c\u73af\n\n\/\/ \u521d\u59cb\u5316\u4e00\u81f4\u6027\u54c8\u5e0c\u73af\nnodes.forEach(function (node) {\n    for (let i = 0; i &lt; 64; i++) {\n        const virtualNode = node + \"\/\" + i;\n        const hash = crypto.createHash(\"md5\").update(virtualNode).digest(\"hex\");\n        ring[hash] = node;\n    }\n});\n\n\/\/ \u6839\u636e\u952e\u83b7\u53d6\u5bf9\u5e94\u7684Redis\u8282\u70b9\nfunction getNode(key) {\n    const hash = crypto.createHash(\"md5\").update(key).digest(\"hex\");\n    let pos = 0;\n    for (let i = 0; i &lt; ring.length; i++) {\n        if (hash &lt;= ring[i]) {\n            pos = i;\n            break;\n        }\n    }\n    return ring[pos];\n}\n\n\/\/ \u67e5\u8be2\u952e\u7684\u503c\nfunction get(key) {\n    return new Promise(function (resolve, reject) {\n        const node = getNode(key);\n        const client = redis.createClient(node);\n        client.get(key, function (err, reply) {\n            if (err) reject(err);\n            else resolve(reply);\n            client.quit();\n        });\n    });\n}\n\n\/\/ \u8bbe\u7f6e\u952e\u7684\u503c\nfunction set(key, value) {\n    return new Promise(function (resolve, reject) {\n        const node = getNode(key);\n        const client = redis.createClient(node);\n        client.set(key, value, function (err, reply) {\n            if (err) reject(err);\n            else resolve(reply);\n            client.quit();\n        });\n    });\n}\n\n\/\/ \u793a\u4f8b\uff1a\u5b58\u50a8\u952e\u503c\u5bf9\nset(\"name\", \"Alice\").then(function (reply) {\n    console.log(\"\u8bbe\u7f6e\u6210\u529f\");\n}).catch(function (err) {\n    console.error(\"\u8bbe\u7f6e\u5931\u8d25: \" + err);\n});\n\n\/\/ \u793a\u4f8b\uff1a\u83b7\u53d6\u952e\u503c\u5bf9\nget(\"name\").then(function (reply) {\n    console.log(\"\u83b7\u53d6\u6210\u529f: \" + reply);\n}).catch(function (err) {\n    console.error(\"\u83b7\u53d6\u5931\u8d25: \" + err);\n});<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2aRedis\u8282\u70b9\u6570\u7ec4\u548c\u4e00\u4e2a\u4e00\u81f4\u6027\u54c8\u5e0c\u73af\u3002\u7136\u540e\uff0c\u901a\u8fc7\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u5c06\u8282\u70b9\u6620\u5c04\u5230\u54c8\u5e0c\u73af\u4e0a\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570get\u548cset\uff0c\u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u8282\u70b9\u7684\u952e\u503c\u5bf9\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fdb\u884c\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5b58\u50a8\u548c\u83b7\u53d6\u952e\u503c\u5bf9\u3002<\/p>\n<p>\u56db\u3001\u603b\u7ed3<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528Redis\u548cNode.js\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8\u529f\u80fd\u3002\u6211\u4eec\u901a\u8fc7Redis\u5ba2\u6237\u7aef\u5e93node-redis\u4e0eRedis\u5efa\u7acb\u8fde\u63a5\uff0c\u5e76\u5229\u7528\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\u5c06\u6570\u636e\u5206\u6563\u5b58\u50a8\u5728\u4e0d\u540c\u7684\u8282\u70b9\u4e0a\u3002\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u5b58\u50a8\u548c\u83b7\u53d6\u5206\u5e03\u5f0f\u5b58\u50a8\u7cfb\u7edf\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p>\u901a\u8fc7\u5229\u7528Redis\u548cNode.js\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8\uff0c\u6211\u4eec\u80fd\u591f\u6784\u5efa\u9ad8\u6027\u80fd\u548c\u53ef\u6269\u5c55\u7684\u5b58\u50a8\u7cfb\u7edf\uff0c\u4ee5\u6ee1\u8db3\u73b0\u4ee3\u5927\u6570\u636e\u65f6\u4ee3\u7684\u6311\u6218\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\uff0c\u6b22\u8fce\u5927\u5bb6\u63a2\u7d22\u66f4\u591a\u5173\u4e8eRedis\u548cNode.js\u7684\u5e94\u7528\u573a\u666f\u548c\u6280\u672f\u7ec6\u8282\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5728MySQL\u4e2d\u4f7f\u7528\u89e6\u53d1\u5668\u548c\u5b58\u50a8\u8fc7\u7a0b\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5229\u7528Redis\u548cJava\u5b9e\u73b0\u4e3b\u4ece\u590d\u5236\u529f\u80fd\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528Redis\u548cNode.j&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-202058","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202058","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202058"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202058\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}