diff --git a/中间件/redis/redis.md b/中间件/redis/redis.md index 72a0844..0fe4f90 100644 --- a/中间件/redis/redis.md +++ b/中间件/redis/redis.md @@ -98,6 +98,7 @@ - [count option](#count-option) - [Listening for new items with XREAD](#listening-for-new-items-with-xread) - [XREAD with STREAMS option](#xread-with-streams-option) + - [XREAD with BLOCK argument](#xread-with-block-argument) # redis @@ -1581,3 +1582,29 @@ redis stream通过不同的命令支持了以上三种query model。 XREAD在和STREAMS一起使用时,并且需要指定`a list of keys`和`maximum ID already seen for each stream by the calling consumer`,故而,该命令只会向consumer返回`messages with and ID greater than the one we specified`。 在上述示例中,命令为`STREAMS race:france 0`,其代表`race:france流中所有ID比0-0大的消息`。故而,返回的结构中,顶层为stream的key name,`因为上述命令可以指定多个stream,针对多个stream进行监听`。 + +在指定`STREAMS` option时,必须后缀key names,故而STREAMS选项必须为最后一个option,任何其他option必须要放在STREAMS之前。 + +除了streams之外,还可以为`XREAD`指定`last ID we own`。 + +##### XREAD with BLOCK argument +上述示例为`non-blocking`形式,除此之外还可以通过`BLOCK`参数将命令转化为`blocking`形式: +```redis-cli +> XREAD BLOCK 0 STREAMS race:france $ +``` +在上述示例中,并没有指定`COUNT`,而是指定了`BLOCK`选项,并设置`timeout`为`0 milliseconds`(代表永远不会超时)。 + +并且,BLOCK版本的示例并没有传递正常的ID,而是传递了一个特殊的ID `$`,该符号代表`XREAD`应当使用`stream已经存储的最大ID`来作为last ID。故而,在指定last ID为`$`后,只会接收到`new messages`。其行为和`unix command`中的`tail -f`类似。 + +在指定了BLOCK选中后,其行为如下: +- `if the command is able to serve our request immediately without blocking`,那么其会立马被执行 +- 如果当前不满足获取entry的条件,那么client会发生阻塞 + +通常,在希望`从new entries`开始消费时,会从`ID $`开始,在获取到`ID $`对应的entry后,下一次消费从`last message recevied`开始。 + +XREAD的blocking形式也支持监听多个streams,也可以指定多个key names。如果至少存在一个stream中`存在元素的ID并命令指定的ID更大`,那么将会立马返回结果;否则,该命令将会阻塞,直到有一个stream获取到新的data。 + +和blocking list操作类似,`blocking stream reads`对`clients wating for data`而言是公平的,也支持FIFO。`the first client that blocked for a given stream will be the first to be unlocked when new items are available`。 + +`XREAD`除了` +