{"id":167643,"date":"2024-11-06T03:19:26","date_gmt":"2024-11-05T19:19:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167643\/"},"modified":"2024-11-06T03:19:27","modified_gmt":"2024-11-05T19:19:27","slug":"%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%be%e9%ab%98%e4%b8%a6%e7%99%bc%e4%ba%8b%e4%bb%b6%e8%99%95%e7%90%86%e6%a8%a1%e5%9e%8b%ef%bc%88redis%e9%ab%98%e4%b8%a6%e7%99%bc%e4%ba%8b%e4%bb%b6%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167643\/","title":{"rendered":"\u5229\u7528Redis\u5be6\u73fe\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\uff08redis\u9ad8\u4e26\u767c\u4e8b\u4ef6\u6a21\u578b\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%be%e9%ab%98%e4%b8%a6%e7%99%bc%e4%ba%8b%e4%bb%b6%e8%99%95%e7%90%86%e6%a8%a1%e5%9e%8b%ef%bc%88redis%e9%ab%98%e4%b8%a6%e7%99%bc%e4%ba%8b%e4%bb%b6%e6%a8%a1%e5%9e%8b-vBEfAAaTGP\">\u5229\u7528Redis\u5be6\u73fe\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\uff08redis\u9ad8\u4e26\u767c\u4e8b\u4ef6\u6a21\u578b\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\u548c\u61c9\u7528\u7a0b\u5e8f\u7684\u8907\u96dc\u6027\u63d0\u5347\uff0c\u5982\u4f55\u6709\u6548\u5730\u8655\u7406\u5927\u91cf\u7684\u8acb\u6c42\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5353\u8d8a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0c\u6210\u70ba\u4e86\u5be6\u73fe\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%89%b9%e6%80%a7-vBEfAAaTGP\">Redis\u7684\u57fa\u672c\u7279\u6027<\/h2>\n<p>Redis\u662f\u4e00\u500b\u958b\u6e90\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\u7b49\u3002\u5176\u4e3b\u8981\u7279\u6027\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u6bcf\u79d2\u8655\u7406\u6578\u5341\u842c\u6b21\u7684\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u9ad8\u4e26\u767c\u5834\u666f\u4e0b\u8868\u73fe\u51fa\u8272\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\uff1a<\/strong>Redis\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u70ba\u7cfb\u7d71\u5d29\u6f70\u800c\u4e1f\u5931\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\u4f86\u5b58\u5132\u548c\u8655\u7406\u6578\u64da\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\u652f\u6301\uff1a<\/strong>Redis Cluster\u5141\u8a31\u5c07\u6578\u64da\u5206\u4f48\u5230\u591a\u500b\u7bc0\u9ede\u4e0a\uff0c\u9032\u4e00\u6b65\u63d0\u9ad8\u4e86\u7cfb\u7d71\u7684\u53ef\u64f4\u5c55\u6027\u548c\u53ef\u7528\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%ab%98%e4%b8%a6%e7%99%bc%e4%ba%8b%e4%bb%b6%e8%99%95%e7%90%86%e6%a8%a1%e5%9e%8b%e7%9a%84%e9%9c%80%e6%b1%82-vBEfAAaTGP\">\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\u7684\u9700\u6c42<\/h2>\n<p>\u5728\u9ad8\u4e26\u767c\u5834\u666f\u4e2d\uff0c\u7cfb\u7d71\u9700\u8981\u80fd\u5920\u540c\u6642\u8655\u7406\u5927\u91cf\u7684\u8acb\u6c42\uff0c\u9019\u5c0d\u65bc\u5f8c\u7aef\u670d\u52d9\u7684\u6027\u80fd\u63d0\u51fa\u4e86\u5f88\u9ad8\u7684\u8981\u6c42\u3002\u50b3\u7d71\u7684\u963b\u585e\u5f0fI\/O\u6a21\u578b\u5728\u9762\u5c0d\u9ad8\u4e26\u767c\u8acb\u6c42\u6642\uff0c\u5f80\u5f80\u6703\u5c0e\u81f4\u6027\u80fd\u74f6\u9838\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u4e00\u554f\u984c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u63a1\u7528\u975e\u963b\u585e\u5f0fI\/O\u548c\u4e8b\u4ef6\u9a45\u52d5\u6a21\u578b\uff0c\u4e26\u7d50\u5408Redis\u4f86\u5be6\u73fe\u9ad8\u6548\u7684\u4e8b\u4ef6\u8655\u7406\u3002<\/p>\n<h2 id=\"%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%be%e9%ab%98%e4%b8%a6%e7%99%bc%e4%ba%8b%e4%bb%b6%e8%99%95%e7%90%86-vBEfAAaTGP\">\u5229\u7528Redis\u5be6\u73fe\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5229\u7528Redis\u5be6\u73fe\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\u7684\u4e00\u500b\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\nclient.on('error', (err) =&gt; {\n    console.log('Error ' + err);\n});\n\n\/\/ \u6a21\u64ec\u9ad8\u4e26\u767c\u8acb\u6c42\nfor (let i = 0; i &lt; 1000; i++) {\n    client.rpush('eventQueue', `event${i}`, (err, reply) =&gt; {\n        if (err) {\n            console.error(err);\n        } else {\n            console.log(`Added event${i} to queue`);\n        }\n    });\n}\n\n\/\/ \u8655\u7406\u4e8b\u4ef6\nsetInterval(() =&gt; {\n    client.lpop('eventQueue', (err, event) =&gt; {\n        if (event) {\n            console.log(`Processing ${event}`);\n            \/\/ \u5728\u9019\u88e1\u8655\u7406\u4e8b\u4ef6\n        }\n    });\n}, 100);\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528Redis\u7684\u5217\u8868\u6578\u64da\u7d50\u69cb\u4f86\u5be6\u73fe\u4e8b\u4ef6\u968a\u5217\u3002\u901a\u904e\u5c07\u4e8b\u4ef6\u63a8\u9001\u5230\u968a\u5217\u4e2d\uff0c\u7136\u5f8c\u5b9a\u671f\u5f9e\u968a\u5217\u4e2d\u53d6\u51fa\u4e8b\u4ef6\u9032\u884c\u8655\u7406\uff0c\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u5730\u5be6\u73fe\u9ad8\u4e26\u767c\u7684\u4e8b\u4ef6\u8655\u7406\u3002<\/p>\n<h2 id=\"%e5%84%aa%e5%8c%96%e9%ab%98%e4%b8%a6%e7%99%bc%e4%ba%8b%e4%bb%b6%e8%99%95%e7%90%86%e6%a8%a1%e5%9e%8b-vBEfAAaTGP\">\u512a\u5316\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b<\/h2>\n<p>\u70ba\u4e86\u9032\u4e00\u6b65\u512a\u5316\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\uff0c\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528Pipeline\uff1a<\/strong>Redis\u652f\u6301Pipeline\u6280\u8853\uff0c\u53ef\u4ee5\u5728\u4e00\u6b21\u7db2\u7d61\u8acb\u6c42\u4e2d\u767c\u9001\u591a\u500b\u547d\u4ee4\uff0c\u5f9e\u800c\u6e1b\u5c11\u7db2\u7d61\u5ef6\u9072\u3002<\/li>\n<li><strong>\u5408\u7406\u8a2d\u8a08\u6578\u64da\u7d50\u69cb\uff1a<\/strong>\u6839\u64da\u5177\u9ad4\u7684\u696d\u52d9\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6578\u64da\u8f49\u63db\u548c\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u76e3\u63a7\u548c\u8abf\u6574\uff1a<\/strong>\u5b9a\u671f\u76e3\u63a7Redis\u7684\u6027\u80fd\u6307\u6a19\uff0c\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u8abf\u6574\u914d\u7f6e\u548c\u67b6\u69cb\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-vBEfAAaTGP\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u5be6\u73fe\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\u662f\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u61c9\u5c0d\u65e5\u76ca\u589e\u9577\u7684\u8acb\u6c42\u91cf\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u512a\u5316\uff0cRedis\u53ef\u4ee5\u5728\u9ad8\u4e26\u767c\u5834\u666f\u4e2d\u767c\u63ee\u51fa\u8272\u7684\u6027\u80fd\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u9ad8\u6027\u80fd\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u4f3a\u670d\u5668<\/a> \u9084\u662f\u9748\u6d3b\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\uff0c\u90fd\u80fd\u70ba\u60a8\u7684\u696d\u52d9\u63d0\u4f9b\u5f37\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u69cb\u5efa\u9ad8\u4e26\u767c\u4e8b\u4ef6\u8655\u7406\u6a21\u578b\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u8207\u97ff\u61c9\u901f\u5ea6\uff0c\u5be6\u73fe\u9ad8\u6548\u80fd\u7684\u6578\u64da\u8655\u7406\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-167643","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167643","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=167643"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167643\/revisions"}],"predecessor-version":[{"id":167644,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167643\/revisions\/167644"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}