-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileinfocache.go
64 lines (57 loc) · 1.41 KB
/
fileinfocache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fs
import "time"
// FileInfoCache is a cache with timeout for FileInfo data.
type FileInfoCache struct {
infos map[string]fileInfoCacheEntry
timeout time.Duration
}
type fileInfoCacheEntry struct {
*FileInfo
time time.Time
}
// NewFileInfoCache returns a new FileInfoCache with timeout,
// or nil if timeout is zero. It is valid to call the methods
// of FileInfoCache for a nil pointer.
func NewFileInfoCache(timeout time.Duration) *FileInfoCache {
if timeout == 0 {
return nil
}
return &FileInfoCache{
infos: make(map[string]fileInfoCacheEntry),
timeout: timeout,
}
}
// Put puts or updates a FileInfo for a path.
func (cache *FileInfoCache) Put(path string, info *FileInfo) {
if cache == nil {
return
}
cache.infos[path] = fileInfoCacheEntry{
FileInfo: info,
time: time.Now(),
}
}
// Get returns the FileInfo for a path or nil and false
// if there is no FileInfo for the path or the FileInfo
// has timed out.
func (cache *FileInfoCache) Get(path string) (info *FileInfo, ok bool) {
if cache == nil {
return nil, false
}
entry, ok := cache.infos[path]
if !ok {
return nil, false
}
if entry.time.Add(cache.timeout).Before(time.Now()) {
delete(cache.infos, path)
return nil, false
}
return entry.FileInfo, true
}
// Delete deletes the FileInfo with path if was cached.
func (cache *FileInfoCache) Delete(path string) {
if cache == nil {
return
}
delete(cache.infos, path)
}