title: Redis coroutine client meta:

  • name: description content: Redis coroutine client,Implemented by swoole coroutine client,Covers the method of redis 99%
  • name: keywords content: swoole|swoole extension|swoole framework|EasySwoole redis| Swoole Redis coroutine client|swoole Redis|Redis coroutine

Subscription/release method

Method name Parameter Description Notes
pSubscribe $callback, $pattern, …$patterns Subscribe to one or more channels that match a given pattern. $callback is a callback function
pubSub $subCommand, …$arguments View subscription and release system status.
publish $channel, $message Send the message to the specified channel.
pUnSubscribe $pattern, …$patterns Unsubscribe from all channels in a given mode.
subscribe $callback, $channel, …$channels Subscribe to information for a given channel or channels.
unsubscribe $channel, …$channels Refers to unsubscribing to a given channel.
setSubscribeStop bool $subscribeStop Set whether to opt out of the subscription Call this command when your callback function wants to exit
isSubscribeStop View current subscription status

Instance

  1. defined("REDIS_HOST") ?: define('REDIS_HOST', '127.0.0.1');
  2. defined("REDIS_PORT") ?: define('REDIS_PORT', 6379);
  3. defined("REDIS_AUTH") ?: define('REDIS_AUTH', 'easyswoole');
  4. go(function () {
  5. $redis = new \EasySwoole\Redis\Redis(new \EasySwoole\Redis\Config\RedisConfig([
  6. 'host' => '127.0.0.1',
  7. 'port' => '6379',
  8. 'auth' => 'easyswoole',
  9. 'serialize' => \EasySwoole\Redis\Config\RedisConfig::SERIALIZE_NONE
  10. ]));;
  11. //Open a new coroutine to subscribe
  12. go(function () {
  13. $redis = new \EasySwoole\Redis\Redis(new \EasySwoole\Redis\Config\RedisConfig([
  14. 'host' => REDIS_HOST,
  15. 'port' => REDIS_PORT,
  16. 'auth' => REDIS_AUTH
  17. ]));
  18. $redis->pSubscribe(function (\EasySwoole\Redis\Redis $redis, $pattern, $str) {
  19. var_dump($pattern,$str);
  20. $data = $redis->unsubscribe('test');
  21. var_dump($data);
  22. $redis->setSubscribeStop(true);
  23. }, 'test', 'test1', 'test2');
  24. });
  25. //Open a new coroutine to subscribe
  26. go(function () {
  27. $redis = new \EasySwoole\Redis\Redis(new \EasySwoole\Redis\Config\RedisConfig([
  28. 'host' => REDIS_HOST,
  29. 'port' => REDIS_PORT,
  30. 'auth' => REDIS_AUTH
  31. ]));
  32. $redis->subscribe(function (\EasySwoole\Redis\Redis $redis, $pattern, $str) {
  33. var_dump($pattern,$str);
  34. $data = $redis->unsubscribe('test');
  35. var_dump($data);
  36. $redis->setSubscribeStop(true);
  37. }, 'test', 'test1', 'test2');
  38. });
  39. $data = $redis->pubSub('CHANNELS');
  40. var_dump($data);
  41. \Swoole\Coroutine::sleep(1);
  42. $data = $redis->publish('test2', 'test');
  43. var_dump($data);
  44. $data = $redis->pUnSubscribe('test');
  45. var_dump($data);
  46. });