框架——緩存框架——redis——數據類型——stream group

1、理論

1.1  概念

  引用原著:

  with different groups for the same stream, explicit acknowledgment of processed items, ability to inspect the pending items, claiming of unprocessed messages, and coherent history visibility for each single client, that is only able to see its private past history of messages.

  它相當於key爲consumer(消費者), value爲stream列表。Group用於管理。

  獲取consumer當前正在處理的item,處於等待狀態的item, 未被處理的item,可以查看每個consumer獲取item的歷史信息。

  每個客戶端相當於一個consumer。

1.2  結構

  

  第一部分,組的名稱。

  第二部分,stream的key值。

  第三部分,最後處理的ID。

  第四部分,客戶端列表,每個客戶端處於pending狀態的ID。

1.3   流程

第一步,客戶端,或者稱爲consumer,調用XREADGROUP命令查詢。

第二步,服務端返回結果,更新。

引用原著:

  If the message was never delivered to anyone, that is, if we are talking about a new message, then a PEL (Pending Entries List) is created.

發送失敗,即服務端未收到客戶端收到消息的確認信息,更新PEL。

  If instead the message was already delivered to this consumer, and it is just re-fetching the same message again, then the last delivery counter is updated to the current time, and the number of deliveries is incremented by one. You can access those message properties using the XPENDING command.

發送成功,更新last_delivered_id

2、操作

2.1  group

只有新增,刪除操作。

2.1.1    xgroup create

格式:

XGROUP CREATE key groupname <id | $> [MKSTREAM] [ENTRIESREAD entries_read]

  Xgroup是管理group的命令,後續跟子命令,當爲create時,創建group。

第一部分,XGROUP CREATE, 命令名稱。略。

第二部分,key,指定streams的key值。

第三部分,groupname,指定組的名稱。

第四部分,id,指定stream中的任意ID,$表示當前stream ID的最大值,group包含從id開始的信息,指定0-0,則爲全部,指定$,爲最新信息。

第五部分,MKSTREAM,當key值不存在時,默認爲錯誤,當添加該選項之後,會創建stream。

第六部分,ENTRIESREAD id,略。

示例:

XGROUP CREATE mystream mygroup $ MKSTREAM

2.1.2    xgroup destroy

格式:

XGROUP DESTROY key groupname

  Xgroup是管理group的命令,後續跟子命令,當爲destory時,刪除group.

2.1.3    xinfo groups key

查詢group的信息,略。

2.2  consumer

2.2.1    xgroup createconsumer

格式:

XGROUP CREATECONSUMER key groupname consumername

  Xgroup是管理group的命令,後續跟子命令,當爲createconsumer時,創建consumer。

示例:略。

2.2.2    xgroup createconsumer

格式:

XGROUP DELCONSUMER key groupname consumername

  Xgroup是管理group的命令,後續跟子命令,當爲delconsumer時,刪除consumer。

示例:略。

2.2.3    xreadgroup

格式:

XREADGROUP GROUP group consumer [COUNT count] [BLOCK milliseconds][NOACK] STREAMS key [key ...] id [id ...]

  Consumer從group中讀取stream,

其中key爲stream的key值,id可以縮小範圍,最小id,或者一次性提供多個id。Block設置讀取方式,

當未讀取到數據時,客戶端處於pending狀態。Count限定返回的條數。

第一部分,XREADGROUP GROUP命令名稱,略

第二部分,group,組的名稱。

第三部分,consumer, consumer在組中的唯一標識。

第四部分,count,限制每個stream的返回結果。

第五部分,block,超時時間。

第六部分,NOACK, 全程爲no acknowledge,添加此選項之後,這些消息不會記錄在consumer的歷史中。

第七部分,streams key id,指定組中的key值,起始id。

示例:

XREADGROUP GROUP mygroup myconsumer STREAMS mystream 0

2.2.4    xack

格式:

XACK key group id [id ...]

  當consumer處理完一條信息時,將其從組中刪除。

第一部分,key, group,stream的key,組的名稱。

第二部分,id,爲stream中的一到多個id。

  示例:

xack mystream mygroup 1526569495631-0

2.2.5    xinfo consumers

格式:

xinfo consumers key group

  查詢consumer在group組中獲取到的stream,其中key值爲stream的key。

示例:略。

2.3   stream

改變stream的狀態命令。

2.3.1    xpending

  格式:

XPENDING key group [[IDLE min-idle-time] start end count [consumer]]

  查詢組的PEL。Pel全程爲pending list。查詢處於pending狀態的stream。

第一部分,XPENDING, 略。

第二部分,key,stream的key值。

第三部分,group,組的名稱。

第四部分,IDLE, 處於PENDING狀態時間超過min-idle-time。單位爲毫秒。

第五部分,start, end,指定起始ID和截止ID。

第六部分, count,限定條數。

第七部分, consumer,組中consumer的唯一ID,若指定,只返回該consumer的PEL。

  示例:

XPENDING mystream mygroup IDLE 9000 - + 10 consumer-123

  返回mygroup中mystream,處於pending超出9秒, 歸屬於consumer~123的前10條信息。

2.3.2    xclaim

格式:

XCLAIM key group consumer min-idle-time id [id ...] [IDLE ms] [TIME unix-time-milliseconds] [RETRYCOUNT count] [FORCE] [JUSTID]  [LASTID id]

  改變組中處於PENDING狀態消息的歸屬consumer。

第一部分,XCLAIM爲命令名稱,略。

第二部分,key,stream的key。

第三部分,group,組的名稱。

第四部分,consumer,新的consumer id,必須在組中存在。

第五部分,min-idle-time,

第六部分, IDLE ms,指定消息處於PENDING的時間,指定爲0時,相當於重置。

第六部分,TIME, 指定相對時間,用於計算PENDING。

第七部分,RETRYCOUNT,指消息失敗的次數。指定爲0時,相當於重置。

第八部分,FORCE,當ID不存在時,創建該ID,並添加到PEL中。

第九部分,JUSTID,略。

xautoclaim等價於先執行xpending, 然後執行xclaim。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章