Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
Swoole Redis server implements the Redis server side protocols based on Swoole TCP Server.
The class Swoole\Redis\Server
inherits from the class Swoole\Server
.
It can call the methods of class Swoole\Server
and its own methods.
The object of class Swoole\Redis\Server
doesn't need to register the callback function for event receive
. It needs to use the method setHandler
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'
.
Swoole\Redis\Server::setHandler ( string $command , string $callback [, string $number_of_string_param [, string $type_of_array_param ]] )
Alias: swoole_redis_server->setHandler( string $command , string $callback [, string $number_of_string_param [, string $type_of_array_param ]] )
<?php
Swoole\Redis\Server->setHandler(string $command, callable $callback)
Set the handling function of redis command.
$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 Swoole\Redis\Server::format
to make the return data.
<?php
$server = new Swoole\Redis\Server('127.0.0.1', 9501);
//synchronous mode
$server->setHandler('Set', function($fd, $data) {
$server->array($data[0], $data[1]);
return Swoole\Redis\Server::format(Server::INT, 1);
});
//asynchronous mode
$server->setHandler('Get', function ($fd, $data) use ($server) {
$db->query($sql, function($db, $result) use ($fd) {
$server->send($fd, Server::format(Server::LIST, $result));
});
});
$server->start();
Swoole\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
$format
the type of data. NIL
, ERROR
, STATUS
, INT
, STRING
, SET
, MAP
The constans for type
parameter of swoole_redis_server::format
The swoole redis server doesn't need to set the callback function for event
receive
.
<?php
use Swoole\Redis\Server;
define('DB_FILE', '/tmp/redis.data');
$server = new Server("127.0.0.1", 9501, SWOOLE_BASE);
if (is_file(DB_FILE))
{
$server->data = unserialize(file_get_contents(DB_FILE));
}
else
{
$server->data = array();
}
$server->setHandler('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->setHandler('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->setHandler('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->setHandler('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->setHandler('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->setHandler('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();