{"id":184666,"date":"2024-11-10T15:07:18","date_gmt":"2024-11-10T07:07:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184666\/"},"modified":"2024-11-10T15:07:19","modified_gmt":"2024-11-10T07:07:19","slug":"%e4%bd%bf%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e4%be%86%e6%94%b9%e5%96%84%e6%80%a7%e8%83%bd%ef%bc%88redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%80%8e%e9%ba%bc%e4%bd%bf%e7%94%a8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184666\/","title":{"rendered":"\u4f7f\u7528Redis\u9023\u63a5\u6c60\u4f86\u6539\u5584\u6027\u80fd\uff08redis\u9023\u63a5\u6c60\u600e\u9ebc\u4f7f\u7528\uff09"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e4%be%86%e6%94%b9%e5%96%84%e6%80%a7%e8%83%bd%ef%bc%88redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%80%8e%e9%ba%bc%e4%bd%bf%e7%94%a8%ef%bc%89-OTUQqoRJZT\">\u4f7f\u7528Redis\u9023\u63a5\u6c60\u4f86\u6539\u5584\u6027\u80fd\uff08redis\u9023\u63a5\u6c60\u600e\u9ebc\u4f7f\u7528\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6027\u80fd\u662f\u5f71\u97ff\u7528\u6236\u9ad4\u9a57\u7684\u95dc\u9375\u56e0\u7d20\u4e4b\u4e00\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u6301\u4e45\u5316\u7b49\u5834\u666f\u3002\u7136\u800c\uff0c\u96a8\u8457\u61c9\u7528\u7684\u64f4\u5c55\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406Redis\u9023\u63a5\u6210\u70ba\u4e86\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002\u9019\u6642\uff0c\u4f7f\u7528Redis\u9023\u63a5\u6c60\u4fbf\u6210\u70ba\u4e86\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-OTUQqoRJZT\">\u4ec0\u9ebc\u662fRedis\u9023\u63a5\u6c60\uff1f<\/h2>\n<p>Redis\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406Redis\u9023\u63a5\u7684\u6280\u8853\uff0c\u901a\u904e\u9810\u5148\u5efa\u7acb\u4e00\u5b9a\u6578\u91cf\u7684\u9023\u63a5\u4e26\u5c07\u5176\u4fdd\u5b58\u5728\u6c60\u4e2d\uff0c\u4f86\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u548c\u8cc7\u6e90\u5229\u7528\u7387\u3002\u9019\u6a23\uff0c\u7576\u61c9\u7528\u9700\u8981\u8a2a\u554fRedis\u6642\uff0c\u53ef\u4ee5\u5f9e\u9023\u63a5\u6c60\u4e2d\u7372\u53d6\u4e00\u500b\u53ef\u7528\u7684\u9023\u63a5\uff0c\u800c\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u5275\u5efa\u65b0\u7684\u9023\u63a5\uff0c\u5f9e\u800c\u6e1b\u5c11\u4e86\u9023\u63a5\u5efa\u7acb\u7684\u958b\u92b7\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-OTUQqoRJZT\">\u70ba\u4ec0\u9ebc\u9700\u8981\u4f7f\u7528\u9023\u63a5\u6c60\uff1f<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u6bcf\u6b21\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u90fd\u9700\u8981\u6d88\u8017\u6642\u9593\u548c\u8cc7\u6e90\uff0c\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u9019\u4e9b\u958b\u92b7\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u9023\u63a5\u6c60\u53ef\u4ee5\u9650\u5236\u540c\u6642\u6253\u958b\u7684\u9023\u63a5\u6578\u91cf\uff0c\u907f\u514d\u56e0\u70ba\u904e\u591a\u7684\u9023\u63a5\u5c0e\u81f4Redis\u670d\u52d9\u5668\u904e\u8f09\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u7c21\u5316\u4ee3\u78bc\u7d50\u69cb\uff0c\u8b93\u958b\u767c\u8005\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\uff0c\u800c\u4e0d\u662f\u9023\u63a5\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-OTUQqoRJZT\">\u5982\u4f55\u4f7f\u7528Redis\u9023\u63a5\u6c60\uff1f<\/h2>\n<p>\u5728\u4f7f\u7528Redis\u9023\u63a5\u6c60\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u9078\u64c7\u4e00\u500b\u652f\u6301\u9023\u63a5\u6c60\u7684Redis\u5ba2\u6236\u7aef\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Node.js\u7684<i>node-redis<\/i>\u5eab\u4f86\u5be6\u73feRedis\u9023\u63a5\u6c60\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const redis = require('redis');\nconst { Pool } = require('generic-pool');\n\nconst factory = {\n    create: () =&gt; {\n        return new Promise((resolve, reject) =&gt; {\n            const client = redis.createClient();\n            client.on('error', (err) =&gt; reject(err));\n            client.on('ready', () =&gt; resolve(client));\n        });\n    },\n    destroy: (client) =&gt; {\n        return new Promise((resolve) =&gt; {\n            client.quit(() =&gt; resolve());\n        });\n    }\n};\n\nconst pool = Pool({\n    create: factory.create,\n    destroy: factory.destroy,\n    max: 10, \/\/ \u6700\u5927\u9023\u63a5\u6578\n    min: 2,  \/\/ \u6700\u5c0f\u9023\u63a5\u6578\n    idleTimeoutMillis: 30000, \/\/ \u7a7a\u9592\u8d85\u6642\n    evictionRunIntervalMillis: 15000, \/\/ \u9a45\u9010\u904b\u884c\u9593\u9694\n});\n\n\/\/ \u4f7f\u7528\u9023\u63a5\u6c60\npool.acquire().then((client) =&gt; {\n    client.get('key', (err, result) =&gt; {\n        console.log(result);\n        pool.release(client); \/\/ \u91cb\u653e\u9023\u63a5\n    });\n}).catch((err) =&gt; {\n    console.error('Error acquiring client from pool', err);\n});<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5275\u5efa\u4e86\u4e00\u500b\u9023\u63a5\u6c60\uff0c\u4e26\u8a2d\u7f6e\u4e86\u6700\u5927\u548c\u6700\u5c0f\u9023\u63a5\u6578\u3002\u7576\u9700\u8981\u4f7f\u7528Redis\u6642\uff0c\u6211\u5011\u5f9e\u6c60\u4e2d\u7372\u53d6\u4e00\u500b\u9023\u63a5\uff0c\u57f7\u884c\u64cd\u4f5c\u5f8c\u518d\u5c07\u5176\u91cb\u653e\u56de\u6c60\u4e2d\u3002<\/p>\n<h2 id=\"%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-OTUQqoRJZT\">\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u5408\u7406\u914d\u7f6e\u9023\u63a5\u6578\uff1a<\/strong>\u6839\u64da\u61c9\u7528\u7684\u5be6\u969b\u9700\u6c42\u548cRedis\u670d\u52d9\u5668\u7684\u6027\u80fd\uff0c\u5408\u7406\u914d\u7f6e\u6700\u5927\u548c\u6700\u5c0f\u9023\u63a5\u6578\u3002<\/li>\n<li><strong>\u76e3\u63a7\u9023\u63a5\u72c0\u614b\uff1a<\/strong>\u5b9a\u671f\u76e3\u63a7\u9023\u63a5\u6c60\u7684\u4f7f\u7528\u60c5\u6cc1\uff0c\u53ca\u6642\u8abf\u6574\u914d\u7f6e\u4ee5\u9069\u61c9\u8b8a\u5316\u7684\u9700\u6c42\u3002<\/li>\n<li><strong>\u8655\u7406\u932f\u8aa4\uff1a<\/strong>\u5728\u4f7f\u7528\u9023\u63a5\u6c60\u6642\uff0c\u52d9\u5fc5\u8655\u7406\u53ef\u80fd\u51fa\u73fe\u7684\u932f\u8aa4\uff0c\u78ba\u4fdd\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-OTUQqoRJZT\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u9023\u63a5\u6c60\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u548c\u8cc7\u6e90\u5229\u7528\u7387\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u7684\u5834\u666f\u4e0b\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u548c\u7ba1\u7406\u9023\u63a5\u6c60\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\uff0c\u800c\u4e0d\u5fc5\u64d4\u5fc3\u9023\u63a5\u7ba1\u7406\u7684\u8907\u96dc\u6027\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u90e8\u7f72Redis\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u696d\u52d9\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Redis\u9023\u63a5\u6c60\u4f86\u63d0\u5347\u6027\u80fd\uff0c\u7c21\u5316\u9023\u63a5\u7ba1\u7406\uff0c\u4e26\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u97ff\u61c9\u901f\u5ea6\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-184666","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184666","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=184666"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184666\/revisions"}],"predecessor-version":[{"id":184667,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184666\/revisions\/184667"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}