ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` //创建适配器,适配器有多种Database,Redis,Sync,Topthink. 自动根据配置加载对应的适配器类 private static function buildConnector() { $options = Config::get('queue'); //配置文件在application\extra\queue.php $type = !empty($options['connector']) ? $options['connector'] : 'Sync'; if (!isset(self::$connector)) { $class = false !== strpos($type, '\\') ? $type : '\\think\\queue\\connector\\' . Str::studly($type); self::$connector = new $class($options); } return self::$connector; } //调用对应适配器里面的静态方法 public static function __callStatic($name, $arguments) { return call_user_func_array([self::buildConnector(), $name], $arguments); } ``` 使用 [魔术方法](https://www.php.net/manual/zh/language.oop5.overloading.php#object.callstatic) __callStatic调用这个类中不存在的静态函数; 主要是这几个函数 \* @method static push($job, $data = '', $queue = null) //创建任务后立刻执行 \* @method static later($delay, $job, $data = '', $queue = null) //创建任务后N秒再执行 \* @method static pop($queue = null) // \* @method static marshal() 例子: ``` //创建一个任务,延迟10秒后执行 $test= \think\Queue::later(10, '\addons\shopro\job\InterestJob@execCycleUserInterestJob',$data, 'shopro'); ``` 在调用later()时并不存在这个函数,而是调用__callStatic,进而调用相应的适配器里面的later函数; 然后用redis管理器查看redis里面的数据: ![](https://img.kancloud.cn/f0/9d/f09dfef10f5c18fcb0c256547dc29bd2_400x158.png) 可以看到'目录'层次是 queues: TP的队列 , shopro:队列名称,delayed:延迟类任务 ; 在delayed里面就是保存着所有延迟任务的数据; ``` {"job":"\\addons\\shopro\\job\\InterestJob@execCycleUserInterestJob","data":{"kind":"cycle","cycle":"month"},"id":"lgrwTERrEwJpwIYJlvu0p8OwhTBT9WWP","attempts":1} ``` job:要执行的任务目标类和函数; data一般是json ,用的时候转回数组; id:随机32位字符串; attempts:任务重试次数,默认是1(因为在创建,就设定为1)