{"id":23614,"date":"2024-11-21T17:48:42","date_gmt":"2024-11-21T09:48:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23614\/"},"modified":"2024-11-21T17:48:42","modified_gmt":"2024-11-21T09:48:42","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8%e9%98%9f%e5%88%97%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23614\/","title":{"rendered":"\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u961f\u5217\u6280\u672f"},"content":{"rendered":"<p>\u968f\u7740web\u7f51\u7ad9\u7684\u4e0d\u65ad\u53d1\u5c55\u548c\u7528\u6237\u91cf\u7684\u589e\u957f\uff0c\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u4efb\u52a1\u8c03\u5ea6\u80fd\u529b\u6210\u4e3a\u8bbe\u8ba1\u4e2d\u7684\u74f6\u9888\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u961f\u5217\u6280\u672f\u88ab\u5e7f\u6cdb\u5730\u5e94\u7528\u4e8eweb\u7cfb\u7edf\u4e2d\u3002<\/p>\n<p>ThinkPHP6\u662f\u4e00\u6b3e\u4f18\u79c0\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u961f\u5217\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u5904\u7406\u4efb\u52a1\u548c\u8c03\u5ea6\u4efb\u52a1\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u961f\u5217\u6280\u672f\u3002<\/p>\n<p>\u4e00\u3001\u961f\u5217\u6280\u672f\u6982\u8ff0<\/p>\n<p>\u961f\u5217\u6280\u672f\u662f\u4e00\u79cd\u5f02\u6b65\u5904\u7406\u4efb\u52a1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u4efb\u52a1\u63d0\u4ea4\u5230\u961f\u5217\u4e2d\uff0c\u7136\u540e\u7531\u540e\u53f0\u8fdb\u7a0b\u6216\u670d\u52a1\u7aef\u5f02\u6b65\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\uff0c\u4ee5\u51cf\u8f7bWeb\u7cfb\u7edf\u7684\u8d1f\u8f7d\u538b\u529b\u3002<\/p>\n<p>\u961f\u5217\u6280\u672f\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b\uff1a\u6d88\u606f\u961f\u5217\u548c\u4efb\u52a1\u961f\u5217\u3002\u6d88\u606f\u961f\u5217\u901a\u5e38\u7528\u4e8e\u5904\u7406\u9700\u8981\u7acb\u5373\u54cd\u5e94\u7684\u6d88\u606f\uff0c\u5982\u6d88\u606f\u901a\u77e5\u7b49\u3002\u800c\u4efb\u52a1\u961f\u5217\u5219\u7528\u4e8e\u5f02\u6b65\u5904\u7406\u90a3\u4e9b\u9700\u8981\u65f6\u95f4\u548c\u8ba1\u7b97\u529b\u7684\u4efb\u52a1\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u4efb\u52a1\u961f\u5217\u7b80\u79f0\u4e3a\u201c\u961f\u5217\u201d\uff0c\u5b83\u7531\u6d88\u606f\u961f\u5217\u9a71\u52a8\u548c\u65e5\u5fd7\u8bb0\u5f55\u540e\u7aef\u7ec4\u6210\u3002<\/p>\n<p>\u4e8c\u3001\u961f\u5217\u6280\u672f\u4f7f\u7528\u6b65\u9aa4<\/p>\n<ol>\n<li>\u5b89\u88c5\u6d88\u606f\u961f\u5217\u9a71\u52a8<\/li>\n<\/ol>\n<p>\u6d88\u606f\u961f\u5217\u9a71\u52a8\u662f\u7528\u4e8e\u5b58\u653e\u548c\u5904\u7406\u4efb\u52a1\u961f\u5217\u7684\u670d\u52a1\u7aef\u7a0b\u5e8f\u3002\u5728ThinkPHP6\u4e2d\uff0c\u652f\u6301\u591a\u79cd\u6d88\u606f\u961f\u5217\u9a71\u52a8\uff0c\u5982Redis\u3001MySQL\u7b49\u3002<\/p>\n<p>\u4ee5Redis\u6d88\u606f\u961f\u5217\u4e3a\u4f8b\uff0c\u5728ThinkPHP6\u4e2d\u9700\u8981\u5b89\u88c5\u201ctopthink\/think-queue\u201d\u6269\u5c55\u5305\u548c\u201cpredis\/predis\u201d\u6269\u5c55\u5305\u3002\u5b89\u88c5\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<p>composer require topthink\/think-queue<br \/>composer require predis\/predis<\/p>\n<ol>\n<li>\u914d\u7f6e\u961f\u5217\u53c2\u6570<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u961f\u5217\u53c2\u6570\uff0c\u5305\u62ec\u9a71\u52a8\u7c7b\u578b\u3001\u9a71\u52a8\u53c2\u6570\u3001\u961f\u5217\u540d\u79f0\u7b49\u3002\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\u4e3a\u201cconfig\/queue.php\u201d\u3002<\/p>\n<p>\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<p>return [<\/p>\n<pre>\/\/ \u9ed8\u8ba4\u961f\u5217\u9a71\u52a8\uff1aRedis\n'default' =&amp;gt; 'redis',\n\n\/\/ \u961f\u5217\u8fde\u63a5\u53c2\u6570\n'connections' =&amp;gt; [\n    'redis' =&amp;gt; [\n        'driver' =&amp;gt; 'redis',\n        'queue' =&amp;gt; 'default', \/\/ \u961f\u5217\u540d\u79f0\n        'connection' =&amp;gt; 'default',\n        'host' =&amp;gt; '127.0.0.1',\n        'port' =&amp;gt; 6379,\n        'password' =&amp;gt; '',\n        'database' =&amp;gt; 0,\n        'expire' =&amp;gt; 60,\n    ],\n],<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>];<\/p>\n<ol>\n<li>\u7f16\u5199\u4efb\u52a1\u7c7b<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u961f\u5217\u4efb\u52a1\u7c7b\u4f4d\u4e8eappjob\u76ee\u5f55\u4e0b\u3002\u4efb\u52a1\u7c7b\u5fc5\u987b\u5305\u542b\u4e00\u4e2a\u65b9\u6cd5handle()\uff0c\u7528\u4e8e\u6267\u884c\u4efb\u52a1\u7684\u5177\u4f53\u903b\u8f91\u3002<\/p>\n<p>\u4efb\u52a1\u7c7b\u793a\u4f8b\uff1a<\/p>\n<p>namespace appjob;<\/p>\n<p>class SendEmail<br \/>{<\/p>\n<pre>public function handle($data)\n{\n    \/\/ \u5904\u7406\u53d1\u9001\u90ae\u4ef6\u4efb\u52a1\u7684\u903b\u8f91\n    echo '\u53d1\u9001\u90ae\u4ef6\uff1a' . $data['email'] . PHP_EOL;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<\/p>\n<ol>\n<li>\u63d0\u4ea4\u4efb\u52a1\u5230\u961f\u5217<\/li>\n<\/ol>\n<p>\u5728\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528Queue\u7c7b\u7684push()\u6216delay()\u65b9\u6cd5\u6765\u63d0\u4ea4\u4efb\u52a1\u5230\u961f\u5217\u3002push()\u65b9\u6cd5\u76f4\u63a5\u63d0\u4ea4\u5230\u961f\u5217\u4e2d\uff0c\u800cdelay()\u65b9\u6cd5\u53ef\u4ee5\u8bbe\u7f6e\u5ef6\u65f6\u65f6\u95f4\u6765\u5f02\u6b65\u5904\u7406\u4efb\u52a1\u3002<\/p>\n<p>\u63d0\u4ea4\u4efb\u52a1\u793a\u4f8b\uff1a<\/p>\n<p>use thinkacadeQueue;<br \/>use appjobSendEmail;<\/p>\n<p>\/\/ \u63d0\u4ea4\u5f02\u6b65\u53d1\u9001\u90ae\u4ef6\u4efb\u52a1<br \/>$data = [<\/p>\n<pre>'email' =&amp;gt; 'example@example.com',\n'title' =&amp;gt; '\u90ae\u4ef6\u6807\u9898',\n'content' =&amp;gt; '\u90ae\u4ef6\u5185\u5bb9',<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>];<br \/>$job = new SendEmail($data);<br \/>Queue::push($job);<\/p>\n<p>\/\/ \u63d0\u4ea4\u5ef6\u65f6\u53d1\u9001\u90ae\u4ef6\u4efb\u52a1<br \/>$job = new SendEmail($data);<br \/>Queue::later(10, $job);<\/p>\n<ol>\n<li>\u542f\u52a8\u961f\u5217\u540e\u53f0\u8fdb\u7a0b<\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u542f\u52a8\u961f\u5217\u540e\u53f0\u8fdb\u7a0b\u7684\u547d\u4ee4\uff0c\u5373\u53ef\u5f00\u59cb\u6267\u884c\u961f\u5217\u4efb\u52a1\uff1a<\/p>\n<p>php think queue:work<\/p>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>\u961f\u5217\u6280\u672f\u662f\u4e00\u79cd\u4f18\u79c0\u7684Web\u7cfb\u7edf\u7684\u5f02\u6b65\u5904\u7406\u548c\u4efb\u52a1\u8c03\u5ea6\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u4efb\u52a1\u8c03\u5ea6\u80fd\u529b\u3002<\/p>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u961f\u5217\u6280\u672f\u7684\u4f7f\u7528\u975e\u5e38\u7b80\u5355\u3002\u53ea\u9700\u8981\u5b89\u88c5\u6d88\u606f\u961f\u5217\u9a71\u52a8\u3001\u914d\u7f6e\u961f\u5217\u53c2\u6570\u3001\u7f16\u5199\u4efb\u52a1\u7c7b\u548c\u63d0\u4ea4\u4efb\u52a1\u5230\u961f\u5217\uff0c\u6700\u540e\u542f\u52a8\u961f\u5217\u540e\u53f0\u8fdb\u7a0b\u5373\u53ef\u3002<\/p>\n<p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528ThinkPHP6\u4e2d\u7684\u961f\u5217\u6280\u672f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u961f\u5217\u6280\u672f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740web\u7f51\u7ad9\u7684\u4e0d\u65ad\u53d1\u5c55\u548c\u7528\u6237\u91cf\u7684\u589e\u957f\uff0c\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u548c\u4efb\u52a1\u8c03\u5ea6\u80fd\u529b\u6210\u4e3a\u8bbe\u8ba1\u4e2d\u7684\u74f6\u9888\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u961f\u5217\u6280\u672f\u88ab\u5e7f\u6cdb\u5730\u5e94\u7528\u4e8eweb\u7cfb\u7edf\u4e2d\u3002 ThinkPHP6\u662f\u4e00\u6b3e\u4f18\u79c0\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u961f\u5217\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u5904\u7406\u4efb\u52a1\u548c\u8c03\u5ea6\u4efb\u52a1\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728ThinkPHP6\u4e2d\u4f7f\u7528\u961f\u5217\u6280\u672f\u3002 \u4e00\u3001\u961f\u5217\u6280\u672f\u6982\u8ff0 \u961f\u5217\u6280\u672f\u662f\u4e00\u79cd\u5f02\u6b65\u5904\u7406\u4efb\u52a1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u4efb\u52a1\u63d0\u4ea4\u5230\u961f\u5217\u4e2d\uff0c\u7136\u540e\u7531\u540e\u53f0\u8fdb\u7a0b\u6216\u670d\u52a1\u7aef\u5f02\u6b65\u5904\u7406\u8fd9\u4e9b\u4efb\u52a1\uff0c\u4ee5\u51cf\u8f7bWeb\u7cfb\u7edf\u7684\u8d1f\u8f7d\u538b\u529b\u3002 \u961f\u5217\u6280\u672f\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b\uff1a\u6d88\u606f\u961f\u5217\u548c\u4efb\u52a1\u961f\u5217\u3002\u6d88\u606f\u961f\u5217\u901a\u5e38\u7528\u4e8e\u5904\u7406\u9700\u8981\u7acb\u5373\u54cd\u5e94\u7684\u6d88\u606f\uff0c\u5982\u6d88\u606f\u901a\u77e5\u7b49\u3002\u800c\u4efb\u52a1\u961f\u5217\u5219\u7528\u4e8e\u5f02\u6b65\u5904\u7406\u90a3\u4e9b\u9700\u8981\u65f6\u95f4\u548c\u8ba1\u7b97\u529b\u7684\u4efb\u52a1\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728ThinkPHP6\u4e2d\uff0c\u4efb\u52a1\u961f\u5217\u7b80\u79f0\u4e3a\u201c\u961f\u5217\u201d\uff0c\u5b83\u7531\u6d88\u606f\u961f\u5217\u9a71\u52a8\u548c\u65e5\u5fd7\u8bb0\u5f55\u540e\u7aef\u7ec4\u6210\u3002 \u4e8c\u3001\u961f\u5217\u6280\u672f\u4f7f\u7528\u6b65\u9aa4 \u5b89\u88c5\u6d88\u606f\u961f\u5217\u9a71\u52a8 \u6d88\u606f\u961f\u5217\u9a71\u52a8\u662f\u7528\u4e8e\u5b58\u653e\u548c\u5904\u7406\u4efb\u52a1\u961f\u5217\u7684\u670d\u52a1\u7aef\u7a0b\u5e8f\u3002\u5728ThinkPHP6\u4e2d\uff0c\u652f\u6301\u591a\u79cd\u6d88\u606f\u961f\u5217\u9a71\u52a8\uff0c\u5982Redis\u3001MySQL\u7b49\u3002 \u4ee5Redis\u6d88\u606f\u961f\u5217\u4e3a\u4f8b\uff0c\u5728ThinkPHP6\u4e2d\u9700\u8981\u5b89\u88c5\u201ctopthink\/think-queue\u201d\u6269\u5c55\u5305\u548c\u201cpredis\/predis\u201d\u6269\u5c55\u5305\u3002\u5b89\u88c5\u547d\u4ee4\u5982\u4e0b\uff1a composer require topthink\/think-queuecomposer require predis\/predis \u914d\u7f6e\u961f\u5217\u53c2\u6570 \u5728ThinkPHP6\u4e2d\uff0c\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u961f\u5217\u53c2\u6570\uff0c\u5305\u62ec\u9a71\u52a8\u7c7b\u578b\u3001\u9a71\u52a8\u53c2\u6570\u3001\u961f\u5217\u540d\u79f0\u7b49\u3002\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\u4e3a\u201cconfig\/queue.php\u201d\u3002 \u914d\u7f6e\u793a\u4f8b\uff1a return [ \/\/ \u9ed8\u8ba4\u961f\u5217\u9a71\u52a8\uff1aRedis &#8216;default&#8217; =&amp;gt; &#8216;redis&#8217;, \/\/ \u961f\u5217\u8fde\u63a5\u53c2\u6570 &#8216;connections&#8217; =&amp;gt; [ &#8216;redis&#8217; =&amp;gt; [ &#8216;driver&#8217; =&amp;gt; &#8216;redis&#8217;, &#8216;queue&#8217; =&amp;gt; &#8216;default&#8217;, \/\/ \u961f\u5217\u540d\u79f0 &#8216;connection&#8217; =&amp;gt; &#8216;default&#8217;, &#8216;host&#8217; =&amp;gt; &#8216;127.0.0.1&#8217;, &#8216;port&#8217; =&amp;gt; 6379, &#8216;password&#8217; =&amp;gt; &#8221;, &#8216;database&#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-23614","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23614","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=23614"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23614\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}