workerman/redis 键超时订阅不成功,请大家帮看下代码哪里有问题? redis连接正常

xinsnd
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Redis\Client;
use Workerman\Timer;
use Workerman\Worker;

global $redis, $redis1;

$worker = new Worker("http://0.0.0.0:8111");

function timer_test(){
    echo "\ntimer is running.";
    global $redis1;
    $redis1->setEx("test_key", 5, "123");
    $redis1->setEx(
}
function expire_test($redis, $pattern, $channel, $message) {
    echo "$pattern, $channel, $message"; 
}
$worker->onWorkerStart = function($worker) {
    global $cfg, $redis1, $redis;

    if($worker->id == 0){
        Timer::add(10, "timer_test");
    }

    $redis1 = new Client('redis://127.0.0.1:6379');
    $redis2 = new Client('redis://127.0.0.1:6379');

    $redis2->psubscribe(array("__keyevent@0__:expired"), "expire_test");
};

$worker->onMessage = function($connection, $data) {
};

Worker::runAll();
2231 2 0
2个回答

walkor 打赏

array("__keyevent@0__:expired") 改成只有字母的字符串试下。比如 array("test")

  • xinsnd 2021-02-13

    __keyevent@0__:expired是redis规定的key超时订阅,改成一般字符串起不到效果

  • walkor 2021-02-16

    找到 redis.conf,把 notify-keyspace-events 设置为 Ex,重启redis。

  • walkor 2021-02-16

    我本地测试是ok的。另外 expire_test参数顺序为 ( $pattern, $channel, $message, $redis),最后一个参数是redis实例

xinsnd

redis.conf配置是开启了的,应该是参数的顺序搞错了,我再测一下,感谢!祝新年快乐!

  • 暂无评论
年代过于久远,无法发表回答
×
🔝