tp6使用redis消息队列

使用composer安装队列包
composer require topthink/think-queue
安装完毕系统配置redis相关配置
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------

return [
    'default'     => 'redis',
    'connections' => [
        'sync'     => [
            'type' => 'sync',
        ],
        'database' => [
            'type'       => 'database',
            'queue'      => 'default',
            'table'      => 'jobs',
            'connection' => null,
        ],
        'redis'    => [
            'type'       => 'redis',
            'queue'      => 'default',
            'host'       => '127.0.0.1',
            'port'       => 6379,
            'password'   => '',
            'select'     => 1,
            'timeout'    => 0,
            'persistent' => false,
        ],
    ],
    'failed'      => [
        'type'  => 'none',
        'table' => 'failed_jobs',
    ],
];
编写入队代码
<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Queue;
/**
 * todo 提醒进入队列
 * @author zhuyanbin
 * @time 2023年9月23日15:21:48
 **/
class Notify extends BaseController {
    
    /**
     * 将客户资料加入到redis队列
     * php /www/wwwroot/oyjst.sdoumei.com/public/cli.php notify/nextContact
     **/
    public function nextContact() {
        $client = Db::table('fa_client')
            ->where('status','in',['已加微信','已加企微'])
            ->select();
        //dump($client);
        foreach ($client as $k => $v) {
            $jobHandlerClassName = 'app\job\Notify@nextContact';
            $jobQueueName = "nextContact";
            $isPushed = Queue::push($jobHandlerClassName, $v, $jobQueueName);
            dump($isPushed);
            exit();
        }
    }
}
消费队列代码
<?php
namespace app\job;

use app\BaseController;
use think\facade\Db;
use think\queue\Job;


class Notify extends BaseController {
    /**
     * 队列消费
     * php think queue:work --queue nextContact
     **/
    public function nextContact(Job $job, $data) {
        //dump($data);
        $job->delete();
    }
}
消费队列
php think queue:work --queue nextContact
监听消费
php think queue:listen --queue nextContact
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值