From c55e2d0164e20d6b2ebadade1d714665be9dd8d7 Mon Sep 17 00:00:00 2001 From: qian Date: Thu, 11 Jun 2026 00:17:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(model):=E4=BF=AE=E5=A4=8D=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E9=97=AE=E9=A2=98=E5=AF=BC=E8=87=B4=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E5=88=86=E9=85=8D=E4=BA=86=E6=A8=A1=E5=9E=8B=E5=8D=B4?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/admin/model.go | 42 ++++++++++++++--------------- internal/logic/admin/model_group.go | 27 +++++++++++++++++++ 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/internal/logic/admin/model.go b/internal/logic/admin/model.go index 78c0438..cea92b2 100644 --- a/internal/logic/admin/model.go +++ b/internal/logic/admin/model.go @@ -306,29 +306,29 @@ func (s *sAdmin) UpdateModel(ctx context.Context, req *v1.ModelUpdateReq) (*v1.M return nil, err } - // 状态从 active → deprecated 时发送通知并清除缓存 - if statusChanged && req.Status == "deprecated" && oldModel.Status == "active" { - go func() { - bgCtx := context.Background() - variables := g.Map{ - "model_name": oldModel.ModelId, - "sunset_date": "", - "replacement_model": req.ReplacementModel, - } - if req.SunsetDate != nil { - variables["sunset_date"] = *req.SunsetDate - } - engine := common.NewNotificationEngine() - if err := engine.SendToAllTenants(bgCtx, "model_deprecated", variables, ""); err != nil { - g.Log().Errorf(bgCtx, "[ModelDeprecation] send notification for %s failed: %v", oldModel.ModelId, err) - } - }() + if statusChanged { + // 状态变更时清除模型缓存和租户分组缓存 relay.NewDataProvider().InvalidateModelCache(oldModel.ModelId) - } + invalidateTenantsForModel(ctx, req.ID) - // 状态从 deprecated → active 时清除缓存 - if statusChanged && req.Status == "active" && oldModel.Status == "deprecated" { - relay.NewDataProvider().InvalidateModelCache(oldModel.ModelId) + // 状态从 active → deprecated 时额外发送通知 + if req.Status == "deprecated" && oldModel.Status == "active" { + go func() { + bgCtx := context.Background() + variables := g.Map{ + "model_name": oldModel.ModelId, + "sunset_date": "", + "replacement_model": req.ReplacementModel, + } + if req.SunsetDate != nil { + variables["sunset_date"] = *req.SunsetDate + } + engine := common.NewNotificationEngine() + if err := engine.SendToAllTenants(bgCtx, "model_deprecated", variables, ""); err != nil { + g.Log().Errorf(bgCtx, "[ModelDeprecation] send notification for %s failed: %v", oldModel.ModelId, err) + } + }() + } } return nil, nil diff --git a/internal/logic/admin/model_group.go b/internal/logic/admin/model_group.go index 8a8064d..d89930d 100644 --- a/internal/logic/admin/model_group.go +++ b/internal/logic/admin/model_group.go @@ -346,6 +346,33 @@ func invalidateTenantGroupCache(ctx context.Context, tenantID int64) { common.TenantGroupModelCache.Delete(ctx, fmt.Sprintf("%d", tenantID)) } +func invalidateTenantsForModel(ctx context.Context, modelID int64) { + var groupIDs []struct { + GroupId int64 `json:"group_id"` + } + dao.MdlGroupModels.Ctx(ctx). + Where("model_id", modelID). + Fields("group_id"). + Scan(&groupIDs) + + for _, g := range groupIDs { + invalidateTenantsInGroup(ctx, g.GroupId) + } + + // 同时清除直接分配了该模型的租户缓存(mdl_tenant_models) + var tenantIDs []struct { + TenantId int64 `json:"tenant_id"` + } + dao.MdlTenantModels.Ctx(ctx). + Where("model_id", modelID). + Fields("tenant_id"). + Scan(&tenantIDs) + + for _, t := range tenantIDs { + invalidateTenantGroupCache(ctx, t.TenantId) + } +} + func invalidateTenantsInGroup(ctx context.Context, groupID int64) { var tenants []struct { TenantId int64 `json:"tenant_id"`