redis-react

(要求Workerman版本>=3.3.6)

安装:

  1. composer require clue/redis-react

示例:

  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. use Clue\React\Redis\Factory;
  4. use Clue\React\Redis\Client;
  5. use Workerman\Worker;
  6. $worker = new Worker('text://0.0.0.0:6161');
  7. // 进程启动时
  8. $worker->onWorkerStart = function() {
  9. global $factory;
  10. $loop = Worker::getEventLoop();
  11. $factory = new Factory($loop);
  12. };
  13. $worker->onMessage = function($connection, $data) {
  14. global $factory;
  15. $factory->createClient('localhost:6379')->then(function (Client $client) use ($connection) {
  16. $client->set('greeting', 'Hello world');
  17. $client->append('greeting', '!');
  18. $client->get('greeting')->then(function ($greeting) use ($connection){
  19. // Hello world!
  20. echo $greeting . PHP_EOL;
  21. $connection->send($greeting);
  22. });
  23. $client->incr('invocation')->then(function ($n) use ($connection){
  24. echo 'This is invocation #' . $n . PHP_EOL;
  25. $connection->send($n);
  26. });
  27. });
  28. };
  29. Worker::runAll();

文档:

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

注意:

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

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