{"id":202062,"date":"2025-05-10T12:18:02","date_gmt":"2025-05-10T04:18:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202062\/"},"modified":"2025-05-10T12:18:02","modified_gmt":"2025-05-10T04:18:02","slug":"redis%e4%b8%8enode-js%e7%9a%84%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e6%93%8d%e4%bd%9c%ef%bc%9a%e5%a6%82%e4%bd%95%e9%ab%98%e6%95%88%e5%9c%b0%e5%ad%98%e5%82%a8%e5%92%8c%e6%9f%a5%e8%af%a2%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202062\/","title":{"rendered":"Redis\u4e0eNode.js\u7684\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\uff1a\u5982\u4f55\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u6570\u636e"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e0eNode.js\u7684\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\uff1a\u5982\u4f55\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u6570\u636e<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aRedis\u4e0eNode.js\u7684\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\uff1a\u5982\u4f55\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u6570\u636e\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p>Redis\u4e0eNode.js\u7684\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\uff1a\u5982\u4f55\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u6570\u636e<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728\u73b0\u4ee3Web\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4e2d\uff0c\u9ad8\u6548\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u6570\u636e\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002Redis\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6027\u80fd\u7684\u5185\u5b58\u6570\u636e\u5e93\uff0c\u4e0eNode.js\u65e0\u7f1d\u96c6\u6210\uff0c\u6210\u4e3a\u4e86\u8bb8\u591a\u5f00\u53d1\u8005\u7684\u9996\u9009\u5de5\u5177\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Redis\u548cNode.js\u8fdb\u884c\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u5b58\u50a8\u548c\u67e5\u8be2\u3002<\/p>\n<p>\u4e00\u3001\u8fde\u63a5Redis\uff1a<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Redis\u5e76\u542f\u52a8\u5b83\u7684\u670d\u52a1\u3002\u7136\u540e\uff0c\u5728Node.js\u4e2d\u4f7f\u7528redis\u6a21\u5757\u6765\u8fde\u63a5\u5230Redis\u670d\u52a1\u5668\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>const redis = require('redis');\nconst client = redis.createClient();\n\nclient.on('connect', function() {\n    console.log('Redis\u8fde\u63a5\u6210\u529f\uff01');\n});<\/pre>\n<p>\u4e8c\u3001String\u7c7b\u578b\u64cd\u4f5c\uff1a<br \/>Redis\u4e2d\u7684String\u7c7b\u578b\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u5404\u79cd\u7c7b\u578b\u7684\u503c\uff0c\u5982\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001JSON\u5bf9\u8c61\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u8bbe\u7f6e\u548c\u83b7\u53d6\u503c\uff1a<\/li>\n<\/ol>\n<pre>client.set('name', 'John', function(err, reply) {\n    console.log(reply); \/\/ OK\n});\n\nclient.get('name', function(err, reply) {\n    console.log(reply); \/\/ John\n});<\/pre>\n<ol>\n<li>\u589e\u52a0\u548c\u51cf\u5c11\u503c\uff1a<\/li>\n<\/ol>\n<pre>client.set('count', 10, function(err, reply) {\n    console.log(reply); \/\/ OK\n});\n\nclient.incr('count', function(err, reply) {\n    console.log(reply); \/\/ 11\n});\n\nclient.decr('count', function(err, reply) {\n    console.log(reply); \/\/ 10\n});<\/pre>\n<ol>\n<li>\u8bbe\u7f6e\u503c\u7684\u8fc7\u671f\u65f6\u95f4\uff1a<\/li>\n<\/ol>\n<pre>client.set('token', 'abc123');\n\/\/ \u8bbe\u7f6etoken\u7684\u8fc7\u671f\u65f6\u95f4\u4e3a10\u79d2\nclient.expire('token', 10);\n\n\/\/ \u83b7\u53d6token\u7684\u5269\u4f59\u8fc7\u671f\u65f6\u95f4\nclient.ttl('token', function(err, reply) {\n    console.log(reply); \/\/ 10 (\u5355\u4f4d\u4e3a\u79d2)\n});<\/pre>\n<p>\u4e09\u3001Hash\u7c7b\u578b\u64cd\u4f5c\uff1a<br \/>Redis\u4e2d\u7684Hash\u7c7b\u578b\u7c7b\u4f3c\u4e8eJavaScript\u4e2d\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u591a\u4e2a\u5b57\u6bb5\u548c\u5bf9\u5e94\u7684\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u7684Hash\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u8bbe\u7f6e\u548c\u83b7\u53d6\u5b57\u6bb5\uff1a<\/li>\n<\/ol>\n<pre>client.hset('user:1', 'name', 'John');\nclient.hset('user:1', 'age', 25);\n\nclient.hget('user:1', 'name', function(err, reply) {\n    console.log(reply); \/\/ John\n});<\/pre>\n<ol>\n<li>\u83b7\u53d6\u6240\u6709\u5b57\u6bb5\u548c\u503c\uff1a<\/li>\n<\/ol>\n<pre>client.hgetall('user:1', function(err, reply) {\n    console.log(reply); \/\/ { name: 'John', age: '25' }\n});<\/pre>\n<ol>\n<li>\u5220\u9664\u5b57\u6bb5\uff1a<\/li>\n<\/ol>\n<pre>client.hdel('user:1', 'age');<\/pre>\n<p>\u56db\u3001List\u7c7b\u578b\u64cd\u4f5c\uff1a<br \/>Redis\u7684List\u7c7b\u578b\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u961f\u5217\u3001\u5806\u6808\u7b49\u6570\u636e\u7ed3\u6784\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u7684List\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u6dfb\u52a0\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre>client.lpush('queue', 'item1');\nclient.lpush('queue', 'item2');<\/pre>\n<ol>\n<li>\u83b7\u53d6\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre>client.lrange('queue', 0, -1, function(err, reply) {\n    console.log(reply); \/\/ [ 'item2', 'item1' ]\n});<\/pre>\n<ol>\n<li>\u5f39\u51fa\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre>client.rpop('queue', function(err, reply) {\n    console.log(reply); \/\/ item1\n});<\/pre>\n<p>\u4e94\u3001Set\u7c7b\u578b\u64cd\u4f5c\uff1a<br \/>Redis\u7684Set\u7c7b\u578b\u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u5b57\u7b26\u4e32\u96c6\u5408\uff0c\u53ef\u4ee5\u7528\u6765\u5b58\u50a8\u4e00\u7ec4\u552f\u4e00\u7684\u503c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u7528\u7684Set\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\u6dfb\u52a0\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre>client.sadd('tags', 'tag1');\nclient.sadd('tags', 'tag2');<\/pre>\n<ol>\n<li>\u83b7\u53d6\u6240\u6709\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre>client.smembers('tags', function(err, reply) {\n    console.log(reply); \/\/ [ 'tag1', 'tag2' ]\n});<\/pre>\n<ol>\n<li>\u5220\u9664\u5143\u7d20\uff1a<\/li>\n<\/ol>\n<pre>client.srem('tags', 'tag2');<\/pre>\n<p>\u516d\u3001\u603b\u7ed3\uff1a<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86Redis\u548cNode.js\u7684\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5b58\u50a8\u548c\u67e5\u8be2\u6570\u636e\uff0c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u5229\u7528Redis\u548cNode.js\u5f00\u53d1\u9ad8\u6548\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\uff08\u603b\u5b57\u6570\uff1a623\u5b57\uff09<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<ol>\n<li>Redis\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/redis.io\/documentation<\/li>\n<li>Node.js Redis\u6a21\u5757\u6587\u6863\uff1ahttps:\/\/github.com\/NodeRedis\/node_redis<\/li>\n<\/ol>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u4f7f\u7528Redis\u548cPHP\u5b9e\u73b0\u5206\u5e03\u5f0f\u4efb\u52a1\u961f\u5217\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u4f7f\u7528Python\u548cRedis\u6784\u5efa\u65e5\u5fd7\u5206\u6790\u7cfb\u7edf\uff1a\u5982\u4f55\u5b9e\u65f6\u76d1\u63a7\u5e94\u7528\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e0eNode.js\u7684\u6570\u636e&#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-202062","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202062","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=202062"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202062\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}