swoole_redis->__call

魔术方法,方法名会映射为Redis指令,参数作为Redis指令的参数。

函数原型

function swoole_redis->__call(string $command, array $params);

订阅/发布消息

Redis服务器除了作为内存存储之外,还可以作为一个消息通道服务器。SwooleRedis客户端也支持了Redis的订阅/发布消息指令。

与普通的存储指令不同,消息订阅/发布指令不是请求响应式的。

$client = new swoole_redis;
$client->on('message', function (swoole_redis $client, $result) {
    var_dump($result);
    static $more = false;
    if (!$more and $result[0] == 'message')
    {
        echo "subscribe new channel\n";
        $client->subscribe('msg_1', 'msg_2');
        $client->unsubscribe('msg_0');
        $more = true;
    }
});
$client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) {
    echo "connect\n";
    $client->subscribe('msg_0');
});

回调函数

function callback(swoole_redis $redis, bool $result);

使用示例

$client->get('key', function (swoole_redis $client, $result) {
	var_dump($result);
});