laravel發佈訂閱

一、php artisan make:command RedisSubscribephp

 在app console中會生成RedisSubscribe.php文件redis

  

<?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;
});
//
}
}

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

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

四、修改redis配置執行時間
在config/database.php的redis配置中添加
'read_write_timeout' => 0,//new參考:https://www.jianshu.com/p/7eef26de7c45callback參考:https://www.jianshu.com/p/41677357d736
相關文章
相關標籤/搜索