https://www.yiichina.com/tutorial/1635php
https://my.oschina.net/gcdong/blog/3031113redis
https://www.yii-china.com/extend/detail/4bootstrap
yii2-queueyii2
安装
首先得安装,用的是composer,这个很简单,在你的项目目录下面
composer require --prefer-dist yiisoft/yii2-queueapp
##配置composer
找到你的项目配置文件,高级版是main.php,普通版console.php,在里面加上你的配置,这个yii2-queue能够使用各类驱动模式来作,我这里选取了redis,因此你得必须先有redis,若是没有能够用file来驱动yii
// 把这个组件注册到控制台
'bootstrap' => [
'queue',
],
'components' => [
'queue' => [
'class' => \yii\queue\redis\Queue::class,
// 链接组件或它的配置
'redis' => 'redis',
// Queue channel key
'channel' => 'queue',
],
'queue' => [
'class' => \yii\queue\file\Queue::class,
// 链接组件或它的配置
'as log' => \yii\queue\LogBehavior::class,
'path' => '@runtime/queue',
],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 0,
]
]
使用
首先咱们得写一个队里文件,里面执行一个队里的任务,我这里简单的写了一个ide
<?php
namespace common\components;
use yii\base\BaseObject;ui
class Job extends BaseObject implements \yii\queue\JobInterface
{
public $url;
public $file;this
public function execute($queue)
{
file_put_contents($this->file, file_get_contents($this->url));
}
}
上面是队列执行的文件,咱们要使用它是这样使用的,通常在控制器里面使用
use common\components\Job;
public function actionJob(){
//添加任务到队列
Yii::$app->queue->push(new Job([
'url' => 'http://img.netpc.com.cn/2018/06/12080502tmd.jpg',
'file' => '/www/project/ideaboat\1.jpg',
]));
}
// 延时5s触发
Yii::$app->queue->delay(5)->push(new Job([
'url' => 'http://img.netpc.com.cn/2018/06/12080502tmd.jpg',
'file' => '/www/project/ideaboat\1.jpg',
]));
最后你还得在命令行把这个队列的监听监听起来
./yii queue/listen这样就能够了