|NO.Z.00012|——————————|BigDataEnd|——|Hadoop&Redis.V12|——|Redis.v12|数据类型.v07|

一、stream数据流类型
### --- stream是Redis5.0后新增的数据结构,用于可持久化的消息队列。

~~~     几乎满足了消息队列具备的全部内容,包括:
~~~     消息ID的序列化生成
~~~     消息遍历
~~~     消息的阻塞和非阻塞读取
~~~     消息的分组消费
~~~     未完成消息的处理
~~~     消息队列监控
~~~     每个Stream都有唯一的名称,它就是Redis的key,首次使用xadd 指令追加消息时自动创建。
二、常见操作命令如下表:
命令名称 命令格式 描述
xadd xadd key id <*> field1 value1.... 将指定消息数据追加到指定队列(key)中,*表示最新生成的id(当前时间+序列号)
xread xread [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] ID [ID ...] 从消息队列中读取,COUNT:读取条数,BLOCK:阻塞读(默认不阻塞)key:
队列名称 id:消息id
xrange xrange key start end [COUNT] 读取队列中给定ID范围的消息 COUNT:返回消息条数(消息id从小到大)
xrevrange xrevrange key start end [COUNT] 读取队列中给定ID范围的消息 COUNT:
返回消息条数(消息id从大到小)
xdel xdel key id 删除队列的消息
xgroup xgroup create key groupname id 创建一个新的消费组
xgroup xgroup destory key groupname 删除指定消费组
xgroup xgroup delconsumer key groupname cname 删除指定消费组中的某个消费者
xgroup xgroup setid key id 修改指定消息的最大id
xreadgroup xreadgroup group groupname
consumer COUNT streams key
从队列中的消费组中创建消费者并消费数据(consumer不存在则创建)
三、应用场景:消息队列的使用
127.0.0.1:6379> xadd topic:001 * name zhangfei age 23
"1631783265723-0"
127.0.0.1:6379> xadd topic:001 * name zhaoyun age 24 name diaochan age 16
"1631783272874-0"
127.0.0.1:6379> xrange topic:001 - +
1) 1) "1631783265723-0"
   2) 1) "name"
      2) "zhangfei"
      3) "age"
      4) "23"
2) 1) "1631783272874-0"
   2) 1) "name"
      2) "zhaoyun"
      3) "age"
      4) "24"
      5) "name"
      6) "diaochan"
      7) "age"
      8) "16"
127.0.0.1:6379> xread COUNT 1 streams topic:001 0
1) 1) "topic:001"
   2) 1) 1) "1631783265723-0"
         2) 1) "name"
            2) "zhangfei"
            3) "age"
            4) "23"
### --- 创建的group1

127.0.0.1:6379> xgroup create topic:001 group1 0
OK
### --- 创建cus1加入到group1 消费 没有被消费过的消息 消费第一条

127.0.0.1:6379> xreadgroup group group1 cus1 count 1 streams topic:001 >
1) 1) "topic:001"
   2) 1) 1) "1631783265723-0"
         2) 1) "name"
            2) "zhangfei"
            3) "age"
            4) "23"
### --- 继续消费 第二条

127.0.0.1:6379> xreadgroup group group1 cus1 count 1 streams topic:001 >
1) 1) "topic:001"
   2) 1) 1) "1631783272874-0"
         2) 1) "name"
            2) "zhaoyun"
            3) "age"
            4) "24"
            5) "name"
            6) "diaochan"
            7) "age"
            8) "16"
### --- 没有可消费

127.0.0.1:6379> xreadgroup group group1 cus1 count 1 streams topic:001 >
(nil)

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(22)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示