36 lines
742 B
Go
36 lines
742 B
Go
// 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
|
|
}
|