Thinkphp6 baiy/think-async redis 异步代码执行/异步延迟执行/异步事件订阅

74 篇文章 0 订阅

配置文件async.php

<?php

use Baiy\ThinkAsync\Subscribe\ConfigFileEventGetter;
use Baiy\ThinkAsync\Subscribe\DemoSubscriber;
use crmeb\subscribes\DemoAsyncSubscriber;
use crmeb\subscribes\DemoAsyncSubscriber01;

return [
    // 异步执行默认队列
    'async_exec_method_queue'        => 'async_exec_method',
    // 异步执行自定义队列
    'async_exec_method_custom_queue' => [
        'async_exec_method_custom' => '自定义异步执行队列',
        'async_exec_method_custom01' => '自定义异步执行队列01',
    ],

    // 异步订阅默认队列名称
    'subscribe_default_queue'        => 'subscribe_default',
    // 异步订阅事件获取类
    'subscribe_event_get_class'      => ConfigFileEventGetter::class,
    // 异步订阅事件配置(可通过修改`subscribe_event_get_class`改变配置来源)
    'subscribe_event_config'         => [
        [
            'name'       => 'demo',
            'title'      => '演示事件',
            'queue'      => 'async_subscribe_demo', // 事件处理队列 为空使用异步订阅默认队列
            // 事件订阅者配置
            'subscriber' => [
                [DemoSubscriber::class, 'handle'],
            ]
        ],
        [
            'name'       => 'zidingyishijian',
            'title'      => '自定义事件',
            'queue'      => 'async_subscribe_zidingyi',
            // 事件订阅者配置
            'subscriber' => [
                [DemoAsyncSubscriber::class, 'handle'],
                [DemoAsyncSubscriber01::class, 'handle'],
            ]
        ],
    ]
];

 

异步代码执行/异步延迟执行/异步事件订阅文件PublicController.php

<?php

namespace app\api\controller\demo;

use Baiy\ThinkAsync\Facade\Async;

/**
 * 公共类
 * Class PublicController
 * @package app\api\controller
 */
class PublicController
{
    //异步代码执行/异步延迟执行/异步事件订阅
    //http://meishu.local/api/demo/demoasync
    public function demoasync(){
        // 异步执行默认队列
        //Async::exec(self::class, 'test', 'exec');
        //异步执行代码使用自定义队列
        //Async::execUseCustomQueue(self::class, 'test','async_exec_method_custom', 'exec');
        //Async::execUseCustomQueue(self::class, 'test','async_exec_method_custom01', 'exec');
        // 异步延迟执行默认队列 延迟10秒
        //Async::delay(10, self::class, 'test', 'delay');
        // 异步延迟执行默认队列 延迟10秒
        //Async::delayUseCustomQueue(10, self::class, 'test','async_exec_method_custom', 'delay');
        // 获取所有队列标示
        //var_dump(Async::queue());
        // 获取队列长度
        //var_dump(Async::queueSize('async_exec_method_custom'));
        // 获取队列名称
        //var_dump(Async::queueName('async_exec_method_custom'));
        
        //事件订阅
        //事件触发
        Async::trigger('zidingyishijian',33,'lisi');
        echo 11;
        
    }

    public static function test()
    {
        $arr['name'] = mt_rand(111111,999999);
        $res = Db::name('demo')->insert($arr);
    }
}

事件订阅者DemoAsyncSubscriber.php

<?php

namespace crmeb\subscribes;
use think\facade\Db;

class DemoAsyncSubscriber

{
    public static function handle($id,$name){
        $arr['name'] = $id.$name;
        $res = Db::name('demo')->insert($arr);
    }
    
}
 

事件订阅者DemoAsyncSubscriber01.php

<?php

namespace crmeb\subscribes;
use think\facade\Db;

class DemoAsyncSubscriber01

{
    public static function handle($id,$name){
        $arr['name'] = $name.$id;
        $res = Db::name('demo')->insert($arr);
    }
    
}

 

执行命令:

php think queue:work --queue async_subscribe_zidingyi

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅坞茶坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值