1

1.修改redis 配置

开启redis.conf 文件中 notify-keyspace-events Ex

或者 在redis-cli 客户端输入如下命令:
config set notify-keyspace-events Ex

2.修改laravel database.php

//datebase.php配置页面
'redis' => [
        'client' => 'predis',
        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
            "queue" => '{default}',//queue站点默认走的redis
        ],
        'publisher' => [ //redis 订阅监听
                    'host' => env('REDIS_HOST', '127.0.0.1'),
                    'password' => env('REDIS_PASSWORD', null),
                    'port' => env('REDIS_PORT', 6379),
                    'database' => 0,
                    "read_write_timeout"=>0,//长连接不要断
                ],
           ]

3.在脚本的地方实现监听方法

        $redis=Redis::connection('publisher');//创建新的实例
        $redis->psubscribe(['__keyevent@*__:expired'], function ($message, $channel) {
            echo $channel.PHP_EOL;//订阅的频道
            echo $message.PHP_EOL;//过期的key
            echo '---'.PHP_EOL;
        });

mark
24 声望4 粉丝