{"id":5098,"date":"2024-11-14T12:42:32","date_gmt":"2024-11-14T04:42:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5098\/"},"modified":"2024-11-14T12:42:32","modified_gmt":"2024-11-14T04:42:32","slug":"redis%e6%80%8e%e4%b9%88%e5%81%9a%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5098\/","title":{"rendered":"redis\u600e\u4e48\u505a\u6d88\u606f\u961f\u5217"},"content":{"rendered":"<p><strong> \u6d88\u606f\u961f\u5217<\/strong><\/p>\n<p>Redis \u662f\u4e00\u4e2a\u5185\u5b58\u6570\u636e\u5e93\uff0c\u517c\u5177\u6d88\u606f\u4ee3\u7406\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u6784\u5efa\u9ad8\u6548\u7684\u6d88\u606f\u961f\u5217\u7cfb\u7edf\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Redis \u6784\u5efa\u6d88\u606f\u961f\u5217<\/strong><\/p>\n<p>\u8981\u4f7f\u7528 Redis \u6784\u5efa\u6d88\u606f\u961f\u5217\uff0c\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u521b\u5efa\u961f\u5217\u5217\u8868\u548c\u6d88\u606f\u5217\u8868<\/strong>\uff1a\u4f7f\u7528 LPUSH \u547d\u4ee4\u4e3a\u6bcf\u4e2a\u961f\u5217\u521b\u5efa\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u5b58\u50a8\u961f\u5217\u540d\u79f0\uff08\u5217\u8868\u540d\u79f0\uff09\uff0c\u53e6\u4e00\u4e2a\u5b58\u50a8\u6d88\u606f\uff08\u5217\u8868\u7684\u5143\u7d20\uff09\u3002<\/li>\n<li> <strong>\u53d1\u5e03\u6d88\u606f<\/strong>\uff1a\u4f7f\u7528 RPOPLPUSH \u547d\u4ee4\u4ece\u4e00\u4e2a\u961f\u5217\u5217\u8868\u4e2d\u5f39\u51fa\u6d88\u606f\uff0c\u7136\u540e\u5c06\u5176\u63a8\u9001\u5230\u53e6\u4e00\u4e2a\u961f\u5217\u5217\u8868\u4e2d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5b9e\u73b0\u4e86\u6d88\u606f\u7684\u8ba2\u9605\u548c\u53d1\u5e03\u3002<\/li>\n<li> <strong>\u8ba2\u9605\u961f\u5217<\/strong>\uff1a\u4f7f\u7528 BLPOP \u547d\u4ee4\u963b\u585e\u5f0f\u5730\u4ece\u961f\u5217\u5217\u8868\u4e2d\u83b7\u53d6\u6d88\u606f\u3002<\/li>\n<\/ol>\n<p><strong>\u4f18\u52bf<\/strong><\/p>\n<ul>\n<li> <strong>\u901f\u5ea6\u5feb<\/strong>\uff1aRedis \u662f\u4e00\u79cd\u5185\u5b58\u6570\u636e\u5e93\uff0c\u56e0\u6b64\u5177\u6709\u975e\u5e38\u5feb\u7684\u54cd\u5e94\u65f6\u95f4\u3002<\/li>\n<li> <strong>\u6301\u4e45\u6027<\/strong>\uff1aRedis \u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5728\u4e2d\uff0c\u4f8b\u5982\u78c1\u76d8\uff0c\u4ee5\u9632\u6b62\u3002<\/li>\n<li> <strong>\u53ef\u6269\u5c55\u6027<\/strong>\uff1aRedis \u53ef\u4ee5\u4f7f\u7528 Redis Sentinel \u6216 Redis Cluster \u7b49\u5de5\u5177\u8fdb\u884c\u6a2a\u5411\u6269\u5c55\u3002<\/li>\n<li> <strong>\u591a\u8ba2\u9605<\/strong>\uff1aRedis \u5141\u8bb8\u591a\u4e2a\u6d88\u8d39\u8005\u540c\u65f6\u8ba2\u9605\u961f\u5217\u3002<\/li>\n<li> <strong>\u53ef\u9760\u6027<\/strong>\uff1aRedis \u63d0\u4f9b\u4e86\u53ef\u9760\u7684\u6d88\u606f\u4f20\u9012\u673a\u5236\uff0c\u786e\u4fdd\u6d88\u606f\u4e0d\u4f1a\u4e22\u5931\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5728 Redis \u4e2d\u53d1\u5e03\u548c\u8ba2\u9605\u6d88\u606f\uff1a<\/p>\n<pre>import redis\n\n# \u521b\u5efa Redis \u5ba2\u6237\u7aef\nr = redis.Redis()\n\n# \u521b\u5efa\u961f\u5217\u5217\u8868\nr.lpush('queue:1', 'apple')\nr.lpush('queue:2', 'orange')\n\n# \u53d1\u5e03\u6d88\u606f\nr.rpoplpush('queue:1', 'queue:2')\n\n# \u8ba2\u9605\u961f\u5217\nmessage = r.blpop('queue:2')\nprint(message[1])  # \u8f93\u51fa\uff1aapple<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8be5\u793a\u4f8b\u4e2d\uff0c\u6d88\u606f &#8220;apple&#8221; \u4ece &#8220;queue:1&#8221; \u53d1\u5e03\u5230 &#8220;queue:2&#8243;\uff0c\u7136\u540e\u4ece &#8220;queue:2&#8221; \u8ba2\u9605\u5e76\u6253\u5370\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u505a\u6d88\u606f\u961f\u5217\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d88\u606f\u961f\u5217 Redis \u662f\u4e00\u4e2a\u5185\u5b58\u6570\u636e\u5e93\uff0c\u517c\u5177\u6d88\u606f\u4ee3\u7406\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u6784\u5efa\u9ad8\u6548\u7684\u6d88\u606f\u961f\u5217\u7cfb\u7edf\u3002 \u5982\u4f55\u4f7f\u7528 Redis \u6784\u5efa\u6d88\u606f\u961f\u5217 \u8981\u4f7f\u7528 Redis \u6784\u5efa\u6d88\u606f\u961f\u5217\uff0c\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u521b\u5efa\u961f\u5217\u5217\u8868\u548c\u6d88\u606f\u5217\u8868\uff1a\u4f7f\u7528 LPUSH \u547d\u4ee4\u4e3a\u6bcf\u4e2a\u961f\u5217\u521b\u5efa\u4e24\u4e2a\u5217\u8868\uff0c\u4e00\u4e2a\u5b58\u50a8\u961f\u5217\u540d\u79f0\uff08\u5217\u8868\u540d\u79f0\uff09\uff0c\u53e6\u4e00\u4e2a\u5b58\u50a8\u6d88\u606f\uff08\u5217\u8868\u7684\u5143\u7d20\uff09\u3002 \u53d1\u5e03\u6d88\u606f\uff1a\u4f7f\u7528 RPOPLPUSH \u547d\u4ee4\u4ece\u4e00\u4e2a\u961f\u5217\u5217\u8868\u4e2d\u5f39\u51fa\u6d88\u606f\uff0c\u7136\u540e\u5c06\u5176\u63a8\u9001\u5230\u53e6\u4e00\u4e2a\u961f\u5217\u5217\u8868\u4e2d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5b9e\u73b0\u4e86\u6d88\u606f\u7684\u8ba2\u9605\u548c\u53d1\u5e03\u3002 \u8ba2\u9605\u961f\u5217\uff1a\u4f7f\u7528 BLPOP \u547d\u4ee4\u963b\u585e\u5f0f\u5730\u4ece\u961f\u5217\u5217\u8868\u4e2d\u83b7\u53d6\u6d88\u606f\u3002 \u4f18\u52bf \u901f\u5ea6\u5feb\uff1aRedis \u662f\u4e00\u79cd\u5185\u5b58\u6570\u636e\u5e93\uff0c\u56e0\u6b64\u5177\u6709\u975e\u5e38\u5feb\u7684\u54cd\u5e94\u65f6\u95f4\u3002 \u6301\u4e45\u6027\uff1aRedis \u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5728\u4e2d\uff0c\u4f8b\u5982\u78c1\u76d8\uff0c\u4ee5\u9632\u6b62\u3002 \u53ef\u6269\u5c55\u6027\uff1aRedis \u53ef\u4ee5\u4f7f\u7528 Redis Sentinel \u6216 Redis Cluster \u7b49\u5de5\u5177\u8fdb\u884c\u6a2a\u5411\u6269\u5c55\u3002 \u591a\u8ba2\u9605\uff1aRedis \u5141\u8bb8\u591a\u4e2a\u6d88\u8d39\u8005\u540c\u65f6\u8ba2\u9605\u961f\u5217\u3002 \u53ef\u9760\u6027\uff1aRedis \u63d0\u4f9b\u4e86\u53ef\u9760\u7684\u6d88\u606f\u4f20\u9012\u673a\u5236\uff0c\u786e\u4fdd\u6d88\u606f\u4e0d\u4f1a\u4e22\u5931\u3002 \u793a\u4f8b \u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5728 Redis \u4e2d\u53d1\u5e03\u548c\u8ba2\u9605\u6d88\u606f\uff1a import redis # \u521b\u5efa Redis \u5ba2\u6237\u7aef r = redis.Redis() # \u521b\u5efa\u961f\u5217\u5217\u8868 r.lpush(&#8216;queue:1&#8217;, &#8216;apple&#8217;) r.lpush(&#8216;queue:2&#8217;, &#8216;orange&#8217;) # [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5098","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5098"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5098\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}