默认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
如果要在监听所有库那么要用的psubscribe 命令监听p是指的pattern。区别在于这个命令支持正则表达式
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');