Redis 简易消息队列。

v0.1.0 2019-08-11 05:45 UTC

This package is auto-updated.

Last update: 2024-08-29 05:23:05 UTC


README

Travis CI Codecov

一个轻量级的 PHP 消息队列,无需专用队列服务器。只需一个 Redis 服务器。

PHP 实现了 smrchy/rsmq

安装

composer require eislambey/rsmq

方法

构造

创建 RSMQ 的新实例。

参数

  • $redis (Redis): *必需 The Redis 实例
  • $ns (字符串): 可选(默认: "rsmq") RSMQ 创建的所有键使用的命名空间前缀
  • $realtime (布尔值): 可选(默认: false) 启用实时 PUBLISH 新消息

示例

<?php

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$rsmq = new \Islambey\RSMQ\RSMQ($redis);

队列

createQueue

创建一个新的队列。

参数

  • $name (字符串): 队列名称。最大 160 个字符;允许字母数字字符、连字符 (-) 和下划线 (_)。
  • $vt (整数): 可选 (默认: 30) 从队列接收到的消息不可见的时间长度,单位为秒,当其他接收组件请求接收消息时。允许值:0-9999999(约 115 天)
  • $delay (整数): 可选 (默认: 0) 队列中所有新消息投递的延迟时间,单位为秒。允许值:0-9999999(约 115 天)
  • $maxsize (整数): 可选 (默认: 65536) 消息的最大字节数。允许值:1024-65536 和 -1(表示无限制大小)

返回值

  • true (布尔值)

抛出

  • \Islambey\RSMQ\Exception

示例

<?php

$rsmq->createQueue('myqueue');

listQueues

列出所有队列

返回一个数组

  • ["qname1", "qname2"]

示例

<?php

$queues = $rsmq->listQueues();

deleteQueue

删除队列及其所有消息。

参数

  • $name (字符串): 队列名称。

返回值

  • true (布尔值)

抛出

  • \Islambey\RSMQ\Exception

示例

<?php

$rsmq->deleteQueue('myqueue');

getQueueAttributes

获取队列属性,计数器和统计信息

参数

  • $queue (字符串): 队列名称。

返回一个关联数组

  • vt (整数): 队列的可见性超时,单位为秒
  • delay (整数): 新消息的延迟时间,单位为秒
  • maxsize (整数): 消息的最大大小,单位为字节
  • totalrecv (整数): 从队列接收到的总消息数
  • totalsent (整数): 发送到队列的总消息数
  • created (浮点数): 队列创建的时间戳(以秒为单位的纪元时间)
  • modified (浮点数): 队列最后修改的时间戳(以秒为单位的纪元时间),使用 setQueueAttributes 进行修改
  • msgs (整数): 队列中当前的消息数
  • hiddenmsgs (整数): 当前隐藏/不可见消息的数量。消息可以在“飞行中”因 vt 参数或使用 delay 发送时被隐藏

示例

<?php

$attributes =  $rsmq->getQueueAttributes('myqueue');
echo "visibility timeout: ", $attributes['vt'], "\n";
echo "delay for new messages: ", $attributes['delay'], "\n";
echo "max size in bytes: ", $attributes['maxsize'], "\n";
echo "total received messages: ", $attributes['totalrecv'], "\n";
echo "total sent messages: ", $attributes['totalsent'], "\n";
echo "created: ", $attributes['created'], "\n";
echo "last modified: ", $attributes['modified'], "\n";
echo "current n of messages: ", $attributes['msgs'], "\n";
echo "hidden messages: ", $attributes['hiddenmsgs'], "\n";

setQueueAttributes

设置队列参数。

参数

  • $queue (字符串): 队列名称。
  • $vt (整数): 可选 * 从队列接收到的消息不可见的时间长度,单位为秒,当其他接收组件请求接收消息时。允许值:0-9999999(约 115 天)
  • $delay (整数): 可选 新消息在队列中投递的延迟时间,单位为秒。允许值:0-9999999(约 115 天)
  • $maxsize (整数): 可选 消息的最大大小,单位为字节。允许值:1024-65536 和 -1(表示无限制大小)

注意:至少必须提供一个属性(vt、delay、maxsize)。只有提供的属性将被修改。

返回一个关联数组

  • vt (整数): 队列的可见性超时,单位为秒
  • delay (整数): 新消息的延迟时间,单位为秒
  • maxsize (整数): 消息的最大大小,单位为字节
  • totalrecv (整数): 从队列接收到的总消息数
  • totalsent (整数): 发送到队列的总消息数
  • created (浮点数): 队列创建的时间戳(以秒为单位的纪元时间)
  • modified (浮点数): 队列最后修改的时间戳(以秒为单位的纪元时间),使用 setQueueAttributes 进行修改
  • msgs (整数): 队列中当前的消息数
  • hiddenmsgs (整数): 当前隐藏/不可见消息的数量。消息可以在“飞行中”因 vt 参数或使用 delay 发送时被隐藏

抛出

  • \Islambey\RSMQ\Exception

示例

<?php

$queue = 'myqueue';
$vt = 50;
$delay = 10;
$maxsize = 2048;
$rsmq->setQueueAttributes($queue, $vt, $delay, $maxsize)

消息

sendMessage

发送一条新消息。

参数

  • $queue (字符串)
  • $message (字符串)
  • $delay (整数): 可选 (默认: 队列设置) 消息投递将被延迟的时间(秒)。允许的值: 0-9999999(约115天)

返回值

  • $id (字符串): 内部消息ID。

抛出

  • \Islambey\RSMQ\Exception

示例

<?php

$id = $rsmq->sendMessage('myqueue', 'a message');
echo "Message Sent. ID: ", $id;

receiveMessage

从队列中接收下一条消息。

参数

  • $queue (字符串): 队列名称。
  • $vt (整数): 可选 (默认: 队列设置) 接收的消息对其他人不可见的时间长度(秒)。允许的值: 0-9999999(约115天)

返回一个关联数组

  • message (字符串): 消息的内容。
  • id (字符串): 内部消息ID。
  • sent (整数): 消息发送/创建的时间戳。
  • fr (整数): 消息首次接收的时间戳。
  • rc (整数): 消息被接收的次数。

注意:如果没有消息,将返回一个空数组

抛出

  • \Islambey\RSMQ\Exception

示例

<?php

$message = $rsmq->receiveMessage('myqueue');
echo "Message ID: ", $message['id'];
echo "Message: ", $message['message'];

deleteMessage

参数

  • $queue (字符串): 队列名称。
  • $id (字符串): 要删除的消息ID。

返回值

  • true 如果成功,false 如果消息未找到(布尔值)。

抛出

  • \Islambey\RSMQ\Exception

示例

<?php

$id = $rsmq->sendMessage('queue', 'a message');
$rsmq->deleteMessage($id);

popMessage

从队列中接收下一条消息 并删除它

重要:此方法会立即删除接收到的消息。如果在处理消息时发生错误,则无法再次接收该消息。

参数

  • $queue (字符串): 队列名称。

返回关联数组

  • message (字符串): 消息的内容。
  • id (字符串): 内部消息ID。
  • sent (整数): 消息发送/创建的时间戳。
  • fr (整数): 消息首次接收的时间戳。
  • rc (整数): 消息被接收的次数。

注意:如果没有消息,将返回一个空对象

抛出

  • \Islambey\RSMQ\Exception

示例

<?php

$message = $rsmq->popMessage('myqueue');
echo "Message ID: ", $message['id'];
echo "Message: ", $message['message'];

changeMessageVisibility

更改单个消息的可见性计时器。消息将再次可见的时间从当前时间(现在)+ vt 计算得出。

参数

  • qname (字符串): 队列名称。
  • id (字符串): 消息ID。
  • vt (整数): 此消息将不可见的时间长度(秒)。允许的值: 0-9999999(约115天)

返回值

  • true 如果成功,false 如果消息未找到(布尔值)。

抛出

  • \Islambey\RSMQ\Exception

示例

<?php

$queue = 'myqueue';
$id = $rsmq->sendMessage($queue, 'a message');
if($rsmq->changeMessageVisibility($queue, $id, 60)) {
	echo "Message hidden for 60 secs";
}

授权协议

MIT授权协议。请参阅授权协议