/
cache.go
144 lines (119 loc) · 2.42 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package prago
import (
"sync"
"time"
)
//use https://github.com/sourcegraph/conc
const staleInterval = 10 * time.Minute
type cache struct {
items map[string]*cacheItem
mutex *sync.RWMutex
}
type cacheItem struct {
updatedAt time.Time
updating bool
value interface{}
createFn func() interface{}
mutex *sync.RWMutex
}
func newCache() *cache {
return &cache{
items: map[string]*cacheItem{},
mutex: &sync.RWMutex{},
}
}
func (ci cacheItem) isStale() bool {
ci.mutex.RLock()
defer ci.mutex.RUnlock()
return ci.updatedAt.Add(staleInterval).Before(time.Now())
}
func (ci cacheItem) getValue() interface{} {
ci.mutex.RLock()
defer ci.mutex.RUnlock()
return ci.value
}
func (ci *cacheItem) reloadValue() {
ci.mutex.RLock()
if ci.updating {
ci.mutex.RUnlock()
return
}
ci.mutex.RUnlock()
ci.mutex.Lock()
ci.updating = true
ci.mutex.Unlock()
var val interface{}
var panicked bool
defer func() {
if err := recover(); err != nil {
panicked = true
}
}()
val = ci.createFn()
ci.mutex.Lock()
if !panicked {
ci.value = val
ci.updatedAt = time.Now()
}
ci.updating = false
ci.mutex.Unlock()
}
func (c *cache) getItem(name string) *cacheItem {
c.mutex.RLock()
defer c.mutex.RUnlock()
item, found := c.items[name]
if !found {
return nil
}
return item
}
func (c *cache) putItem(name string, createFn func() interface{}) *cacheItem {
item := &cacheItem{
updatedAt: time.Now(),
value: createFn(),
createFn: createFn,
mutex: &sync.RWMutex{},
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.items[name] = item
return item
}
func loadCache[T any](c *cache, name string, createFn func() T) T {
fn := func() interface{} {
return createFn()
}
item := c.getItem(name)
if item == nil {
item := c.putItem(name, fn)
return item.getValue().(T)
}
if item.isStale() {
go func() {
item.reloadValue()
}()
return item.getValue().(T)
}
return item.getValue().(T)
}
func Cached[T any](app *App, name string, createFn func() T) chan T {
ret := make(chan T)
go func() {
val := loadCache(app.cache, name, createFn)
ret <- val
}()
return ret
}
func (app *App) ClearCache() {
app.cache.clear()
app.userDataCacheDeleteAll()
}
func (c *cache) forceLoad(cacheName string, createFn func() interface{}) interface{} {
item := c.putItem(cacheName, createFn)
return item.getValue()
}
func (c *cache) clear() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.items = map[string]*cacheItem{}
}