OpenSwoole Redis Server

Latest version: pecl install openswoole-22.0.0 | composer require openswoole/core:22.1.1

OpenSwoole Redis server implements the Redis server side protocols based on OpenSwoole TCP Server.

The class OpenSwoole\Redis\Server inherits from the class OpenSwoole\Server.

It can call the methods of class OpenSwoole\Server and its own methods.

The object of class OpenSwoole\Redis\Server doesn't need to register the callback function for event receive. It needs to use the method addCommand to set the corresponding function fo redis command. If the swoole redis server receives the command that hasn't the corresponding function to handle, it responds the message ERR unknown command '$command'.

OpenSwoole\Redis\Server::addCommand ( string $command , string $callback [, string $number_of_string_param [, string $type_of_array_param ]] )

Alias: swoole_redis_server->addCommand( string $command , string $callback [, string $number_of_string_param [, string $type_of_array_param ]] )

Prototype
<?php
OpenSwoole\Redis\Server->addCommand(string $command, callable $callback)

Set the handling function of redis command.

Parameter
  • $command the redis command

  • $callback the function to handle the redis command. The return data of this function must is in form of Redis. You can the method OpenSwoole\Redis\Server::format to make the return data.

Example
<?php
$server = new OpenSwoole\Redis\Server('127.0.0.1', 9501);

//synchronous mode
$server->addCommand('Set', function($fd, $data) {
    $server->array($data[0], $data[1]);
    return OpenSwoole\Redis\Server::format(Server::INT, 1);
});

//asynchronous mode
$server->addCommand('Get', function ($fd, $data) use ($server) {
    $db->query($sql, function($db, $result) use ($fd) {
        $server->send($fd, Server::format(Server::LIST, $result));
    });
});

$server->start();
OpenSwoole\Redis\Server::format ( string $type [, string $value ] )

Alias: swoole_redis_server::format( string $type [, string $value ] )

<?php
Swoole\Redis\Server::format(int $type, mixed $value = null);

Format the data to the format of redis

Parameter
  • $format the type of data. NIL, ERROR, STATUS, INT, STRING, SET, MAP
Constants

The constans for type parameter of swoole_redis_server::format

  • Server::NIL return nil data
  • Server::ERROR return error code
  • Server::STATUS return status data
  • Server::INT return integer
  • Server::STRING return strung
  • Server::SET return sequential array
  • Server::MAP return associative array

The swoole redis server doesn't need to set the callback function for event receive.

Example
<?php
use OpenSwoole\Redis\Server;
define('DB_FILE', '/tmp/redis.data');
$server = new Server("127.0.0.1", 9501, OpenSwoole\Server::SIMPLE_MODE);
if (is_file(DB_FILE))
{
    $server->data = unserialize(file_get_contents(DB_FILE));
}
else
{
    $server->data = array();
}
$server->addCommand('GET', function ($fd, $data) use ($server) {
    if (count($data) == 0)
    {
        return Server::format(Server::ERROR, "ERR wrong number of arguments for 'GET' command");
    }

    $key = $data[0];
    if (empty($server->data[$key]))
    {
        return Server::format(Server::NIL);
    }
    else
    {
        return Server::format(Server::STRING, $server->data[$key]);
    }
});
$server->addCommand('SET', function ($fd, $data) use ($server) {
    if (count($data) < 2)
    {
        return Server::format(Server::ERROR, "ERR wrong number of arguments for 'SET' command");
    }
    $key = $data[0];
    $server->data[$key] = $data[1];
    return Server::format(Server::STATUS, 'OK');
});
$server->addCommand('sAdd', function ($fd, $data) use ($server) {
    if (count($data) < 2)
    {
        return Server::format(Server::ERROR, "ERR wrong number of arguments for 'sAdd' command");
    }
    $key = $data[0];
    if (!isset($server->data[$key]))
    {
        $array[$key] = array();
    }
    $count = 0;
    for($i = 1; $i < count($data); $i++)
    {
        $value = $data[$i];
        if (!isset($server->data[$key][$value]))
        {
            $server->data[$key][$value] = 1;
            $count ++;
        }
    }
    return Server::format(Server::INT, $count);
});
$server->addCommand('sMembers', function ($fd, $data) use ($server) {
    if (count($data) < 1)
    {
        return Server::format(Server::ERROR, "ERR wrong number of arguments for 'sMembers' command");
    }
    $key = $data[0];
    if (!isset($server->data[$key]))
    {
        return Server::format(Server::NIL);
    }
    return Server::format(Server::SET, array_keys($server->data[$key]));
});
$server->addCommand('hSet', function ($fd, $data) use ($server) {
    if (count($data) < 3)
    {
        return Server::format(Server::ERROR, "ERR wrong number of arguments for 'hSet' command");
    }
    $key = $data[0];
    if (!isset($server->data[$key]))
    {
        $array[$key] = array();
    }
    $field = $data[1];
    $value = $data[2];
    $count = !isset($server->data[$key][$field]) ? 1 : 0;
    $server->data[$key][$field] = $value;
    return Server::format(Server::INT, $count);
});
$server->addCommand('hGetAll', function ($fd, $data) use ($server) {
    if (count($data) < 1)
    {
        return Server::format(Server::ERROR, "ERR wrong number of arguments for 'hGetAll' command");
    }
    $key = $data[0];
    if (!isset($server->data[$key]))
    {
        return Server::format(Server::NIL);
    }
    return Server::format(Server::MAP, $server->data[$key]);
});
$server->on('WorkerStart', function ($server) {
    $server->tick(10000, function() use ($server) {
        file_put_contents(DB_FILE, serialize($server->data));
    });
});
$server->start();
Last updated on September 21, 2022