laravel发布订阅

1、php artisan make:command RedisSubscribe

 在app console中会生成RedisSubscribe.php文件

  

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class RedisSubscribe extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'redis:subscribe';//自行修改

/**
* The console command description.
*
* @var string
*/
protected $description = 'Subscribe to a Redis channel';//自行修改

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
Redis::subscribe(['test-channel'], function ($message) {
echo $message;
});
//
}
}

2、用php Artisan生产命令
  1).在Kernel中添加配置:
    protected $commands = [
    Commands\RedisSubscribe::class,
  //
    ];

  2).php artisan make:command subscribe
3、在控制器里面使用方法:
  
Redis::publish('test-channel', "sdsdsadad")

4、修改redis配置执行时间
在config/database.php的redis配置中添加
'read_write_timeout' => 0,//new


参考:https://www.jianshu.com/p/7eef26de7c45
callback参考:https://www.jianshu.com/p/41677357d736

posted on 2018-09-17 12:18  我很迷茫  阅读(875)  评论(0编辑  收藏  举报