refactor(cache): remove database fallback from conversation sequence retrieval
All checks were successful
Build Backend / build (push) Successful in 1m59s
Build Backend / build-docker (push) Successful in 1m13s

Remove the fallback mechanism that queried the database when Redis keys were missing or unavailable. The `GetConvMaxSeq` and `GetConvMaxSeqBatch` methods now strictly rely on the Redis cache and return `ErrKeyNotFound` if the data is not present.
This commit is contained in:
2026-05-15 13:48:29 +08:00
parent 6c14309624
commit de0766df5e

View File

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