fix(model):修复模型缓存问题导致租户分配了模型却不能用

This commit is contained in:
qian committed 2026-06-11 00:17:45 +08:00
1 parent 2425cbad23
commit c55e2d0164
2 files changed
+48 -21

No files matched your search

+21 -21
View File
@@ -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
+27
View File
@@ -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"`