1.15.5.1 react/redis

优质
小牛编辑
133浏览
2023-12-01

安装:

composer require clue/redis-react

注意:此组件为试验性质的第三方组件,可能有隐藏的bug,建议使用redis扩展。

示例:

require_once __DIR__ . '/vendor/autoload.php';
use ClueReactRedisFactory;
use ClueReactRedisClient;
use WorkermanWorker;

$worker = new Worker('text://0.0.0.0:6161');

// 进程启动时
$worker->onWorkerStart = function() {
    global $factory;
    $loop    = Worker::getEventLoop();
    $factory = new Factory($loop);
};

$worker->onMessage = function($connection, $data) {
    global $factory;
    $factory->createClient('localhost:6379')->then(function (Client $client) use ($connection) {
        $client->set('greeting', 'Hello world');
        $client->append('greeting', '!');

        $client->get('greeting')->then(function ($greeting) use ($connection){
            // Hello world!
            echo $greeting . PHP_EOL;
            $connection->send($greeting);
        });

        $client->incr('invocation')->then(function ($n) use ($connection){
            echo 'This is invocation #' . $n . PHP_EOL;
            $connection->send($n);
        });
    });
};

Worker::runAll();

文档:

https://github.com/clue/php-redis-react

注意:

1、所有的异步编码必须在onXXX回调中编写

2、异步客户端需要的$loop变量请使用Worker::getEventLoop();返回值