Swoole Async Redis client

Notice: the documentation is suitable for PHP extension swoole until v4.7.1, we only support PHP extension openswoole from v4.7.1

This class or function have been deprecated.

Swoole async redis client is based on hiredis.

Install hiredis library
wget https://github.com/redis/hiredis/archive/v0.13.3.tar.gz
tar zxvf v0.13.3.tar.gz
cd hiredis-0.13.3/
make -j
sudo make install
sudo ldconfig
Compile hiredis library into Swoole extension
./configure --enable-async-redis
make clean
make -j
sudo make install
Events
  • message
  • close
Swoole\Redis::connect(string $host, int $port, callable $callback)

Alias: swoole_redis->connect(string $host, int $port, callable $callback)

Connect to the Redis server.

Example:

<?php
$client = new swoole_redis;
$client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) {
    if ($result === false) {
        echo "connect to redis server failed.\n"
        return;
    }
    $client->set('key', 'swoole', function (swoole_redis $client, $result) {
        var_dump($result);
    });
});
Swoole\Redis::on(string $event_name, callable $callback)

Alias: swoole_redis->on(string $event_name, callable $callback)

Register callback function based on event name: Close and Message, Receive.

Example:

<?php
$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');
});
Swoole\Redis::\_\_call(string $command, array $params)

Alias: swoole_redis->__call(string $command, array $params)

Execute redis client commands: http://redis.io/commands

Commands:

  • subscribe
  • psubscribe
  • unsubscribe
  • punsubscribe
Swoole\Redis::close()

Alias: swoole_redis->close()

Close the redis connection.