{"id":197484,"date":"2024-11-14T08:20:18","date_gmt":"2024-11-14T00:20:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/197484\/"},"modified":"2024-11-14T08:20:20","modified_gmt":"2024-11-14T00:20:20","slug":"%e5%88%a9%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%8f%90%e5%8d%87%e7%b3%bb%e7%b5%b1%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\/197484\/","title":{"rendered":"\u5229\u7528Redis\u9023\u63a5\u6c60\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\uff08redis\u9023\u63a5\u6c60\u600e\u9ebc\u4f7f\u7528\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%8f%90%e5%8d%87%e7%b3%bb%e7%b5%b1%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-XCAkjrmepT\">\u5229\u7528Redis\u9023\u63a5\u6c60\u63d0\u5347\u7cfb\u7d71\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\u512a\u5316\u662f\u6bcf\u500b\u958b\u767c\u8005\u548c\u7cfb\u7d71\u67b6\u69cb\u5e2b\u5fc5\u9808\u9762\u5c0d\u7684\u91cd\u8981\u8ab2\u984c\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u56e0\u5176\u5feb\u901f\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u7136\u800c\uff0c\u96a8\u8457\u61c9\u7528\u7684\u64f4\u5c55\uff0c\u5982\u4f55\u6709\u6548\u7ba1\u7406Redis\u7684\u9023\u63a5\u6210\u70ba\u4e86\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u7684\u95dc\u9375\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u9023\u63a5\u6c60\u4f86\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\uff0c\u4e26\u63d0\u4f9b\u5177\u9ad4\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u793a\u4f8b\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-XCAkjrmepT\">\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\u7684Redis\u9023\u63a5\uff0c\u4e26\u5728\u9700\u8981\u6642\u91cd\u8907\u4f7f\u7528\u9019\u4e9b\u9023\u63a5\uff0c\u5f9e\u800c\u6e1b\u5c11\u6bcf\u6b21\u8acb\u6c42\u6642\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u7684\u958b\u92b7\u3002\u9019\u6a23\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7cfb\u7d71\u7684\u97ff\u61c9\u901f\u5ea6\uff0c\u9084\u80fd\u6e1b\u5c11\u8cc7\u6e90\u7684\u6d6a\u8cbb\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-XCAkjrmepT\">\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>\u901a\u904e\u9650\u5236\u540c\u6642\u9023\u63a5\u7684\u6578\u91cf\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u7cfb\u7d71\u904e\u8f09\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u9023\u63a5\u6c60\u7684\u4f7f\u7528\u53ef\u4ee5\u4f7f\u4ee3\u78bc\u66f4\u52a0\u7c21\u6f54\uff0c\u6e1b\u5c11\u91cd\u8907\u7684\u9023\u63a5\u7ba1\u7406\u908f\u8f2f\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-XCAkjrmepT\">\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<h2 id=\"%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e9%85%8d%e7%bd%ae%e5%8f%83%e6%95%b8-XCAkjrmepT\">\u9023\u63a5\u6c60\u7684\u914d\u7f6e\u53c3\u6578<\/h2>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86\u5e7e\u500b\u91cd\u8981\u7684\u914d\u7f6e\u53c3\u6578\uff1a<\/p>\n<ul>\n<li><strong>max\uff1a<\/strong>\u5b9a\u7fa9\u9023\u63a5\u6c60\u4e2d\u6700\u5927\u7684\u9023\u63a5\u6578\u91cf\u3002<\/li>\n<li><strong>min\uff1a<\/strong>\u5b9a\u7fa9\u9023\u63a5\u6c60\u4e2d\u6700\u5c0f\u7684\u9023\u63a5\u6578\u91cf\u3002<\/li>\n<li><strong>idleTimeoutMillis\uff1a<\/strong>\u5b9a\u7fa9\u9023\u63a5\u5728\u7a7a\u9592\u72c0\u614b\u4e0b\u7684\u8d85\u6642\u6642\u9593\u3002<\/li>\n<li><strong>evictionRunIntervalMillis\uff1a<\/strong>\u5b9a\u7fa9\u9a45\u9010\u904b\u884c\u7684\u9593\u9694\u6642\u9593\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XCAkjrmepT\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u9023\u63a5\u6c60\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\uff0c\u6e1b\u5c11\u9023\u63a5\u7ba1\u7406\u7684\u958b\u92b7\uff0c\u4e26\u6709\u6548\u5730\u7ba1\u7406\u8cc7\u6e90\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u9023\u63a5\u6c60\u7684\u53c3\u6578\uff0c\u53ef\u4ee5\u6839\u64da\u5be6\u969b\u9700\u6c42\u8abf\u6574\u6027\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u5b58\u53d6\u7684\u61c9\u7528\u4f86\u8aaa\uff0cRedis\u9023\u63a5\u6c60\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u63d0\u5347\u60a8\u7684\u7cfb\u7d71\u6027\u80fd\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\u61c9\u7528\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u9023\u63a5\u6c60\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\uff0c\u4e86\u89e3Redis\u9023\u63a5\u6c60\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\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-197484","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197484","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=197484"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197484\/revisions"}],"predecessor-version":[{"id":197485,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197484\/revisions\/197485"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=197484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=197484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=197484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}