This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
/
cached_app_info_store.go
112 lines (93 loc) · 2.42 KB
/
cached_app_info_store.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package collector
import (
"log"
"strings"
"sync"
"time"
)
// CachedAppInfoStore caches app info lookups against the APIStore.
type CachedAppInfoStore struct {
store AppInfoStore
cacheTTL time.Duration
cacheLastCleared time.Time
mu sync.Mutex
cache map[AppGUID]AppInfo
}
// NewCachedAppInfoStore initializes a CachedAppInfoStore.
func NewCachedAppInfoStore(s AppInfoStore, opts ...CachedAppInfoStoreOption) *CachedAppInfoStore {
c := &CachedAppInfoStore{
store: s,
cache: make(map[AppGUID]AppInfo),
cacheTTL: 150 * time.Second,
cacheLastCleared: time.Now(),
}
for _, opt := range opts {
opt(c)
}
return c
}
// Lookup associates AppInfo for a particular app GUID.
func (c *CachedAppInfoStore) Lookup(guids []string) (map[AppGUID]AppInfo, error) {
if c.cacheLastCleared.Add(c.cacheTTL).Before(time.Now()) {
c.cache = make(map[AppGUID]AppInfo)
c.cacheLastCleared = time.Now()
}
var toLookup []string
cached := make(map[AppGUID]AppInfo)
c.mu.Lock()
for _, g := range guids {
appInfo, ok := c.cache[AppGUID(g)]
if !ok {
toLookup = append(toLookup, g)
continue
}
cached[AppGUID(g)] = appInfo
}
c.mu.Unlock()
if len(toLookup) == 0 {
return cached, nil
}
fresh, err := c.store.Lookup(toLookup)
if err != nil {
log.Printf("call to HTTP store failed: %s", err)
return cached, nil
}
c.updateCache(fresh)
return merge(fresh, cached), nil
}
func (c *CachedAppInfoStore) updateCache(fresh map[AppGUID]AppInfo) {
c.mu.Lock()
defer c.mu.Unlock()
for k, v := range fresh {
c.cache[k] = v
}
}
type CachedAppInfoStoreOption func(*CachedAppInfoStore)
func WithCacheTTL(cacheTTL time.Duration) CachedAppInfoStoreOption {
return func(c *CachedAppInfoStore) {
c.cacheTTL = cacheTTL
}
}
// GUIDIndex is a concatentation of GUID and instance index in the format
// some-guid/some-index, e.g., 7b8228a0-cf40-42d8-a7bb-b287a88198a3/0
type GUIDIndex string
// GUID returns the GUID of the GUIDIndex
func (g GUIDIndex) GUID() string {
return strings.Split(string(g), "/")[0]
}
// Index returns the Index of the GUIDIndex
func (g GUIDIndex) Index() string {
parts := strings.Split(string(g), "/")
if len(parts) < 2 {
return "0"
}
return parts[1]
}
// merge combines two maps. If a key exists in both a and b, then a's value
// takes precedence.
func merge(a, b map[AppGUID]AppInfo) map[AppGUID]AppInfo {
for k, v := range a {
b[k] = v
}
return b
}