forked from muesli/go-gitignore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
60 lines (50 loc) · 1.3 KB
/
cache.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
package gitignore
import (
"sync"
)
// Cache is the interface for the GitIgnore cache
type Cache interface {
// Set stores the GitIgnore ignore against its path.
Set(path string, ig GitIgnore)
// Get attempts to retrieve an GitIgnore instance associated with the given
// path. If the path is not known nil is returned.
Get(path string) GitIgnore
}
// cache is the default thread-safe cache implementation
type cache struct {
_i map[string]GitIgnore
_lock sync.Mutex
}
// NewCache returns a Cache instance. This is a thread-safe, in-memory cache
// for GitIgnore instances.
func NewCache() Cache {
return &cache{}
} // Cache()
// Set stores the GitIgnore ignore against its path.
func (c *cache) Set(path string, ignore GitIgnore) {
if ignore == nil {
return
}
// ensure the map is defined
if c._i == nil {
c._i = make(map[string]GitIgnore)
}
// set the cache item
c._lock.Lock()
c._i[path] = ignore
c._lock.Unlock()
} // Set()
// Get attempts to retrieve an GitIgnore instance associated with the given
// path. If the path is not known nil is returned.
func (c *cache) Get(path string) GitIgnore {
c._lock.Lock()
_ignore, _ok := c._i[path]
c._lock.Unlock()
if _ok {
return _ignore
} else {
return nil
}
} // Get()
// ensure cache supports the Cache interface
var _ Cache = &cache{}