map预分配容量避免动态扩容,优化了返回值复用情况

This commit is contained in:
Cold
2025-12-06 10:13:38 +08:00
committed by 张斌
parent 15ce1cf345
commit 86661c687a
3 changed files with 52 additions and 10 deletions

39
redis/types.go Normal file
View File

@@ -0,0 +1,39 @@
package redis
// SendStreamMessage 发送到 Redis Stream 的消息结构
type SendStreamMessage struct {
UserId string `json:"user_id"` // 用户ID
Content string `json:"content"` // 消息内容
Timestamp int64 `json:"timestamp"` // 时间戳(秒)
MessageId string `json:"message_id"` // 消息唯一ID
}
// ToMap 转换为 map[string]interface{} 用于 Stream 存储
func (m *SendStreamMessage) ToMap() map[string]interface{} {
return map[string]interface{}{
"user_id": m.UserId,
"content": m.Content,
"timestamp": m.Timestamp,
"message_id": m.MessageId,
}
}
// BatchStreamMessage 批量消息结构
type BatchStreamMessage struct {
UserId string `json:"user_id"` // 用户ID
Content string `json:"content"` // 消息内容
Timestamp int64 `json:"timestamp"` // 时间戳(秒)
BatchId string `json:"batch_id"` // 批次ID
Index int `json:"index"` // 批次内序号
}
// ToMap 转换为 map[string]interface{} 用于 Stream 存储
func (m *BatchStreamMessage) ToMap() map[string]interface{} {
return map[string]interface{}{
"user_id": m.UserId,
"content": m.Content,
"timestamp": m.Timestamp,
"batch_id": m.BatchId,
"index": m.Index,
}
}