From e01a01c1aade8cfb153086deaa8fab9784a04fdf Mon Sep 17 00:00:00 2001 From: yml Date: Fri, 20 Nov 2026 19:46:09 +0800 Subject: [PATCH] =?UTF-8?q?perf(chat):=20=E4=BD=BF=E7=94=A8=E9=A2=84?= =?UTF-8?q?=E8=81=9A=E5=90=88=E5=BF=AB=E7=85=A7=E4=BC=98=E5=8C=96=E5=AE=A2?= =?UTF-8?q?=E6=9C=8D=E4=BC=9A=E8=AF=9D=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/chat/admin_counts_cache.go | 114 +++++++++++++++++- .../modules/chat/admin_counts_cache_test.go | 99 +++++++++++++++ backend/internal/modules/chat/message.go | 1 + backend/internal/modules/chat/qrcode.go | 1 + backend/internal/modules/chat/repository.go | 10 +- backend/internal/modules/chat/support.go | 53 ++++++-- .../features/admin/views/AdminChatsView.vue | 27 ++++- frontend/src/features/chats/api/chats.ts | 1 + 8 files changed, 282 insertions(+), 24 deletions(-) create mode 100644 backend/internal/modules/chat/admin_counts_cache_test.go diff --git a/backend/internal/modules/chat/admin_counts_cache.go b/backend/internal/modules/chat/admin_counts_cache.go index a046854a..b5dd84b8 100644 --- a/backend/internal/modules/chat/admin_counts_cache.go +++ b/backend/internal/modules/chat/admin_counts_cache.go @@ -6,19 +6,68 @@ import ( "encoding/hex" "encoding/json" "fmt" + "strconv" "time" "hfb_sys/backend/internal/tenancy" + + "go.uber.org/zap" ) -const adminChatCountsCacheTTL = 5 * time.Second +const ( + adminChatCountsCacheTTL = 30 * time.Minute + adminChatCountsFreshFor = 15 * time.Second + adminChatCountsRefreshTTL = 20 * time.Second +) + +type adminChatCountsSnapshot struct { + Counts *AdminConversationCountsDTO `json:"counts"` + GeneratedAt time.Time `json:"generated_at"` + Generation int64 `json:"generation"` +} func (r *Repository) adminChatCountsCacheKey(ctx context.Context, principal Principal, filter, stage, keyword string) string { sum := sha256.Sum256([]byte(keyword)) - return fmt.Sprintf("admin-chat:counts:v3:%s:%d:%s:%s:%s:%s", principal.Type, principal.ID, tenancy.ScopeKey(ctx), filter, stage, hex.EncodeToString(sum[:8])) + return fmt.Sprintf("admin-chat:counts:v4:%s:%d:%s:%s:%s:%s", principal.Type, principal.ID, tenancy.ScopeKey(ctx), filter, stage, hex.EncodeToString(sum[:8])) } -func (r *Repository) loadAdminChatCountsCache(ctx context.Context, principal Principal, filter, stage, keyword string) *AdminConversationCountsDTO { +func adminChatCountsGenerationKey(scope string) string { + return "admin-chat:counts:generation:" + scope +} + +func (r *Repository) adminChatCountsGeneration(ctx context.Context) int64 { + if r.redis == nil { + return 0 + } + raw, err := r.redis.Get(ctx, adminChatCountsGenerationKey(tenancy.ScopeKey(ctx))).Result() + if err != nil { + return 0 + } + value, _ := strconv.ParseInt(raw, 10, 64) + return value +} + +func (r *Repository) invalidateAdminChatCounts(ctx context.Context, tenantIDs []uint64) { + if r.redis == nil { + return + } + seen := make(map[uint64]struct{}, len(tenantIDs)) + for _, tenantID := range tenantIDs { + if tenantID == 0 { + continue + } + if _, ok := seen[tenantID]; ok { + continue + } + seen[tenantID] = struct{}{} + _ = r.redis.Incr(ctx, adminChatCountsGenerationKey(fmt.Sprintf("tenant:%d", tenantID))).Err() + } + if len(seen) == 0 { + _ = r.redis.Incr(ctx, adminChatCountsGenerationKey(tenancy.ScopeKey(ctx))).Err() + } +} + +func (r *Repository) loadAdminChatCountsSnapshot(ctx context.Context, principal Principal, filter, stage, keyword string) *adminChatCountsSnapshot { if r.redis == nil { return nil } @@ -26,20 +75,73 @@ func (r *Repository) loadAdminChatCountsCache(ctx context.Context, principal Pri if err != nil { return nil } - var value AdminConversationCountsDTO + var value adminChatCountsSnapshot if err := json.Unmarshal(raw, &value); err != nil { return nil } + if value.Counts == nil || value.GeneratedAt.IsZero() { + return nil + } return &value } -func (r *Repository) storeAdminChatCountsCache(ctx context.Context, principal Principal, filter, stage, keyword string, value *AdminConversationCountsDTO) { +func (r *Repository) storeAdminChatCountsSnapshot(ctx context.Context, principal Principal, filter, stage, keyword string, value *AdminConversationCountsDTO, generation int64) { if r.redis == nil || value == nil { return } - raw, err := json.Marshal(value) + raw, err := json.Marshal(adminChatCountsSnapshot{ + Counts: value, + GeneratedAt: time.Now(), + Generation: generation, + }) if err != nil { return } _ = r.redis.Set(ctx, r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword), raw, adminChatCountsCacheTTL).Err() } + +func (r *Repository) refreshAdminChatCounts(ctx context.Context, principal Principal, filter, stage, keyword string) (*AdminConversationCountsDTO, error) { + key := r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword) + value, err, _ := r.countsGroup.Do(key, func() (any, error) { + generation := r.adminChatCountsGeneration(ctx) + result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword) + if err != nil { + return nil, err + } + r.storeAdminChatCountsSnapshot(ctx, principal, filter, stage, keyword, result.DTO, generation) + return result.DTO, nil + }) + if err != nil { + return nil, err + } + counts, ok := value.(*AdminConversationCountsDTO) + if !ok || counts == nil { + return nil, fmt.Errorf("invalid admin chat counts snapshot") + } + return counts, nil +} + +func (r *Repository) refreshAdminChatCountsAsync(ctx context.Context, principal Principal, filter, stage, keyword string) { + if r.redis == nil { + return + } + key := r.adminChatCountsCacheKey(ctx, principal, filter, stage, keyword) + ch := r.countsGroup.DoChan(key, func() (any, error) { + refreshCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), adminChatCountsRefreshTTL) + defer cancel() + generation := r.adminChatCountsGeneration(refreshCtx) + result, err := r.adminConversationCounts(refreshCtx, principal, filter, stage, keyword) + if err != nil { + return nil, err + } + r.storeAdminChatCountsSnapshot(refreshCtx, principal, filter, stage, keyword, result.DTO, generation) + return result.DTO, nil + }) + select { + case result := <-ch: + if result.Err != nil && r.logger != nil { + r.logger.Warn("后台客服统计刷新失败", zap.Error(result.Err)) + } + default: + } +} diff --git a/backend/internal/modules/chat/admin_counts_cache_test.go b/backend/internal/modules/chat/admin_counts_cache_test.go new file mode 100644 index 00000000..3b941ba5 --- /dev/null +++ b/backend/internal/modules/chat/admin_counts_cache_test.go @@ -0,0 +1,99 @@ +package chat + +import ( + "encoding/json" + "testing" + "time" + + "hfb_sys/backend/internal/model" + "hfb_sys/backend/internal/tenancy" + + "github.com/redis/go-redis/v9" +) + +func TestAdminConversationCountsUsesFreshSnapshot(t *testing.T) { + db := setupAdminListTestDB(t) + client := redis.NewClient(&redis.Options{Addr: "127.0.0.1:16379", DB: 15}) + t.Cleanup(func() { + _ = client.Close() + }) + if err := client.Ping(t.Context()).Err(); err != nil { + t.Skipf("Redis 测试服务不可用: %v", err) + } + + admin := model.AdminUser{Username: "counts-cache", Nickname: "缓存测试", Status: "active"} + if err := db.Create(&admin).Error; err != nil { + t.Fatal(err) + } + conversation := model.ChatConversation{ + TenantID: 1, + Type: ConversationTypeGeneralSupport, + Title: "缓存测试会话", + Status: "active", + } + if err := db.Create(&conversation).Error; err != nil { + t.Fatal(err) + } + participant := model.ChatParticipant{ + ConversationID: conversation.ID, + ParticipantType: "admin", + ParticipantID: admin.ID, + Role: "support", + JoinedAt: time.Now(), + } + if err := db.Create(&participant).Error; err != nil { + t.Fatal(err) + } + + repo := NewRepository(db, nil, client) + ctx := tenancy.WithScope(t.Context(), tenancy.Scope{TenantID: 1}) + principal := Principal{Type: "admin", ID: admin.ID} + cacheKey := repo.adminChatCountsCacheKey(ctx, principal, adminChatFilterMine, adminChatStageAll, "") + t.Cleanup(func() { + _ = client.Del(t.Context(), cacheKey).Err() + }) + + first, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "") + if err != nil { + t.Fatal(err) + } + if first.Ownership[adminChatFilterMine] != 1 { + t.Fatalf("首次统计 mine = %d, want 1", first.Ownership[adminChatFilterMine]) + } + + if err := db.Delete(&participant).Error; err != nil { + t.Fatal(err) + } + cached, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "") + if err != nil { + t.Fatal(err) + } + if cached.Ownership[adminChatFilterMine] != 1 { + t.Fatalf("15 秒内应读取快照,mine = %d, want 1", cached.Ownership[adminChatFilterMine]) + } + + repo.invalidateAdminChatCounts(ctx, []uint64{1}) + + stale, err := repo.AdminConversationCounts(ctx, principal, adminChatFilterMine, adminChatStageAll, "") + if err != nil { + t.Fatal(err) + } + if !stale.Refreshing { + t.Fatal("过期快照应标记 refreshing") + } + + raw, err := client.Get(t.Context(), cacheKey).Bytes() + if err != nil { + t.Fatal(err) + } + var snapshot adminChatCountsSnapshot + deadline := time.Now().Add(3 * time.Second) + for time.Now().Before(deadline) { + raw, err = client.Get(t.Context(), cacheKey).Bytes() + if err == nil && json.Unmarshal(raw, &snapshot) == nil && snapshot.Counts.Ownership[adminChatFilterMine] == 0 { + return + } + time.Sleep(50 * time.Millisecond) + } + t.Fatal("过期快照未在超时前完成后台刷新") +} diff --git a/backend/internal/modules/chat/message.go b/backend/internal/modules/chat/message.go index 616caa7e..b546618b 100644 --- a/backend/internal/modules/chat/message.go +++ b/backend/internal/modules/chat/message.go @@ -186,6 +186,7 @@ func (r *Repository) SendMessage(ctx context.Context, principal Principal, conve }, } tenantScopedEvent(r.db.WithContext(ctx), event) + r.invalidateAdminChatCounts(ctx, event.TenantIDs) r.hub.NotifyConversation(conversationID, event) // 同时广播给所有在线客服,使「未分配 / 全部」视图实时刷新; // 参与者客服会重复收到,由前端按消息 id 去重。 diff --git a/backend/internal/modules/chat/qrcode.go b/backend/internal/modules/chat/qrcode.go index 1a89178d..4dfa7c0c 100644 --- a/backend/internal/modules/chat/qrcode.go +++ b/backend/internal/modules/chat/qrcode.go @@ -805,6 +805,7 @@ func (r *Repository) notifyDeliveredQrCodeMessages(messages []model.ChatMessage) }, } tenantScopedEvent(r.db, event) + r.invalidateAdminChatCounts(context.Background(), event.TenantIDs) r.hub.NotifyConversation(msg.ConversationID, event) r.hub.NotifyAllAdmins(event) } diff --git a/backend/internal/modules/chat/repository.go b/backend/internal/modules/chat/repository.go index 76d1b5f4..cee0f57f 100644 --- a/backend/internal/modules/chat/repository.go +++ b/backend/internal/modules/chat/repository.go @@ -9,8 +9,10 @@ import ( "github.com/redis/go-redis/v9" "go.uber.org/zap" + "golang.org/x/sync/singleflight" "gorm.io/gorm" "gorm.io/gorm/clause" + "hfb_sys/backend/internal/model" "hfb_sys/backend/internal/modules/chathub" ) @@ -24,6 +26,7 @@ type Repository struct { ocrGate *ocrRequestGate ocrSleep func(context.Context, time.Duration) error ocrServiceURL string + countsGroup singleflight.Group } const ( @@ -177,14 +180,15 @@ func EnsureOrderConversation(tx *gorm.DB, order model.RentalOrder) (*model.ChatC return &conversation, nil } func (r *Repository) NotifyNewConversation(conversationID uint64) { - if r.hub == nil { - return - } event := &chathub.ChatEvent{ Type: "conversation_updated", ConversationID: conversationID, } tenantScopedEvent(r.db, event) + r.invalidateAdminChatCounts(context.Background(), event.TenantIDs) + if r.hub == nil { + return + } r.hub.NotifyConversation(conversationID, event) // 新会话(如未分配的平台咨询)需让所有在线客服的列表实时出现。 r.hub.NotifyAllAdmins(event) diff --git a/backend/internal/modules/chat/support.go b/backend/internal/modules/chat/support.go index d45a1533..3ae8ed80 100644 --- a/backend/internal/modules/chat/support.go +++ b/backend/internal/modules/chat/support.go @@ -30,8 +30,9 @@ var adminChatFilters = []string{adminChatFilterMine, adminChatFilterAll, adminCh var adminChatStages = []string{adminChatStageAll, adminChatStagePending, adminChatStageUnjoined, adminChatStageHandoff, adminChatStageRenting, adminChatStageAfterSale, adminChatStageEnded} type AdminConversationCountsDTO struct { - Ownership map[string]int64 `json:"ownership"` - Stages map[string]int64 `json:"stages"` + Ownership map[string]int64 `json:"ownership"` + Stages map[string]int64 `json:"stages"` + Refreshing bool `json:"refreshing,omitempty"` } func (r *Repository) TransferConversation(ctx context.Context, principal Principal, conversationID uint64, toAdminID uint64) error { @@ -98,10 +99,13 @@ func (r *Repository) TransferConversation(ctx context.Context, principal Princip } return nil }) - if err == nil && r.hub != nil { + if err == nil { event := tenantScopedEvent(r.db.WithContext(ctx), &chathub.ChatEvent{Type: "conversation_updated", ConversationID: conversationID}) - r.hub.NotifyConversation(conversationID, event) - r.hub.NotifyAllAdmins(event) + r.invalidateAdminChatCounts(ctx, event.TenantIDs) + if r.hub != nil { + r.hub.NotifyConversation(conversationID, event) + r.hub.NotifyAllAdmins(event) + } } return err } @@ -288,15 +292,25 @@ func (r *Repository) AdminConversationCounts(ctx context.Context, principal Prin filter = normalizeAdminChatFilter(filter) stage = normalizeAdminChatStage(stage) keyword = strings.TrimSpace(keyword) - if cached := r.loadAdminChatCountsCache(ctx, principal, filter, stage, keyword); cached != nil { - return cached, nil + if r.redis == nil || keyword != "" { + result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword) + if err != nil { + return nil, err + } + return result.DTO, nil } - result, err := r.adminConversationCounts(ctx, principal, filter, stage, keyword) - if err != nil { - return nil, err + + generation := r.adminChatCountsGeneration(ctx) + if cached := r.loadAdminChatCountsSnapshot(ctx, principal, filter, stage, keyword); cached != nil { + if cached.Generation == generation && time.Since(cached.GeneratedAt) <= adminChatCountsFreshFor { + return cached.Counts, nil + } + r.refreshAdminChatCountsAsync(ctx, principal, filter, stage, keyword) + stale := *cached.Counts + stale.Refreshing = true + return &stale, nil } - r.storeAdminChatCountsCache(ctx, principal, filter, stage, keyword, result.DTO) - return result.DTO, nil + return r.refreshAdminChatCounts(ctx, principal, filter, stage, keyword) } func (r *Repository) adminConversationTotal(ctx context.Context, principal Principal, filter string, stage string, keyword string) (int64, error) { @@ -397,7 +411,7 @@ func (r *Repository) adminConversationCounts(ctx context.Context, principal Prin StageAll, StagePending, StageUnjoined, StageHandoff, StageRenting, StageAfterSale, StageEnded int64 } var result row - if err := r.adminConversationBase(ctx, principal, keyword).Select(strings.Join(parts, ", "), args...).Scan(&result).Error; err != nil { + if err := r.adminConversationCountsBase(ctx, principal).Select(strings.Join(parts, ", "), args...).Scan(&result).Error; err != nil { return nil, err } return &adminConversationCountsResult{ @@ -409,6 +423,19 @@ func (r *Repository) adminConversationCounts(ctx context.Context, principal Prin }, nil } +// adminConversationCountsBase 只保留统计条件真正使用的关联。 +// 管理端的展示关联不参与计数,避免每次统计都额外访问状态表和商品表。 +func (r *Repository) adminConversationCountsBase(ctx context.Context, principal Principal) *gorm.DB { + db := r.db.WithContext(ctx).Table("chat_conversations AS c"). + Joins("LEFT JOIN chat_participants AS cp_me ON cp_me.conversation_id = c.id AND cp_me.participant_type = ? AND cp_me.participant_id = ?", principal.Type, principal.ID). + Joins("LEFT JOIN rental_orders AS explicit_lo ON explicit_lo.id = c.order_id") + scoped, err := adminConversationScope(ctx, db, principal.ID) + if err == nil { + return scoped + } + return tenantConversationScope(ctx, db) +} + func normalizeAdminChatFilter(filter string) string { switch filter { case adminChatFilterAll, adminChatFilterMine, adminChatFilterUnassigned: diff --git a/frontend/src/features/admin/views/AdminChatsView.vue b/frontend/src/features/admin/views/AdminChatsView.vue index 5abca114..8da5132a 100644 --- a/frontend/src/features/admin/views/AdminChatsView.vue +++ b/frontend/src/features/admin/views/AdminChatsView.vue @@ -76,6 +76,9 @@ const remarkEditing = ref(false) const remarkValue = ref('') let searchTimer: ReturnType | null = null let countsTimer: ReturnType | null = null +let countsRecheckTimer: ReturnType | null = null +let countsPollTimer: ReturnType | null = null +let countsRecheckAttempts = 0 let countsCacheKey = '' let countsLoadedAt = 0 @@ -203,11 +206,16 @@ onMounted(async () => { } keyword.value = firstQueryValue(route.query.chat_keyword) await Promise.all([loadConversations(), loadCounts(true), loadQuickReplies()]) + countsPollTimer = setInterval(() => { + if (!document.hidden) void loadCounts(true) + }, 30_000) }) onBeforeUnmount(() => { if (searchTimer) clearTimeout(searchTimer) if (countsTimer) clearTimeout(countsTimer) + if (countsRecheckTimer) clearTimeout(countsRecheckTimer) + if (countsPollTimer) clearInterval(countsPollTimer) }) async function loadConversations(showLoading = true, resetPage = false) { @@ -244,22 +252,37 @@ async function loadCounts(force = false) { keyword: keyword.value.trim(), } const cacheKey = JSON.stringify(query) - if (!force && cacheKey === countsCacheKey && Date.now() - countsLoadedAt < 5000) return + if (!force && cacheKey === countsCacheKey && Date.now() - countsLoadedAt < 10_000) return try { chatCounts.value = await fetchAdminChatCounts(query) countsCacheKey = cacheKey countsLoadedAt = Date.now() + if (chatCounts.value.refreshing && countsRecheckAttempts < 2) { + countsRecheckAttempts++ + if (countsRecheckTimer) clearTimeout(countsRecheckTimer) + countsRecheckTimer = setTimeout(() => { + countsRecheckTimer = null + void loadCounts(true) + }, 1500) + } else if (!chatCounts.value.refreshing) { + countsRecheckAttempts = 0 + } } catch { /* keep the last successful counts visible */ } } function scheduleCountsRefresh() { + countsRecheckAttempts = 0 + if (countsRecheckTimer) { + clearTimeout(countsRecheckTimer) + countsRecheckTimer = null + } if (countsTimer) clearTimeout(countsTimer) countsTimer = setTimeout(() => { countsTimer = null void loadCounts(true) - }, 1000) + }, 2000) } function handlePageChange(page: number) { diff --git a/frontend/src/features/chats/api/chats.ts b/frontend/src/features/chats/api/chats.ts index 0a37dc14..a1b6866c 100644 --- a/frontend/src/features/chats/api/chats.ts +++ b/frontend/src/features/chats/api/chats.ts @@ -66,6 +66,7 @@ export function resolveSupportScene(path: string): SupportScene { export interface AdminChatCounts { ownership?: Record stages?: Record + refreshing?: boolean } export interface AdminChatQuery {