mongo数据库增加是否重缓存查询数据,增加随机修改删除状态接口;redis增加-队列消息读取封装方法
This commit is contained in:
14
redis/message.go
Normal file
14
redis/message.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package redis
|
||||
|
||||
import "context"
|
||||
|
||||
type QueueMessage struct {
|
||||
StreamKey string // Stream 键名
|
||||
GroupName string // 消费者组名称
|
||||
ConsumerName string // 消费者名称
|
||||
Timeout int64 // 阻塞超时时间(毫秒)
|
||||
BatchSize int64 // 最大并发数(信号量容量)
|
||||
BlockMs int64
|
||||
Block bool
|
||||
HandleFunc func(ctx context.Context, message map[string]interface{}) error
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package redis
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -33,6 +34,9 @@ func GetRedisClient() *gredis.Redis {
|
||||
return getClient()
|
||||
}
|
||||
|
||||
// RedisClient 导出的 Redis 客户端(供 mongo.go 使用,兼容旧代码)
|
||||
var RedisClient = getClient()
|
||||
|
||||
// Lock 分布式锁
|
||||
func Lock(ctx context.Context, key string, expireSeconds int64, fn func(ctx context.Context) error) (success bool, err error) {
|
||||
limit := 3
|
||||
@@ -66,8 +70,52 @@ LOOP:
|
||||
}
|
||||
}
|
||||
|
||||
// RedisClient 导出的 Redis 客户端(供 mongo.go 使用,兼容旧代码)
|
||||
var RedisClient = getClient()
|
||||
func GetReadStream(ctx context.Context, msg ...QueueMessage) error {
|
||||
for _, t := range msg {
|
||||
err := GetReadFromStream(ctx, t.StreamKey, t.GroupName, t.ConsumerName, t.BatchSize, t.BlockMs, t.Block, t.HandleFunc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetReadFromStream 读取ReadFromStream数据
|
||||
func GetReadFromStream(ctx context.Context, streamKey, groupName, consumerName string, count, blockMs int64, Block bool, fn func(ctx context.Context, message map[string]interface{}) error) (err error) {
|
||||
glog.Infof(ctx, "初始化 Stream: %s, 消费者组: %s", streamKey, groupName)
|
||||
err = InitStreamGroup(ctx, streamKey, groupName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
// 从 Redis Stream 读取一批消息
|
||||
messages, err := ReadFromStream(ctx, streamKey, groupName, consumerName, count, blockMs)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "[DEBUG Redis] XREADGROUP 错误: %v", err)
|
||||
return err
|
||||
}
|
||||
// 处理消息
|
||||
for _, msg := range messages {
|
||||
fmt.Printf("消费者 '%s' -> 接收到消息 ID: %s, 内容: %v\n", consumerName, msg.ID, msg.Values)
|
||||
// 业务处理
|
||||
if err = fn(ctx, msg.Values); err != nil {
|
||||
return err
|
||||
}
|
||||
// 确认消息 (ACK)
|
||||
if Block {
|
||||
// 处理成功后,必须调用 XAck,否则消息会一直留在 PEL 中
|
||||
err = AckMessage(ctx, streamKey, groupName, msg.ID)
|
||||
if err != nil {
|
||||
glog.Infof(ctx, "消费者 '%s' 确认消息 ID %s 失败: %v\n", consumerName, msg.ID, err)
|
||||
} else {
|
||||
glog.Infof(ctx, "消费者 '%s' -> 已确认消息 ID: %s\n", consumerName, msg.ID)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Stream 和消费者组常量
|
||||
const (
|
||||
@@ -150,6 +198,7 @@ func ReadFromStream(ctx context.Context, streamKey, groupName, consumerName stri
|
||||
execCtx = context.Background()
|
||||
}
|
||||
|
||||
RECONNECT:
|
||||
// XREADGROUP GROUP groupName consumerName COUNT count BLOCK blockMs STREAMS streamKey >
|
||||
result, err := redisClient.Do(execCtx,
|
||||
"XREADGROUP", "GROUP", groupName, consumerName,
|
||||
@@ -157,10 +206,8 @@ func ReadFromStream(ctx context.Context, streamKey, groupName, consumerName stri
|
||||
"BLOCK", blockMs,
|
||||
"STREAMS", streamKey, ">",
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "[DEBUG Redis] XREADGROUP 错误: %v", err)
|
||||
return nil, err
|
||||
goto RECONNECT
|
||||
}
|
||||
|
||||
glog.Debugf(ctx, "[DEBUG Redis] XREADGROUP 返回: %+v", result)
|
||||
|
||||
Reference in New Issue
Block a user