{"id":184674,"date":"2024-11-10T15:11:19","date_gmt":"2024-11-10T07:11:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/184674\/"},"modified":"2024-11-10T15:11:20","modified_gmt":"2024-11-10T07:11:20","slug":"%e4%bd%bf%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%8f%90%e5%8d%87%e6%80%a7%e8%83%bd%e5%8f%8a%e7%af%80%e7%9c%81%e6%99%82%e9%96%93%ef%bc%88redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%80%8e%e9%ba%bc%e4%bd%bf","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/184674\/","title":{"rendered":"\u4f7f\u7528Redis\u9023\u63a5\u6c60\u63d0\u5347\u6027\u80fd\u53ca\u7bc0\u7701\u6642\u9593\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%e6%8f%90%e5%8d%87%e6%80%a7%e8%83%bd%e5%8f%8a%e7%af%80%e7%9c%81%e6%99%82%e9%96%93%ef%bc%88redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%80%8e%e9%ba%bc%e4%bd%bf-cbCsEoiFYS\">\u4f7f\u7528Redis\u9023\u63a5\u6c60\u63d0\u5347\u6027\u80fd\u53ca\u7bc0\u7701\u6642\u9593\uff08redis\u9023\u63a5\u6c60\u600e\u9ebc\u4f7f\u7528\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6027\u80fd\u548c\u6548\u7387\u662f\u81f3\u95dc\u91cd\u8981\u7684\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\u63d0\u5347\u6027\u80fd\u548c\u7bc0\u7701\u6642\u9593\u7684\u6709\u6548\u65b9\u6cd5\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-cbCsEoiFYS\">\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\u6e1b\u5c11\u6bcf\u6b21\u8acb\u6c42\u6642\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u7684\u958b\u92b7\u3002\u9019\u6a23\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\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-cbCsEoiFYS\">\u70ba\u4ec0\u9ebc\u9700\u8981\u4f7f\u7528\u9023\u63a5\u6c60\uff1f<\/h2>\n<ul>\n<li><strong>\u6e1b\u5c11\u5ef6\u9072\uff1a<\/strong>\u6bcf\u6b21\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u90fd\u9700\u8981\u6642\u9593\uff0c\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u907f\u514d\u9019\u7a2e\u5ef6\u9072\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u9023\u63a5\u6c60\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u9023\u63a5\u7684\u6578\u91cf\uff0c\u907f\u514d\u56e0\u70ba\u904e\u591a\u7684\u9023\u63a5\u5c0e\u81f4\u8cc7\u6e90\u8017\u76e1\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u541e\u5410\u91cf\uff1a<\/strong>\u901a\u904e\u91cd\u7528\u9023\u63a5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7684\u6574\u9ad4\u541e\u5410\u91cf\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-cbCsEoiFYS\">\u5982\u4f55\u4f7f\u7528Redis\u9023\u63a5\u6c60<\/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\u500bRedis\u5ba2\u6236\u7aef\u7684\u5de5\u5ee0\uff0c\u7136\u5f8c\u4f7f\u7528<i>generic-pool<\/i>\u5eab\u4f86\u7ba1\u7406\u9023\u63a5\u6c60\u3002\u901a\u904e\u8a2d\u7f6e\u6700\u5927\u548c\u6700\u5c0f\u9023\u63a5\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u9748\u6d3b\u5730\u63a7\u5236\u8cc7\u6e90\u7684\u4f7f\u7528\u3002<\/p>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e6%9c%80%e4%bd%b3%e5%af%a6%e8%b8%90-cbCsEoiFYS\">\u9023\u63a5\u6c60\u7684\u6700\u4f73\u5be6\u8e10<\/h2>\n<ul>\n<li><strong>\u5408\u7406\u914d\u7f6e\u9023\u63a5\u6578\uff1a<\/strong>\u6839\u64da\u61c9\u7528\u7684\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\u61c9\u8a72\u59a5\u5584\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-cbCsEoiFYS\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u9023\u63a5\u6c60\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\uff0c\u6e1b\u5c11\u5ef6\u9072\u4e26\u6709\u6548\u7ba1\u7406\u8cc7\u6e90\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u548c\u4f7f\u7528\u9023\u63a5\u6c60\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728\u9ad8\u4e26\u767c\u7684\u74b0\u5883\u4e2d\u4fdd\u6301\u61c9\u7528\u7684\u7a69\u5b9a\u6027\u548c\u9ad8\u6548\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\u7f72\u60a8\u7684Redis\u670d\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u7684\u696d\u52d9\u63d0\u4f9b\u66f4\u597d\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u9023\u63a5\u6c60\u53ef\u986f\u8457\u63d0\u5347\u6027\u80fd\uff0c\u7bc0\u7701\u6642\u9593\uff0c\u7c21\u5316\u9023\u63a5\u7ba1\u7406\uff0c\u8b93\u61c9\u7528\u66f4\u9ad8\u6548\u904b\u884c\u3002\u4e86\u89e3\u5982\u4f55\u6709\u6548\u4f7f\u7528Redis\u9023\u63a5\u6c60\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-184674","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184674","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=184674"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184674\/revisions"}],"predecessor-version":[{"id":184675,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/184674\/revisions\/184675"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=184674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=184674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=184674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}