-
Notifications
You must be signed in to change notification settings - Fork 211
/
querycache.go
291 lines (259 loc) · 7.2 KB
/
querycache.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package sql
import (
"context"
"slices"
"sync"
"github.com/hashicorp/golang-lru/v2/simplelru"
)
const defaultLRUCacheSize = 100
type (
QueryCacheKind string
inGetValueCtxKey struct{}
)
var NullQueryCache QueryCache = (*queryCache)(nil)
type queryCacheKey struct {
Kind QueryCacheKind
Key string
}
// QueryCacheKey creates a key for QueryCache.
func QueryCacheKey(kind QueryCacheKind, key string) queryCacheKey {
return queryCacheKey{Kind: kind, Key: key}
}
// QueryCacheSubKey denotes a cache subkey. The empty subkey refers to the main
// key. All other subkeys are cleared by UpdateSlice for the key. The subkeys
// are intended to store data derived from the query results, such as serialized
// responses.
type QueryCacheSubKey string
const (
// When UpdateSlice method or AppendToCachedSlice function is
// called with mainSubKey, all other subkeys for the CacheKey
// are invalidated.
mainSubKey QueryCacheSubKey = ""
)
type (
// UntypedRetrieveFunc retrieves a value to be cached.
UntypedRetrieveFunc func(ctx context.Context) (any, error)
// SliceAppender modifies slice value stored in the cache, appending the
// specified item to it and returns the updated slice.
SliceAppender func(s any) any
)
// QueryCache stores results of SQL queries and data derived from these results.
// Presently, the cached entries are never removed, but eventually, it might
// become an LRU cache.
type QueryCache interface {
// IsCached returns true if the requests are being cached.
IsCached() bool
// GetValue retrieves the specified key+subKey value from the cache. If
// the entry is absent from cache, it's populated by calling retrieve func.
// Note that the retrieve func should never cause UpdateSlice to be
// called for this cache.
GetValue(
ctx context.Context,
key queryCacheKey,
subKey QueryCacheSubKey,
retrieve UntypedRetrieveFunc,
) (any, error)
// UpdateSlice updates the slice stored in the cache by invoking the
// specified SliceAppender. If the entry is not cached, the method does
// nothing.
UpdateSlice(key queryCacheKey, update SliceAppender)
// ClearCache empties the cache.
ClearCache()
}
// RetrieveFunc retrieves a value to be stored in the cache.
type RetrieveFunc[T any] func() (T, error)
// IsCached returns true if the database is cached.
func IsCached(db any) bool {
cache, ok := db.(QueryCache)
return ok && cache.IsCached()
}
// WithCachedValue retrieves the specified value from the cache. If the entry is
// absent from the cache, it's populated by calling retrieve func. Note that the
// retrieve func should never cause UpdateSlice to be called.
func WithCachedValue[T any](
ctx context.Context,
db any,
key queryCacheKey,
retrieve func(ctx context.Context) (T, error),
) (T, error) {
return WithCachedSubKey(ctx, db, key, mainSubKey, retrieve)
}
// WithCachedValue retrieves the specified value identified by the key and
// subKey from the cache. If the entry is absent from the cache, it's populated
// by calling retrieve func. Note that the retrieve func should never cause
// UpdateSlice to be called.
func WithCachedSubKey[T any](
ctx context.Context,
db any,
key queryCacheKey,
subKey QueryCacheSubKey,
retrieve func(ctx context.Context) (T, error),
) (T, error) {
cache, ok := db.(QueryCache)
if !ok {
return retrieve(ctx)
}
v, err := cache.GetValue(
ctx, key, subKey,
func(ctx context.Context) (any, error) {
return retrieve(ctx)
})
if err != nil {
var r T
return r, err
}
return v.(T), nil
}
// AppendToCachedSlice adds a value to the slice stored in the cache by invoking
// the specified SliceAppender. If the entry is not cached, the function does
// nothing.
func AppendToCachedSlice[T any](db any, key queryCacheKey, v T) {
if cache, ok := db.(QueryCache); ok {
cache.UpdateSlice(key, func(s any) any {
if s == nil {
return []T{v}
}
return append(s.([]T), v)
})
}
}
type lruCacheKey struct {
key string
subKey QueryCacheSubKey
}
type lru = simplelru.LRU[lruCacheKey, any]
type queryCache struct {
sync.Mutex
updateMtx sync.RWMutex
subKeyMap map[queryCacheKey][]QueryCacheSubKey
cacheSizesByKind map[QueryCacheKind]int
caches map[QueryCacheKind]*lru
}
var _ QueryCache = &queryCache{}
func (c *queryCache) ensureLRU(kind QueryCacheKind) *lru {
if lruForKind, found := c.caches[kind]; found {
return lruForKind
}
size, found := c.cacheSizesByKind[kind]
if !found || size <= 0 {
size = defaultLRUCacheSize
}
lruForKind, err := simplelru.NewLRU[lruCacheKey, any](size, func(k lruCacheKey, v any) {
if k.subKey == mainSubKey {
c.clearSubKeys(queryCacheKey{Kind: kind, Key: k.key})
}
})
if err != nil {
panic("NewLRU failed: " + err.Error())
}
if c.caches == nil {
c.caches = make(map[QueryCacheKind]*lru)
}
c.caches[kind] = lruForKind
return lruForKind
}
func (c *queryCache) clearSubKeys(key queryCacheKey) {
lru, found := c.caches[key.Kind]
if !found {
return
}
for _, sk := range c.subKeyMap[key] {
lru.Remove(lruCacheKey{
key: key.Key,
subKey: sk,
})
}
}
func (c *queryCache) get(key queryCacheKey, subKey QueryCacheSubKey) (any, bool) {
c.Lock()
defer c.Unlock()
lru, found := c.caches[key.Kind]
if !found {
return nil, false
}
return lru.Get(lruCacheKey{
key: key.Key,
subKey: subKey,
})
}
func (c *queryCache) set(key queryCacheKey, subKey QueryCacheSubKey, v any) {
c.Lock()
defer c.Unlock()
if subKey != mainSubKey {
sks := c.subKeyMap[key]
if slices.Index(sks, subKey) < 0 {
if c.subKeyMap == nil {
c.subKeyMap = make(map[queryCacheKey][]QueryCacheSubKey)
}
c.subKeyMap[key] = append(sks, subKey)
}
}
lru := c.ensureLRU(key.Kind)
lru.Add(lruCacheKey{key: key.Key, subKey: subKey}, v)
}
func (c *queryCache) IsCached() bool {
return c != nil
}
func (c *queryCache) GetValue(
ctx context.Context,
key queryCacheKey,
subKey QueryCacheSubKey,
retrieve UntypedRetrieveFunc,
) (any, error) {
if c == nil {
return retrieve(ctx)
}
// Avoid recursive locking from within retrieve()
if ctx.Value(inGetValueCtxKey{}) == nil {
c.updateMtx.RLock()
defer c.updateMtx.RUnlock()
}
v, found := c.get(key, subKey)
var err error
if !found {
// This may seem like a race, but at worst, retrieve() will be
// called several times when populating this cached entry.
// That's better than locking for the duration of retrieve(),
// which can also refer to this cache
v, err = retrieve(context.WithValue(ctx, inGetValueCtxKey{}, true))
if err == nil {
c.set(key, subKey, v)
}
}
return v, err
}
func (c *queryCache) UpdateSlice(key queryCacheKey, update SliceAppender) {
if c == nil {
return
}
// Here we lock for the call b/c we can't have conflicting updates for
// the slice at the same time
c.updateMtx.Lock()
c.Lock()
defer func() {
c.Unlock()
c.updateMtx.Unlock()
}()
lru, found := c.caches[key.Kind]
if !found {
return
}
k := lruCacheKey{key: key.Key, subKey: mainSubKey}
if v, found := lru.Get(k); found {
lru.Add(k, update(v))
c.clearSubKeys(key)
}
}
func (c *queryCache) ClearCache() {
if c == nil {
return
}
c.updateMtx.Lock()
c.Lock()
defer func() {
c.Unlock()
c.updateMtx.Unlock()
}()
// No need to clear c.subKeyMap as it's only used to keep track of possible subkeys for each key
c.caches = nil
}