Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Swoole Redis Server instance


May 14, 2021 Swoole


Table of contents


Introduction to the Swoole Redis/Server asynchronous client

The Swoole-1.8.14 version adds a Server framework compatible with the Redis server-side protocol, which enables server programs that implement the Redis protocol based on this framework. Swoole\Redis\Server from Swoole\Server all methods provided by the parent class.

Redis\Server set the onReceive Instance program: Https://github.com/swoole/swoole-src/blob/master/examples/redis/server.php

Available clients

  • Redis clients for any programming language, including PHP's redis extension and phpredis library
  • The Swoole extension provides asynchronous Redis clients
  • Redis provides command-line tools, redis-cli redis-benchmark
Note: Swoole-1.8.0 support for asynchronous Redis clients, based on the hiredis library implementation officially available by redis. S woole provides __call __call method to map the vast majority of Redis instructions.

Compile and installhiredis

With the Redis client, thehiredis library needs to be installed. After hiredis source code, execute

make -j
sudo make install
sudo ldconfig

Enable asynchronous Redis clients

Compilation swoole is to add --enable-async-redis configure

./configure --enable-async-redis
make clean
make -j
sudo make install

A simple example:

$redis = new Swoole\Redis;
$redis->connect('127.0.0.1', 6379, function ($redis, $result) {
    $redis->set('test_key', 'value', function ($redis, $result) {
        $redis->get('test_key', function ($redis, $result) {
            var_dump($result);
        });
    });
});

$cli = new Swoole\Http\Client('127.0.0.1', 80);
$cli->setHeaders(array('User-Agent' => 'swoole-http-client'));
$cli->setCookies(array('test' => 'value'));

$cli->post('/dump.php', array("test" => 'abc'), function ($cli) {
    var_dump($cli->body);
    $cli->get('/index.php', function ($cli) {
        var_dump($cli->cookies);
        var_dump($cli->headers);
    });
});