開啓redis.conf 文件中 notify-keyspace-events Ex
php
或者 在redis-cli 客戶端輸入以下命令:config set notify-keyspace-events Ex
laravel
//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,//長鏈接不要斷 ], ]
$redis=Redis::connection('publisher');//建立新的實例 $redis->psubscribe(['__keyevent@*__:expired'], function ($message, $channel) { echo $channel.PHP_EOL;//訂閱的頻道 echo $message.PHP_EOL;//過時的key echo '---'.PHP_EOL; });