friendsofhyperf/redis-subscriber

基于 Swoole 协程的 Redis 原生协议订阅者

v3.1.40 2024-09-13 08:31 UTC

This package is auto-updated.

Last update: 2024-09-25 00:29:50 UTC


README

Latest Stable Version Total Downloads License

mix-php/redis-subscriber 分支而来

基于 Swoole 协程的 Redis 原生协议订阅者

基于 Swoole 协程的 Redis 原生协议订阅库

它直接通过 Socket 连接到 Redis 服务器,独立于 phpredis 扩展。此订阅者具有以下优势:

  • 平滑修改:可以在任何时候添加或取消订阅,以满足无缝频道切换的需求。
  • 协程间安全关闭:可以在任何时候关闭订阅。
  • 通道消息检索:该库的封装风格受到了 Go 语言中 go-redis 库的启发,通过通道检索订阅的消息。

安装

composer require friendsofhyperf/redis-subscriber

订阅频道

  • 连接或订阅失败将抛出异常
$sub = new \FriendsOfHyperf\Redis\Subscriber\Subscriber('127.0.0.1', 6379, '', 5); // Connection failure will throw an exception
$sub->subscribe('foo', 'bar'); // Subscription failure will throw an exception

$chan = $sub->channel();
while (true) {
    $data = $chan->pop();
    if (empty($data)) { // Manual close or abnormal disconnection from Redis will return false
        if (!$sub->closed) {
            // Handle abnormal disconnection from Redis
            var_dump('Redis connection is disconnected abnormally');
        }
        break;
    }
    var_dump($data);
}

接收订阅消息

object(FriendsOfHyperf\Redis\Subscriber\Message)#8 (2) {
  ["channel"]=>
  string(2) "foo"
  ["payload"]=>
  string(4) "test"
}

所有方法

许可证

MIT