Optimize asset cache lookups and add accessors (#16894)

* optimize asset cache lookups and add accessors.

* adopt cache.FilterAssets to optimize asset processing.
This commit is contained in:
Jane Haring 2026-02-07 10:00:49 +08:00 committed by GitHub
parent 3f3db3c34f
commit 82647de886
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 75 additions and 25 deletions

38
kernel/cache/asset.go vendored
View file

@ -104,17 +104,53 @@ var (
assetsLock = sync.Mutex{}
)
// GetAssets 返回所有资源的副本
func GetAssets() (ret map[string]*Asset) {
assetsLock.Lock()
defer assetsLock.Unlock()
ret = map[string]*Asset{}
// 指定长度避免分配新内存时扩容和迁移
ret = make(map[string]*Asset, len(assetsCache))
for k, v := range assetsCache {
ret[k] = v
}
return
}
// IterateAssets 遍历所有资源,适合只读场景
func IterateAssets(fn func(path string, asset *Asset) bool) {
assetsLock.Lock()
defer assetsLock.Unlock()
for path, asset := range assetsCache {
if !fn(path, asset) {
break
}
}
}
// FilterAssets 根据过滤函数返回符合条件的资源
func FilterAssets(filter func(path string, asset *Asset) bool) (ret map[string]*Asset) {
assetsLock.Lock()
defer assetsLock.Unlock()
ret = make(map[string]*Asset)
for path, asset := range assetsCache {
if filter(path, asset) {
ret[path] = asset
}
}
return
}
// GetAssetByPath 根据路径获取资源
func GetAssetByPath(path string) *Asset {
assetsLock.Lock()
defer assetsLock.Unlock()
return assetsCache[path]
}
func RemoveAsset(path string) {
assetsLock.Lock()
defer assetsLock.Unlock()