refactor(cache): remove database fallback from conversation sequence retrieval
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:
29
internal/cache/conversation_cache.go
vendored
29
internal/cache/conversation_cache.go
vendored
@@ -589,30 +589,23 @@ 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 批量获取多个会话的 maxSeq(MGet 减少网络往返)
|
// GetConvMaxSeqBatch 批量获取多个会话的 maxSeq(MGet 减少网络往返)
|
||||||
func (c *ConversationCache) GetConvMaxSeqBatch(ctx context.Context, convIDs []string) (map[string]int64, error) {
|
func (c *ConversationCache) GetConvMaxSeqBatch(ctx context.Context, convIDs []string) (map[string]int64, error) {
|
||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user