{"id":62175,"date":"2024-10-13T16:48:17","date_gmt":"2024-10-13T08:48:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/62175\/"},"modified":"2024-10-13T16:48:17","modified_gmt":"2024-10-13T08:48:17","slug":"js%e5%9f%ba%e6%96%bcnodejs%e7%9a%84redis%e7%b7%a9%e5%ad%98%e5%af%a6%e7%8f%be%e6%a9%9f%e5%88%b6%ef%bc%88redis%e7%b7%a9%e5%ad%98node%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/62175\/","title":{"rendered":"Js\u57fa\u65bcNodejs\u7684Redis\u7de9\u5b58\u5be6\u73fe\u6a5f\u5236\uff08redis\u7de9\u5b58node\uff09"},"content":{"rendered":"<h1 id=\"js%e5%9f%ba%e6%96%bcnodejs%e7%9a%84redis%e7%b7%a9%e5%ad%98%e5%af%a6%e7%8f%be%e6%a9%9f%e5%88%b6%ef%bc%88redis%e7%b7%a9%e5%ad%98node%ef%bc%89-eFgKloNTdw\">Js\u57fa\u65bcNodejs\u7684Redis\u7de9\u5b58\u5be6\u73fe\u6a5f\u5236\uff08redis\u7de9\u5b58node\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u662f\u7528\u6236\u9ad4\u9a57\u7684\u95dc\u9375\u56e0\u7d20\u4e4b\u4e00\u3002\u70ba\u4e86\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\uff0c\u8a31\u591a\u958b\u767c\u8005\u9078\u64c7\u4f7f\u7528\u7de9\u5b58\u6280\u8853\uff0c\u5176\u4e2dRedis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u7de9\u5b58\u89e3\u6c7a\u65b9\u6848\uff0c\u8d8a\u4f86\u8d8a\u53d7\u5230\u9752\u775e\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728Node.js\u74b0\u5883\u4e2d\u5be6\u73feRedis\u7de9\u5b58\u6a5f\u5236\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-eFgKloNTdw\">\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\u5b83\u7684\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9ad8\u6548\u7684\u8b80\u5beb\u6027\u80fd\uff1aRedis\u80fd\u5920\u5728\u6beb\u79d2\u7d1a\u5225\u5167\u5b8c\u6210\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u3002<\/li>\n<li>\u6301\u4e45\u5316\uff1a\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u907f\u514d\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8redis%e4%bd%9c%e7%82%ba%e7%b7%a9%e5%ad%98%ef%bc%9f-eFgKloNTdw\">\u70ba\u4ec0\u9ebc\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\uff1f<\/h2>\n<p>\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\u7684\u539f\u56e0\u4e3b\u8981\u6709\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li><strong>\u6e1b\u5c11\u6578\u64da\u5eab\u8ca0\u64d4\uff1a<\/strong>\u901a\u904e\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u7de9\u5b58\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u97ff\u61c9\u901f\u5ea6\uff1a<\/strong>\u7531\u65bcRedis\u7684\u9ad8\u6548\u6027\u80fd\uff0c\u5f9e\u7de9\u5b58\u4e2d\u8b80\u53d6\u6578\u64da\u7684\u901f\u5ea6\u9060\u5feb\u65bc\u5f9e\u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u3002<\/li>\n<li><strong>\u9748\u6d3b\u7684\u904e\u671f\u7b56\u7565\uff1a<\/strong>Redis\u652f\u6301\u8a2d\u7f6e\u9375\u7684\u904e\u671f\u6642\u9593\uff0c\u80fd\u5920\u81ea\u52d5\u6e05\u7406\u904e\u671f\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%9c%a8node-js%e4%b8%ad%e4%bd%bf%e7%94%a8redis-eFgKloNTdw\">\u5728Node.js\u4e2d\u4f7f\u7528Redis<\/h2>\n<p>\u5728Node.js\u4e2d\u4f7f\u7528Redis\u975e\u5e38\u7c21\u55ae\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd\u76f8\u61c9\u7684npm\u5305\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd\uff1a<\/p>\n<pre><code>npm install redis<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u4ee3\u78bc\u793a\u4f8b\u4f86\u5be6\u73fe\u57fa\u672c\u7684Redis\u7de9\u5b58\u529f\u80fd\uff1a<\/p>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\n\/\/ \u9023\u63a5\u5230Redis\u670d\u52d9\u5668\nclient.on('connect', function() {\n    console.log('\u5df2\u9023\u63a5\u5230Redis');\n});\n\n\/\/ \u8a2d\u7f6e\u7de9\u5b58\nfunction setCache(key, value, expiration) {\n    client.setex(key, expiration, JSON.stringify(value), (err, reply) =&gt; {\n        if (err) {\n            console.error('\u8a2d\u7f6e\u7de9\u5b58\u5931\u6557:', err);\n        } else {\n            console.log('\u7de9\u5b58\u8a2d\u7f6e\u6210\u529f:', reply);\n        }\n    });\n}\n\n\/\/ \u7372\u53d6\u7de9\u5b58\nfunction getCache(key, callback) {\n    client.get(key, (err, reply) =&gt; {\n        if (err) {\n            console.error('\u7372\u53d6\u7de9\u5b58\u5931\u6557:', err);\n            callback(err, null);\n        } else {\n            if (reply) {\n                console.log('\u7de9\u5b58\u547d\u4e2d:', reply);\n                callback(null, JSON.parse(reply));\n            } else {\n                console.log('\u7de9\u5b58\u672a\u547d\u4e2d');\n                callback(null, null);\n            }\n        }\n    });\n}\n\n\/\/ \u4f7f\u7528\u793a\u4f8b\nconst key = 'user:1000';\nconst value = { name: 'John Doe', age: 30 };\nconst expiration = 3600; \/\/ 1\u5c0f\u6642\n\nsetCache(key, value, expiration);\ngetCache(key, (err, data) =&gt; {\n    if (data) {\n        console.log('\u7372\u53d6\u7684\u6578\u64da:', data);\n    }\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-eFgKloNTdw\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u7de9\u5b58\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8Node.js\u61c9\u7528\u7684\u6027\u80fd\u3002\u901a\u904e\u7c21\u55ae\u7684API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07Redis\u96c6\u6210\u5230\u81ea\u5df1\u7684\u61c9\u7528\u4e2d\uff0c\u5f9e\u800c\u5be6\u73fe\u6578\u64da\u7684\u5feb\u901f\u8b80\u53d6\u548c\u5beb\u5165\u3002\u7121\u8ad6\u662f\u6e1b\u5c11\u6578\u64da\u5eab\u8ca0\u64d4\u9084\u662f\u63d0\u9ad8\u97ff\u61c9\u901f\u5ea6\uff0cRedis\u90fd\u80fd\u5920\u63d0\u4f9b\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684Node.js\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u5be6\u73fe\u9ad8\u6027\u80fd\u7684\u61c9\u7528\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u57fa\u65bcNode.js\u7684Redis\u7de9\u5b58\u5be6\u73fe\u6a5f\u5236\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\uff0c\u512a\u5316\u6578\u64da\u5b58\u53d6\u6548\u7387\uff0c\u5be6\u73fe\u5feb\u901f\u97ff\u61c9\u3002<\/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-62175","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62175","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=62175"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62175\/revisions"}],"predecessor-version":[{"id":62176,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/62175\/revisions\/62176"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=62175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=62175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=62175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}