Redis 流返回一个空数组

Redis streams is returning an empty array

我使用以下命令创建了一个新的 Redis steam。

XGROUP CREATE A mygroup $ MKSTREAM

我添加了下面提到的数据

xadd A * X 1
xadd A * X 2
xadd A * X 3
xadd A * X 4

我正在使用以下命令读取数据。

XREADGROUP GROUP mygroup Alice COUNT 1 STREAMS A 0

它返回一个空数组

1) 1) "A"
   2) (empty array)

我使用的是 Redis 6.2.1 版。请帮我调试错误。

当你使用XREADGROUP命令读取消息时,你应该指定>作为ID,而不是0

来自 doc 的引用:

  • The special > ID, which means that the consumer want to receive only messages that were never delivered to any other consumer. It just means, give me new messages.
  • Any other ID, that is, 0 or any other valid ID or incomplete ID (just the millisecond time part), will have the effect of returning entries that are pending for the consumer sending the command with IDs greater than the one provided. So basically if the ID is not >, then the command will just let the client access its pending entries: messages delivered to it, but not yet acknowledged. Note that in this case, both BLOCK and NOACK are ignored.

如果 ID 不是 >,您只能读取待处理的消息,但是,在您的情况下,没有待处理的消息,因为您还没有消费任何东西。