-
Notifications
You must be signed in to change notification settings - Fork 49
/
cache.go
93 lines (80 loc) · 2.17 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package filemetadata
import (
"path/filepath"
"sync/atomic"
"github.com/bazelbuild/remote-apis-sdks/go/pkg/cache"
)
var globalCache cache.SingleFlight
// ResetGlobalCache clears the cache globally.
// Applies to all Cache instances created by NewSingleFlightCache.
func ResetGlobalCache() {
globalCache.Reset()
}
// Cache is a store for file digests that supports invalidation.
type fmCache struct {
Backend *cache.SingleFlight
cacheHits uint64
cacheMisses uint64
}
// NewSingleFlightCache returns a singleton-backed in-memory cache, with no validation.
func NewSingleFlightCache() Cache {
return &fmCache{Backend: &globalCache}
}
// Get retrieves the metadata of the file with the given filename, whether from cache or by
// computing the digest.
func (c *fmCache) Get(filename string) *Metadata {
abs, err := filepath.Abs(filename)
if err != nil {
return &Metadata{Err: err}
}
md, ch, err := c.loadMetadata(abs)
if err != nil {
return &Metadata{Err: err}
}
c.updateMetrics(ch)
return md
}
// Delete deletes an entry from cache.
func (c *fmCache) Delete(filename string) error {
abs, err := filepath.Abs(filename)
if err != nil {
return err
}
c.Backend.Delete(abs)
return nil
}
// Update updates the cache entry for the filename with the given value.
func (c *fmCache) Update(filename string, cacheEntry *Metadata) error {
abs, err := filepath.Abs(filename)
if err != nil {
return err
}
c.Backend.Store(abs, cacheEntry)
return nil
}
// GetCacheHits returns the number of cache hits.
func (c *fmCache) GetCacheHits() uint64 {
return atomic.LoadUint64(&c.cacheHits)
}
// GetCacheMisses returns the number of cache misses.
func (c *fmCache) GetCacheMisses() uint64 {
return atomic.LoadUint64(&c.cacheMisses)
}
func (c *fmCache) loadMetadata(filename string) (*Metadata, bool, error) {
cacheHit := true
val, err := c.Backend.LoadOrStore(filename, func() (interface{}, error) {
cacheHit = false
return Compute(filename), nil
})
if err != nil {
return nil, false, err
}
return val.(*Metadata), cacheHit, nil
}
func (c *fmCache) updateMetrics(cacheHit bool) {
if cacheHit {
atomic.AddUint64(&c.cacheHits, 1)
} else {
atomic.AddUint64(&c.cacheMisses, 1)
}
}