fix(common): policy tag filter cache (#22485)

This commit is contained in:
屈轩
2025-05-08 15:56:57 +08:00
committed by GitHub
parent cc3477415d
commit f0858ce4d2
+15 -15
View File
@@ -204,18 +204,17 @@ func objIdQueryWithTagsOptimized(ctx context.Context, objIdSubQ *sqlchemy.SSubQu
if len(tags) == 0 {
continue
}
hashKeys := []string{idField, jsonutils.Marshal(tags).String(), modelName}
hash := fmt.Sprintf("%x", md5.Sum([]byte(jsonutils.Marshal(hashKeys).String())))
cache := tagsCache.Get(hash)
if cache != nil {
ids := cache.([]string)
ret = append(ret, ids...)
continue
}
objIdQ := objIdSubQ.Query()
objIdQ = objIdQ.AppendField(objIdQ.Field(idField))
for key, val := range tags {
hashKeys := []string{idField, key, modelName}
hashKeys = append(hashKeys, val...)
hash := fmt.Sprintf("%x", md5.Sum([]byte(jsonutils.Marshal(hashKeys).String())))
cache := tagsCache.Get(hash)
if cache != nil {
ids := cache.([]string)
ret = append(ret, ids...)
continue
}
sq := manager.Query("obj_id").Equals("obj_type", modelName).Equals("key", key)
if len(val) > 0 {
ssq := sq.In("value", val).SubQuery()
@@ -228,13 +227,14 @@ func objIdQueryWithTagsOptimized(ctx context.Context, objIdSubQ *sqlchemy.SSubQu
ssq := sq.SubQuery()
objIdQ = objIdQ.Join(ssq, sqlchemy.Equals(objIdQ.Field(idField), ssq.Field("obj_id")))
}
ids, err := FetchIds(objIdQ)
if err != nil {
continue
}
ret = append(ret, ids...)
tagsCache.AtomicSet(hash, ids)
}
ids, err := FetchIds(objIdQ.Distinct())
if err != nil {
log.Errorf("FetchIds %s %v", objIdQ.String(), err)
continue
}
ret = append(ret, ids...)
tagsCache.AtomicSet(hash, ids)
}
return ret
}