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"`