diff --git a/internal/cache/conversation_cache.go b/internal/cache/conversation_cache.go index d654074..82f3675 100644 --- a/internal/cache/conversation_cache.go +++ b/internal/cache/conversation_cache.go @@ -589,29 +589,22 @@ func (c *ConversationCache) GetUserReadSeqs(ctx context.Context, userID string, return result, nil } -// GetConvMaxSeq 获取会话的最大 seq(优先 Redis,降级 DB) +// GetConvMaxSeq 获取会话的最大 seq(复用 msg_seq 计数器,读取当前值) func (c *ConversationCache) GetConvMaxSeq(ctx context.Context, convID string) (int64, error) { seqKey := MessageSeqKey(convID) redisCache, ok := c.cache.(*RedisCache) - if ok { - rdb := redisCache.GetRedisClient().GetClient() - nKey := normalizeKey(seqKey) - val, err := rdb.Get(ctx, nKey).Int64() - if err == nil { - return val, nil - } + if !ok { + return 0, ErrKeyNotFound } - // Redis 未命中或不可用,降级到 DB - if c.repo != nil { - conv, err := c.repo.GetConversationByID(convID) - if err == nil && conv.LastSeq > 0 { - return conv.LastSeq, nil - } + rdb := redisCache.GetRedisClient().GetClient() + nKey := normalizeKey(seqKey) + val, err := rdb.Get(ctx, nKey).Int64() + if err != nil { + return 0, ErrKeyNotFound } - - return 0, ErrKeyNotFound + return val, nil } // GetConvMaxSeqBatch 批量获取多个会话的 maxSeq(MGet 减少网络往返) @@ -645,12 +638,6 @@ func (c *ConversationCache) GetConvMaxSeqBatch(ctx context.Context, convIDs []st result := make(map[string]int64, len(convIDs)) for i, val := range vals { if val == nil { - // Redis 未命中,降级到 DB - if c.repo != nil { - if conv, err := c.repo.GetConversationByID(convIDs[i]); err == nil && conv.LastSeq > 0 { - result[convIDs[i]] = conv.LastSeq - } - } continue } var seq int64