From 06db8e36e4ee903dce75747f54d02742850d4972 Mon Sep 17 00:00:00 2001 From: lan Date: Tue, 12 May 2026 18:26:31 +0800 Subject: [PATCH] refactor(cache): remove deprecated unread count methods and keys Clean up the codebase by removing unused and deprecated unread count functionality in `ConversationCache` and associated Redis key generators. This follows the transition to sequence-based unread calculation. - Remove `IncrementUnread`, `ClearUnread`, `GetUnreadCountFromHash`, `GetAllUnreadCounts`, and `GetTotalUnread` from `ConversationCache`. - Remove `PrefixUnreadHash` and `PrefixUnreadTotal` constants and `UnreadHashKey`/`UnreadTotalKey` helper functions. - Remove unused `isValidContentType` helper in `MessageHandler`. --- internal/cache/conversation_cache.go | 30 ---------------------------- internal/cache/keys.go | 14 +------------ internal/handler/message_handler.go | 10 ---------- 3 files changed, 1 insertion(+), 53 deletions(-) diff --git a/internal/cache/conversation_cache.go b/internal/cache/conversation_cache.go index b8b349d..0c9b095 100644 --- a/internal/cache/conversation_cache.go +++ b/internal/cache/conversation_cache.go @@ -692,36 +692,6 @@ func (c *ConversationCache) ComputeAllUnreadCount(ctx context.Context, userID st return total, nil } -// IncrementUnread 递增用户在某会话的未读数(已废弃:改用 seq 算术计算) -// Deprecated: Unread count is now computed via arithmetic (maxSeq - readSeq). -func (c *ConversationCache) IncrementUnread(ctx context.Context, userID, convID string) error { - return nil -} - -// ClearUnread 清零用户在某会话的未读数(已废弃:改用 seq 算术计算) -// Deprecated: No longer needed. Unread is computed from seq arithmetic. -func (c *ConversationCache) ClearUnread(ctx context.Context, userID, convID string) error { - return nil -} - -// GetUnreadCountFromHash 从 Redis Hash 获取单个会话未读数(已废弃) -// Deprecated: Use ComputeUnreadCount instead. -func (c *ConversationCache) GetUnreadCountFromHash(ctx context.Context, userID, convID string) (int64, error) { - return 0, ErrKeyNotFound -} - -// GetAllUnreadCounts 从 Redis Hash 获取用户所有会话的未读数(已废弃) -// Deprecated: Use ComputeAllUnreadCount instead. -func (c *ConversationCache) GetAllUnreadCounts(ctx context.Context, userID string) (map[string]int64, error) { - return nil, ErrKeyNotFound -} - -// GetTotalUnread 从 Redis 获取用户总未读数(已废弃) -// Deprecated: Use ComputeAllUnreadCount instead. -func (c *ConversationCache) GetTotalUnread(ctx context.Context, userID string) (int64, error) { - return 0, ErrKeyNotFound -} - // GetNextSeq 获取会话的下一个 seq 值(原子递增) // 使用 Redis INCR 实现,首次调用时从 DB 初始化并补齐差值 func (c *ConversationCache) GetNextSeq(ctx context.Context, convID string) (int64, error) { diff --git a/internal/cache/keys.go b/internal/cache/keys.go index 38e0756..bd2de12 100644 --- a/internal/cache/keys.go +++ b/internal/cache/keys.go @@ -26,9 +26,7 @@ const ( PrefixUnreadSystem = "unread:system" PrefixUnreadConversation = "unread:conversation" PrefixUnreadDetail = "unread:detail" - PrefixUnreadHash = "unread:hash" - PrefixUnreadTotal = "unread:total" - + // 已读位置相关(OpenIM 风格:缓存 hasReadSeq,用于 O(1) 未读数计算) PrefixUserReadSeq = "user_read_seq" @@ -123,16 +121,6 @@ func UnreadDetailKey(userID, conversationID string) string { return fmt.Sprintf("%s:%s:%s", PrefixUnreadDetail, userID, conversationID) } -// UnreadHashKey 生成用户未读数 Hash 缓存键 -func UnreadHashKey(userID string) string { - return fmt.Sprintf("%s:%s", PrefixUnreadHash, userID) -} - -// UnreadTotalKey 生成用户总未读数计数器键 -func UnreadTotalKey(userID string) string { - return fmt.Sprintf("%s:%s", PrefixUnreadTotal, userID) -} - // UserInfoKey 生成用户信息缓存键 func UserInfoKey(userID string) string { return fmt.Sprintf("%s:%s", PrefixUserInfo, userID) diff --git a/internal/handler/message_handler.go b/internal/handler/message_handler.go index 6c37558..843ad97 100644 --- a/internal/handler/message_handler.go +++ b/internal/handler/message_handler.go @@ -653,16 +653,6 @@ func (h *MessageHandler) DeleteMessage(c *gin.Context) { response.SuccessWithMessage(c, "message deleted", nil) } -// 辅助函数:验证内容类型 -func isValidContentType(contentType model.ContentType) bool { - switch contentType { - case model.ContentTypeText, model.ContentTypeImage, model.ContentTypeVideo, model.ContentTypeAudio, model.ContentTypeFile: - return true - default: - return false - } -} - // 辅助函数:获取会话参与者信息 func (h *MessageHandler) getConversationParticipants(ctx context.Context, conversationID string, currentUserID string) ([]*model.User, error) { // 从repository获取参与者列表