-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
cache.go
62 lines (50 loc) · 1.02 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
61
62
package namesys
import (
"time"
path "github.com/ipfs/go-path"
)
func (ns *mpns) cacheGet(name string) (path.Path, bool) {
// existence of optional mapping defined via IPFS_NS_MAP is checked first
if ns.staticMap != nil {
val, ok := ns.staticMap[name]
if ok {
return val, true
}
}
if ns.cache == nil {
return "", false
}
ientry, ok := ns.cache.Get(name)
if !ok {
return "", false
}
entry, ok := ientry.(cacheEntry)
if !ok {
// should never happen, purely for sanity
log.Panicf("unexpected type %T in cache for %q.", ientry, name)
}
if time.Now().Before(entry.eol) {
return entry.val, true
}
ns.cache.Remove(name)
return "", false
}
func (ns *mpns) cacheSet(name string, val path.Path, ttl time.Duration) {
if ns.cache == nil || ttl <= 0 {
return
}
ns.cache.Add(name, cacheEntry{
val: val,
eol: time.Now().Add(ttl),
})
}
func (ns *mpns) cacheInvalidate(name string) {
if ns.cache == nil {
return
}
ns.cache.Remove(name)
}
type cacheEntry struct {
val path.Path
eol time.Time
}