默认Redis配置是禁用的、需要修改配置文件

图

notify-keyspace-events "Ex"

也可以用临时命令

config set notify-keyspace-events Ex

图

K

命令格式为:__keyspace@__ 为前缀

E

命令格式为:__keyevent@__ 为前缀

Redis客户端操作

127.0.0.1:6379> subscribe __keyevent@0__:expired

图

127.0.0.1:6379> psubscribe __keyevent@*__:expired

php代码使用方式psubscribe和subscribe

$channel="__keyevent@0__:expired";
$p_channel="__keyevent@*__:expired";
$sub_client=new Redis();
$sub_client->connect("127.0.0.1",6379);
$sub_client->select(0);
function p_call($redis, $pattern, $chan, $msg){
    var_dump($redis);
    echo PHP_EOL;
    var_dump($pattern);
    echo PHP_EOL;
    var_dump($chan);
    echo PHP_EOL;
    var_dump($msg);
    echo PHP_EOL;
}
function call($redis, $chan, $msg){
    var_dump($redis);
    echo PHP_EOL;
    var_dump($chan);
    echo PHP_EOL;
    var_dump($msg);
    echo PHP_EOL;
}
$sub_client->psubscribe([$p_channel],'p_call');
$sub_client->subscribe([$channel],'call');
Last modification:February 10, 2020
如果觉得我的文章对你有用,请随意赞赏