{"id":1912,"date":"2023-04-01T09:20:13","date_gmt":"2023-04-01T01:20:13","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1912"},"modified":"2023-04-22T08:53:55","modified_gmt":"2023-04-22T00:53:55","slug":"implementation-of-redis-queue","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/implementation-of-redis-queue\/","title":{"rendered":"Redis\u961f\u5217\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>\u5173\u4e8eredis\u961f\u5217\u7684\u5b9e\u73b0\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a<\/p>\n<ul>\n<li>\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u5f0f<\/li>\n<li>\u53d1\u5e03\u8005\u8ba2\u9605\u8005\u6a21\u5f0f<\/li>\n<\/ul>\n<h2>\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u5f0f<\/h2>\n<p><!-- more --><\/p>\n<h3>\u666e\u901a\u7248\u672c<\/h3>\n<p>\u6bd4\u5982\u4e00\u4e2a\u961f\u5217\u91cc\u9762\uff0c\u751f\u4ea7\u8005A push\u4e86\u4e00\u4e2a\u6570\u636e\u8fdb\u53bb\uff0c\u6d88\u8d39\u8005B pop\u4e86\u8fd9\u4e2a\u6570\u636e\uff0c\u90a3\u4e2a\u8fd9\u4e2a\u961f\u5217\u4f9d\u65e7\u4e3a\u7a7a\u3002\u6240\u4ee5\u662f\u4e00\u5bf9\u4e00\u7684\u3002<\/p>\n<p>\u81f3\u4e8e\u662f\u5148\u8fdb\u5148\u51fa\u8fd8\u662f\u5148\u8fdb\u540e\u51fa\u7b49\uff0c\u53ef\u4ee5\u4f9d\u7167\u51fd\u6570<code>lpush<\/code>(\u4ece\u961f\u5217\u5de6\u8fb9\uff0c\u4e5f\u5c31\u662f\u961f\u9996push\u4e00\u4e2a\u6570\u636e)\u3001<code>rpush<\/code>(\u4ece\u961f\u5217\u53f3\u8fb9\u4e5f\u5c31\u662f\u961f\u5c3epush\u4e00\u4e2a\u6570\u636e)\u3001<code>lpop<\/code>(\u540c\u7406)\u3001<code>rpop<\/code>\u7b49\u6765\u63a7\u5236\u3002<\/p>\n<p>\u63d2\u5165\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; lpush mylist 2\n(integer) 1\n127.0.0.1:6379&gt; lpush mylist 1\n(integer) 2\n127.0.0.1:6379&gt; rpush mylist 3\n(integer) 3<\/code><\/pre>\n<p>\u663e\u793a\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; lrange mylist 0 -1\n1) &quot;1&quot;\n2) &quot;2&quot;\n3) &quot;3&quot;<\/code><\/pre>\n<p>\u53d6\u51fa\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; lpop mylist\n&quot;1&quot;\n127.0.0.1:6379&gt; rpop mylist\n&quot;3&quot;<\/code><\/pre>\n<h3>\u963b\u585e\u7248\u672c<\/h3>\n<p>\u4f46\u662f\u4e0a\u9762\u7684\u547d\u4ee4\u90fd\u662f\u7acb\u5373\u8fd4\u56de\u7684\uff0c\u65e0\u8bba\u6570\u636e\u6709\u65e0\uff0c\u5173\u4e8e\u53d6\u6570\u636e<code>lpop<\/code>\u6709\u4e2a\u589e\u5f3a\u7248\u672c\uff0c<code>blpop<\/code>(block left pop)\u963b\u585e\u7248\u672c<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<code>blpop key1 key2 ... keyn 10<\/code><\/p>\n<p>\u540c\u65f6\u9884\u83b7\u53d6\u591a\u4e2akey\u7684\u503c\uff0c\u5e76\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u4e3a10s\uff0c\u5982\u679c\u6240\u6709key\uff0c\u6709\u4e9bkey\u6709value\u5c31\u7acb\u5373\u8fd4\u56de\uff0c\u5982\u679c\u6240\u6709key\u90fd\u6ca1\u6709value\u5c31\u963b\u585e10\u79d2\u8fd4\u56de<\/p>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; blpop mylist 5\n1) &quot;mylist&quot;\n2) &quot;2&quot;\n127.0.0.1:6379&gt; blpop mylist 5\n(nil)\n(5.01s)<\/code><\/pre>\n<pre><code class=\"language-bash\">127.0.0.1:6379&gt; rpush mylist one\n(integer) 1\n127.0.0.1:6379&gt; rpush mylist two\n(integer) 2\n127.0.0.1:6379&gt; rpush mylist2 one2\n(integer) 1\n127.0.0.1:6379&gt; blpop mylist mylist2 5\n1) &quot;mylist&quot;\n2) &quot;one&quot;\n127.0.0.1:6379&gt; blpop mylist mylist2 5\n1) &quot;mylist&quot;\n2) &quot;two&quot;\n127.0.0.1:6379&gt; blpop mylist mylist2 5\n1) &quot;mylist2&quot;\n2) &quot;one2&quot;\n127.0.0.1:6379&gt; blpop mylist mylist2 5\n(nil)\n(5.18s)<\/code><\/pre>\n<p>\u5173\u4e8e<code>blpop<\/code>\u591a\u4e2akey\u8fd4\u56de\u6570\u636e\u7684\u987a\u5e8f\uff0c\u6bd4\u5982<code>blpop mylist mylist2 5<\/code>\u8fd9\u4e2a\u547d\u4ee4\uff0c\u5148\u68c0\u67e5mylist\u6709\u6570\u636e\u5c31\u8fd4\u56de\uff0c\u5982\u679c\u6ca1\u6709\u6570\u636e\uff0c\u5c31\u68c0\u67e5mylist2\u4f9d\u6b21\u3002\u3002\u3002\u3002\u76f4\u5230\u6240\u6709key\u68c0\u67e5\u5b8c\u5982\u679c\u90fd\u6ca1\u6709\u6570\u636e\u5c31\u963b\u585e\u3002<\/p>\n<p>\u8fd9\u79cd\u4ece\u591a\u4e2a\u961f\u5217\u91cc\u9762\u53d6\u6570\u636e\u7684\u65b9\u5f0f\u53ef\u4ee5\u7528\u6765\u505a\u4f18\u5148\u7ea7\u7684\u961f\u5217\uff0c\u6bd4\u5982mylist\u961f\u5217\u7684\u4f18\u5148\u7ea7\u9ad8\u4e8emylist2\uff0cpush\u7684\u65f6\u5019\uff0c\u9ad8\u4f18\u5148\u7ea7\u5c31push\u5230mylist\u91cc\u9762\uff0c\u666e\u901a\u4f18\u5148\u7ea7\u5c31push\u5230mylist2\u91cc\u9762\uff0c\u8fd9\u6837\u5c31\u4f1a\u5148\u53d6mylist\u91cc\u9762\u7684\u9ad8\u4f18\u5148\u7ea7\u7684\u6570\u636e\u6765\u5904\u7406\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u9047\u5230\u961f\u5217\u7684\u4f18\u5148\u7ea7\u7b49\u7ea7\u8fc7\u591a\uff0c\u6bd4\u5982\u6709(0-9999)\u4e2a\u4f18\u5148\u7ea7\uff0c\u4e0a\u9762\u5c31\u4e0d\u884c\u4e86\u3002\u89e3\u51b3\u601d\u8def\u662f\u63d2\u5165\u7684\u65f6\u5019\u5148\u628a\u6570\u636e\u53d6\u51fa\u6765\u81ea\u5df1\u5b9e\u73b0\u4e8c\u5206\u67e5\u627e\u627e\u51fa\u8be5\u63d2\u5165\u7684\u4f4d\u7f6e\uff0c\u7528lset\u547d\u4ee4\u63d2\u5165\u3002<\/p>\n<p>\u5982\u679c\u6570\u636e\u8fc7\u591a\uff0c\u6bd4\u5982\u961f\u5217\u6709\u51e0\u5341\u4e07\uff0c\u53ef\u4ee5\u628a\u961f\u5217\u5206\u6210\u51e0\u5341\u4e2a\u6216\u51e0\u767e\u4e2a\u5c0f\u961f\u5217\uff0c\u6bd4\u59820\u53f7\u961f\u5217\u5b58\u4f18\u5148\u7ea7\u4e3a\uff080-1000\uff09\uff0c1\u53f7\u961f\u5217\u5b58\u4f18\u5148\u7ea7\u4e3a(1001-2000)\u7684\u6570\u636e\uff0c\u4f9d\u6b21\u3002\u3002\u3002\u3002\u3002<\/p>\n<p>\u7531\u4e8e\u8fd9\u79cd\u961f\u5217\u6a21\u5f0fpop\u51fa\u6765\u4e00\u4e2a\u540e\u5c31\u8fd4\u56de\u4e86\uff0c\u6240\u4ee5\u5904\u7406\u4e1a\u52a1\u7684\u65f6\u5019\u6700\u597d\u628apop\u5199\u5728\u4e00\u4e2a<code>while(true){pop.....do logic}<\/code>\u5faa\u73af\u91cc\u9762\u3002<\/p>\n<h2>\u53d1\u5e03\u8005\u8ba2\u9605\u8005\u6a21\u5f0f<\/h2>\n<p>\u6982\u5ff5\uff1a\u4e09\u4e2a\u7528\u6237A,B,C\u540c\u65f6\u90fd\u8ba2\u9605\u4e86\u4e00\u4e2achannel\u540d\u5b57\u53ebmsg\uff0c\u7136\u540e\u53d1\u5e03\u8005\u5f80msg\u7684channel\u91cc\u9762\u53d1\u5e03\u4e86\u4e00\u4e2a\u6570\u636e\uff0c\u90a3\u4e48A,B,C\u4e09\u4e2a\u7528\u6237\u90fd\u4f1a\u6536\u5230\u8be5\u6570\u636e\u3002<\/p>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>1\u3001\u5f88\u660e\u663e\uff0c\u4e09\u4e2a\u7528\u6237ABC\u9700\u8981\u963b\u585e\u3002\u600e\u4e48\u6536\u5230\u8ba2\u9605\u7684\u6570\u636e\u5462\uff0c\u80af\u5b9a\u662f\u4f9d\u9760\u6ce8\u518c\u5728redis\u91cc\u9762\u7684\u56de\u8c03\u51fd\u6570<br \/>\n2\u3001\u53d1\u5e03\u7684\u6570\u636e\u4e0d\u4f1a\u5728redis\u91cc\u9762\u590d\u73b0\uff0c\u610f\u601d\u5c31\u662f\u53d1\u5e03\u4e86\u4ee5\u540e\uff0cA,B,C\u7531\u4e8e\u79cd\u79cd\u539f\u56e0\u6ca1\u6536\u5230\u5c31\u6ca1\u6536\u5230<\/p>\n<p>\u76f4\u63a5\u4e0a\u4ee3\u7801\uff1a<\/p>\n<p>\uff081\uff09\u53d1\u5e03\u8005<\/p>\n<pre><code class=\"language-php\">$redis   = new Redis();\n$redis-&gt;connect(&#039;127.0.0.1&#039;, &#039;6379&#039;);\n\n$type    = &#039;msg&#039;;\n$msg     = &quot;https:\/\/www.appblog.cn&quot;;\n\n$result = $redis-&gt;publish($type, $msg);    \/\/\u540c\u6b65\u64cd\u4f5c\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fchannel\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6570\u636e\n\nif (empty($result)) {\n    echo &#039;publish failed&#039;;\n} else {\n    echo &#039;publish success&#039;;\n}<\/code><\/pre>\n<p>\uff082\uff09\u8ba2\u9605\u8005<\/p>\n<pre><code class=\"language-php\">$redis    = new Redis();\n$redis-&gt;connect(&#039;127.0.0.1&#039;, &#039;6379&#039;);\n\n$redis-&gt;setOption(\\Redis::OPT_READ_TIMEOUT, - 1);\n\/\/\u91cd\u70b9\uff01\uff01\uff01\u5f97\u8bbe\u7f6e\u4e0dtimeout\uff0c\u5426\u521960\u5185\u6ca1\u6536\u5230\u53d1\u5e03\u8005\u7684\u6d88\u606f\u5c31\u4f1a\u81ea\u52a8\u65ad\u5f00\n\n$type     = &#039;msg&#039;;\n\n$result = $redis-&gt;subscribe(array($type), &#039;callback&#039;);    \/\/\u5f02\u6b65\u963b\u585e\uff0c\u6709\u6d88\u606f\u6765\u81ea\u52a8\u8c03\u7528callback\u51fd\u6570\n\nfunction callback($redis, $type, $msg) {\n    \/\/\u8fd9\u91cc\u5904\u7406\u903b\u8f91\n    echo $type. &quot;==&gt;&quot; . $msg . &quot;\\r\\n&quot;;\n}<\/code><\/pre>\n<h2>\u4e24\u79cd\u65b9\u5f0f\u6bd4\u8f83<\/h2>\n<p>1\u3001\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u5f0f\u9700\u8981\u6d88\u8d39\u8005\u4e3b\u52a8\u53bb\u62c9\u6570\u636e\uff0c\u5982\u679c\u5199\u6210\u6b7b\u5faa\u73af\u5e76\u4e14\u963b\u585e\u6a21\u5f0f\uff0c\u5c31\u548c\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u5dee\u4e0d\u591a\u4e86<br \/>\n2\u3001\u53d1\u5e03\u8005\u8ba2\u9605\u8005\u6a21\u5f0f\u7684\u6570\u636e\u5e76\u4e0d\u5b58\u5728\u4e8e\u67d0\u4e2akey\u91cc\u9762\uff0c\u5982\u679c\u8ba2\u9605\u8005\u6ca1\u6536\u5230\u5219\u8be5\u6570\u636e\u5c31\u4e22\u5931\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8eredis\u961f\u5217\u7684\u5b9e\u73b0\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a \u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u5f0f \u53d1\u5e03\u8005\u8ba2\u9605\u8005\u6a21\u5f0f \u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u5f0f \u666e\u901a\u7248\u672c \u6bd4\u5982\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[443],"class_list":["post-1912","post","type-post","status-publish","format-standard","hentry","category-redis","tag-443"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1912"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1912\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}