ÔõÑùʹÓÃThinkPHP6¾ÙÐÐÓʼþÐÐÁд¦Àí£¿
Ëæ×ÅwebÓ¦ÓóÌÐòµÄÔö¶à£¬¹ØÓÚ·¢ËÍÓʼþµÄÐèÇóÒ²Ô½À´Ô½´ó¡£ÔÚijЩÇéÐÎÏ£¬ÐèÒªÅúÁ¿·¢ËÍÓʼþ»ò½«Óʼþ·¢Ë͵½ÐÐÁÐÖд¦ÀíÒÔÌá¸ßÐÔÄÜ¡£thinkphp 6ÖÐÌṩÁËÀû±ãµÄÓʼþ·¢ËͺÍÐÐÁд¦Àí¹¦Ð§£¬±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃthinkphp 6¾ÙÐÐÓʼþÐÐÁд¦Àí¡£
Ò»¡¢×°Öò¢ÉèÖÃÐÐÁÐЧÀÍ
1.×°ÖÃRedis
RedisÊÇÒ»¸ö¿ªÔ´µÄÄÚ´æÊý¾Ý½á¹¹´æ´¢Ð§ÀÍÆ÷£¬ÓÃ×÷Êý¾Ý¿â¡¢»º´æºÍÐÂÎÅÊðÀí¡£ÓÉÓÚÐÐÁеÄÊý¾Ý±ØÐèÒª³¤ÆÚ»¯£¬ÒÔÊÇÐèҪͨ¹ýRedisÀ´ÉúÑÄÐÐÁÐÊý¾Ý¡£×°ÖÃRedis¿ÉÒԲο¼¹Ù·½Îĵµ¼°ÆäËûÔÚÏ̡߽̳£
2.ÉèÖÃÐÐÁÐÅþÁ¬
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚThinkPHPµÄÉèÖÃÎļþconfig/queue.phpÖÐÉèÖÃÐÐÁÐÅþÁ¬£¬Ê¾ÀýÈçÏ£º
return [ // ĬÈÏÇý¶¯ 'default' => env('queue.driver', 'redis'), // ÐÐÁÐÅþÁ¬²ÎÊý 'connections' => [ 'sync' => [ 'driver' => 'sync', ], 'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => env('queue.redis.queue', 'default'), 'retry_after' => 90, 'block_for' => null, ], ], // ¼àÌýµÄʹÃüÀàÃüÃû¿Õ¼ä 'queue_class' => [ // 'AppJobs' ], ];
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬defaultÊÇĬÈϵÄÐÐÁÐÇý¶¯£¬ÕâÀïÉèÖÃΪredis¡£connectionsÖÐÉèÖÃÁËredisÇý¶¯µÄÏà¹Ø²ÎÊý£¬°üÀ¨ÅþÁ¬Ãû¡¢ÅþÁ¬Çý¶¯¡¢ÅþÁ¬µÄqueueÐÐÁÐÃûµÈ¡£queue_classÓÃÀ´¼àÌýÖ´ÐÐʹÃüµÄÀàµÄÃüÃû¿Õ¼ä¡£
3.Æô¶¯ÐÐÁмàÌý
ÔÚÏÂÁîÐÐÖÐÆô¶¯ÐÐÁмàÌýÆ÷£¬¿ÉÒÔ½«ÐÐÁÐÖеÄʹÃüÖðһȡ³ö²¢Ö´ÐС£Ê¾ÀýÈçÏ£º
php think queue:listen
µÇ¼ºó¸´ÖÆ
¶þ¡¢Ê¹ÓÃÐÐÁз¢ËÍÓʼþ
1.½¨ÉèÓʼþ·¢ËÍʹÃü
ÔÚapp/jobĿ¼Öн¨ÉèÒ»¸öÓʼþ·¢ËÍʹÃüÀ࣬²¢±àдÏà¹ØÂß¼¡£ÀýÈç·¢ËÍÓʼþµÄʹÃüÀàSnedMailJob£º
<?php namespace appjob; use appcommonMail; use thinkqueueJob; class SendMailJob { /** * Send the email message. * * @param Job $job * @param array $data email message data */ public function fire(Job $job, $data) { try { // ·¢ËÍÓʼþ Mail::send($data['to'], $data['subject'], $data['content']); // Ö´ÐÐʹÃüÀֳɣ¬É¾³ýʹÃü $job->delete(); } catch (Exception $e) { // Ö´ÐÐʹÃüʧ°Ü£¬ÖØзÅÈëʹÃüÐÐÁÐÖÐ // ϵͳ»á×Ô¶¯Ð½¨Ò»¸ö¿ÉÖØÊÔʹÃü²¢·ÅÈëÐÐÁУ¬¸ÃʹÃü¿¢ÊºóÖØÐÂʵÑéÖ´ÐÐÄ¿½ñʹÃü $job->release(); // »òÕß $job->failed(); } } }
µÇ¼ºó¸´ÖÆ
2.Ìí¼ÓʹÃüµ½ÐÐÁÐ
ÔÚÐèÒª·¢ËÍÓʼþµÄµØ·½£¬Í¨¹ýÒÔÏ´úÂ뽫ʹÃüÌí¼Óµ½ÐÐÁÐÖУº
use thinkacadeQueue; // Ìí¼ÓÒ»ÌõSendMailJobʹÃüµ½ÐÐÁÐÖÐ Queue::push(new SendMailJob($to, $subject, $content));
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬$to¡¢$subject¡¢$contentΪÓʼþµÄÎüÊÕÕß¡¢Ö÷ÌâºÍÄÚÈÝ¡£
3.ÐÐÁмàÌýÆ÷Ö´ÐÐʹÃü
Æô¶¯ÐÐÁмàÌýÆ÷ºó£¬¾Í»á×Ô¶¯´ÓÐÐÁÐÖÐÈ¡³öʹÃü²¢Ö´ÐС£Ö´ÐÐÀֳɺó£¬Ê¹Ãü»á×ÔÐдÓÐÐÁÐÖÐɾ³ý¡£Ö´ÐÐʧ°Üºó£¬ÐÐÁмàÌýÆ÷»áÖØн«¸ÃʹÃüÍÆÈëÐÐÁÐÖУ¬Ö±µ½Ê¹ÃüÖ´ÐÐÀֳɻòµÖ´ï×î´óʵÑé´ÎÊý£¨¿ÉÔÚ.envÎļþÖоÙÐÐÉèÖã©¡£
Èý¡¢½áÓï
±¾ÎÄÏÈÈÝÁËʹÓÃThinkPHP6¾ÙÐÐÓʼþÐÐÁд¦ÀíµÄÒªÁ죬°üÀ¨×°Öò¢ÉèÖÃÐÐÁÐЧÀÍ¡¢½¨ÉèÓʼþ·¢ËÍʹÃü¡¢Ìí¼ÓʹÃüµ½ÐÐÁкÍÐÐÁмàÌýÆ÷Ö´ÐÐʹÃü¡£Í¨¹ýʹÓÃÓʼþÐÐÁУ¬¿ÉÒÔ½«ÓʼþʹÃüÊèÉ¢³öÓ¦ÓóÌÐò£¬Ìá¸ßÐÔÄܺͿɿ¿ÐÔ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐÐÓʼþÐÐÁд¦Àí£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡