-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
64 lines (52 loc) · 1.1 KB
/
cache.go
File metadata and controls
64 lines (52 loc) · 1.1 KB
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 main
import (
"sync"
)
// Cache for files by their paths.
type Cache interface {
List() []string
Get(path string) (file File, ok bool)
Add(path string, file File)
Remove(path string) (File, bool)
}
// NewCache instance.
func NewCache() Cache {
return &cache{
files: map[string]File{},
}
}
type cache struct {
mutex sync.RWMutex
files map[string]File
}
func (c *cache) List() []string {
c.mutex.RLock()
defer c.mutex.RUnlock()
list := make([]string, 0, len(c.files))
for path := range c.files {
list = append(list, path)
}
return list
}
func (c *cache) Get(path string) (File, bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
if val, ok := c.files[path]; ok {
return val.(File), true
}
return nil, false
}
func (c *cache) Add(path string, file File) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.files[path] = file
}
func (c *cache) Remove(path string) (File, bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
if val, ok := c.files[path]; ok {
delete(c.files, path)
return val.(File), true
}
return nil, false
}