Initial commit (code only without large binaries)
This commit is contained in:
35
EdgeNode/internal/utils/fs/stat.go
Normal file
35
EdgeNode/internal/utils/fs/stat.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package fsutils
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var locker = &sync.RWMutex{}
|
||||
var cacheMap = map[string]*StatResult{} // path => StatResult
|
||||
|
||||
const cacheLife = 3 // seconds
|
||||
|
||||
// StatDeviceCache stat device with cache
|
||||
func StatDeviceCache(path string) (*StatResult, error) {
|
||||
locker.RLock()
|
||||
stat, ok := cacheMap[path]
|
||||
if ok && stat.updatedAt >= fasttime.Now().Unix()-cacheLife {
|
||||
locker.RUnlock()
|
||||
return stat, nil
|
||||
}
|
||||
locker.RUnlock()
|
||||
|
||||
locker.Lock()
|
||||
defer locker.Unlock()
|
||||
|
||||
stat, err := StatDevice(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cacheMap[path] = stat
|
||||
return stat, nil
|
||||
}
|
||||
Reference in New Issue
Block a user