jdwx/redis-pubsub

此软件包最新版本(v0.2.0)没有可用的许可信息。

v0.2.0 2023-04-03 20:40 UTC

This package is auto-updated.

Last update: 2024-09-03 23:36:33 UTC


README

这是一个轻量级的PHP客户端,用于异步Redis pub/sub功能,具有TLS和认证。它使用PHP的内置流函数,没有外部依赖。

目标用例是用于长时间运行的过程,该过程订阅了一个或多个Redis频道,并需要及时处理传入的消息,同时还需要执行其他操作。

它还支持简单的发布接口。这对于需要向Redis频道发布消息,但不与其他Redis交互的应用程序来说是合适的。

此软件包不打算成为一个功能齐全的Redis客户端。该空间已被其他扩展和软件包充分覆盖。

此软件包需要PHP 8.0或更高版本。

安装

通过composer

composer require jdwx/redis-pubsub

使用

发布者

use JDWX\RedisPubSub\RedisPubSub;

# By default, a connection is made to localhost:6379 without TLS.
$rps = new RedisPubSub();
$rps->publish( 'test', 'Hello, world!' );

订阅者

use JDWX\RedisPubSub\RedisPubSub;

# TLS is enabled by providing the paths to the client certificate and key, and,
# optionally, the CA certificate if the server should be verified.
$rps = new RedisPubSub( 'localhost', 6379, './client.crt', './client.key', './ca.crt' );

# Perform authentication.
$rps->auth( 'password' );

# Subscribe to a channel.
$rps->subscribe( 'test' );

# This will block until a message is received.
$msg = $rps->recv();

# This will not block if there are no pending messages.
$msg = $rps->tryRecv();

# This will wait for up to 5 seconds for a message to return.
$msg = $rps->tryRecv( 5.0 );

# This will wait for a message for up to 5 seconds without returning it.
$msg = $rps->tryWait( 5.0 );

# A callback function.
function MyCallback( array $msg ) : void {
    var_dump( $msg );
}

# This will pass all messages currently in the queue to the callback function,
# one at a time.
$rps->recvAll( 'MyCallback' );

# This will wait 5 seconds for messages to arrive, passing them one at a time
# to the callback function.
$rps->recvAllWait( 5.0, 'MyCallback' );

许可

此库采用BSD 2条款许可。有关详细信息,请参阅LICENSE文件。