-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_cache.go
487 lines (434 loc) · 12 KB
/
model_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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package kvutil
import (
"context"
"encoding"
"errors"
"reflect"
"time"
"unsafe"
"github.com/jxskiss/gopkg/v2/collection/set"
"github.com/jxskiss/gopkg/v2/easy"
"github.com/jxskiss/gopkg/v2/internal/linkname"
"github.com/jxskiss/gopkg/v2/perf/lru"
"github.com/jxskiss/gopkg/v2/unsafe/reflectx"
)
// DefaultBatchSize is the default batch size for batch operations.
const DefaultBatchSize = 200
// DefaultLoaderBatchSize is the default batch size for calling Loader.
const DefaultLoaderBatchSize = 500
// DefaultLRUExpiration is the default expiration time for data in LRU cache.
const DefaultLRUExpiration = time.Second
var ErrDataNotFound = errors.New("data not found")
// Model is the interface implemented by types that can be cached by Cache.
type Model interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
// KVPair represents a key value pair to work with Cache.
type KVPair struct {
K string
V []byte
}
// Storage is the interface which provides storage for Cache.
// Users may use any key-value storage to implement this.
type Storage interface {
MGet(ctx context.Context, keys ...string) ([][]byte, error)
MSet(ctx context.Context, kvPairs []KVPair, expiration time.Duration) error
Delete(ctx context.Context, keys ...string) error
}
// Loader loads data from underlying persistent storage.
type Loader[K comparable, V Model] func(ctx context.Context, pks []K) (map[K]V, error)
// CacheConfig configures a Cache instance.
type CacheConfig[K comparable, V Model] struct {
// Storage must return a Storage implementation which will be used
// as the underlying key-value storage.
Storage func(ctx context.Context) Storage
// IDFunc returns the primary key of a Model object.
IDFunc func(V) K
// KeyFunc specifies the key function to use with the storage.
KeyFunc Key
// MGetBatchSize optionally specifies the batch size for one MGet
// calling to storage. The default is 200.
MGetBatchSize int
// MSetBatchSize optionally specifies the batch size for one MSet
// calling to storage. The default is 200.
MSetBatchSize int
// DeleteBatchSize optionally specifies the batch size for one Delete
// calling to storage. The default is 200.
DeleteBatchSize int
// LRUCache optionally enables LRU cache, which may help to improve
// the performance for high concurrency use-case.
LRUCache lru.Interface[K, V]
// LRUExpiration specifies the expiration time for data in LRU cache.
// The default is one second.
LRUExpiration time.Duration
// Loader optionally specifies a function to load data from underlying
// persistent storage when the data is missing from cache.
Loader Loader[K, V]
// LoaderBatchSize optionally specifies the batch size for calling
// Loader. The default is 500.
LoaderBatchSize int
// CacheExpiration specifies the expiration time to cache the data to
// Storage, when Loader is configured and data are loaded by Loader.
// The default is zero, which means no expiration.
CacheExpiration time.Duration
// CacheLoaderResultAsync makes the Cache to save data from Loader
// to Storage async, errors returned from Storage.MSet will be ignored.
// The default is false, it reports errors to the caller.
CacheLoaderResultAsync bool
}
func (p *CacheConfig[_, _]) checkAndSetDefaults() {
if p.MGetBatchSize <= 0 {
p.MGetBatchSize = DefaultBatchSize
}
if p.MSetBatchSize <= 0 {
p.MSetBatchSize = DefaultBatchSize
}
if p.DeleteBatchSize <= 0 {
p.DeleteBatchSize = DefaultBatchSize
}
if p.LRUExpiration <= 0 {
p.LRUExpiration = DefaultLRUExpiration
}
if p.LoaderBatchSize <= 0 {
p.LoaderBatchSize = DefaultLoaderBatchSize
}
}
func buildNewElemFunc[V any]() func() V {
var x V
typ := reflectx.RTypeOf(x)
if typ.Kind() == reflect.Ptr {
valTyp := typ.Elem()
return func() V {
elem := linkname.Reflect_unsafe_New(unsafe.Pointer(valTyp))
return typ.PackInterface(elem).(V)
}
}
return func() V {
return *new(V)
}
}
// NewCache returns a new Cache instance.
func NewCache[K comparable, V Model](config *CacheConfig[K, V]) *Cache[K, V] {
config.checkAndSetDefaults()
newElemFn := buildNewElemFunc[V]()
return &Cache[K, V]{
config: config,
newElemFunc: newElemFn,
}
}
// Cache encapsulates frequently used batching cache operations,
// such as MGet, MSet and Delete.
//
// A Cache must not be copied after initialized.
type Cache[K comparable, V Model] struct {
config *CacheConfig[K, V]
newElemFunc func() V
}
// Get queries Cache for a given pk.
//
// If pk cannot be found either in the cache nor from the Loader,
// it returns an error ErrDataNotFound.
func (p *Cache[K, V]) Get(ctx context.Context, pk K) (V, error) {
if p.config.LRUCache != nil {
val, exists := p.config.LRUCache.GetNotStale(pk)
if exists {
return val, nil
}
}
var zeroVal V
stor := p.config.Storage(ctx)
key := p.config.KeyFunc(pk)
cacheResult, err := stor.MGet(ctx, key)
if err != nil && p.config.Loader == nil {
return zeroVal, err
}
if len(cacheResult) > 0 && len(cacheResult[0]) > 0 {
elem := p.newElemFunc()
err = elem.UnmarshalBinary(cacheResult[0])
if err != nil {
return zeroVal, err
}
if p.config.LRUCache != nil {
p.config.LRUCache.Set(pk, elem, p.config.LRUExpiration)
}
return elem, nil
}
if p.config.Loader != nil {
var loaderResult map[K]V
loaderResult, err = p.config.Loader(ctx, []K{pk})
if err != nil {
return zeroVal, err
}
elem, exists := loaderResult[pk]
if exists {
if p.config.CacheLoaderResultAsync {
go func() {
_ = p.Set(ctx, pk, elem, p.config.CacheExpiration)
}()
} else {
err = p.Set(ctx, pk, elem, p.config.CacheExpiration)
if err != nil {
return zeroVal, err
}
}
return elem, nil
}
}
return zeroVal, ErrDataNotFound
}
// MGetSlice queries Cache and returns the cached values as a slice
// of type []V.
func (p *Cache[K, V]) MGetSlice(ctx context.Context, pks []K) ([]V, error) {
if len(pks) == 0 {
return nil, nil
}
// pk 去重
pks = easy.Unique(pks, false)
out := make([]V, 0, len(pks))
valfunc := func(pk K, elem V) {
out = append(out, elem)
}
err := p.mget(ctx, pks, valfunc)
return out, err
}
// MGetMap queries Cache and returns the cached values as a map
// of type map[K]V.
func (p *Cache[K, V]) MGetMap(ctx context.Context, pks []K) (map[K]V, error) {
if len(pks) == 0 {
return nil, nil
}
// pk 去重
pks = easy.Unique(pks, false)
out := make(map[K]V, len(pks))
valfunc := func(pk K, elem V) {
out[pk] = elem
}
err := p.mget(ctx, pks, valfunc)
return out, err
}
func (p *Cache[K, V]) mget(ctx context.Context, pks []K, f func(pk K, elem V)) error {
var lruMissingPKs []K
var lruMissingKeys []string
if p.config.LRUCache != nil {
lruResult := p.config.LRUCache.MGetNotStale(pks...)
lruMissingKeys = make([]string, 0, len(pks)-len(lruResult))
for _, pk := range pks {
if elem, ok := lruResult[pk]; ok {
f(pk, elem)
} else {
key := p.config.KeyFunc(pk)
lruMissingPKs = append(lruMissingPKs, pk)
lruMissingKeys = append(lruMissingKeys, key)
}
}
} else {
lruMissingPKs = pks
lruMissingKeys = make([]string, len(pks))
for i, pk := range pks {
key := p.config.KeyFunc(pk)
lruMissingKeys[i] = key
}
}
stor := p.config.Storage(ctx)
var err error
var batchValues [][]byte
var fromCache map[K]V
if p.config.LRUCache != nil {
fromCache = make(map[K]V, len(lruMissingKeys))
}
var cachedPKs = set.NewWithSize[K](len(lruMissingKeys))
var batchKeys = easy.Split(lruMissingKeys, p.config.MGetBatchSize)
for _, bat := range batchKeys {
batchValues, err = stor.MGet(ctx, bat...)
if err != nil {
return err
}
for _, val := range batchValues {
if len(val) == 0 {
continue
}
elem := p.newElemFunc()
err = elem.UnmarshalBinary(val)
if err != nil {
return err
}
pk := p.config.IDFunc(elem)
if fromCache != nil {
fromCache[pk] = elem
}
f(pk, elem)
cachedPKs.Add(pk)
}
}
// load from underlying persistent storage if configured
var fromLoader map[K]V
if p.config.Loader != nil && cachedPKs.Size() < len(lruMissingPKs) {
cacheMissingPKs := cachedPKs.FilterNotContains(lruMissingPKs)
fromLoader = make(map[K]V, len(cacheMissingPKs))
batchPKs := easy.Split(cacheMissingPKs, p.config.LoaderBatchSize)
for _, bat := range batchPKs {
batchResult, err := p.config.Loader(ctx, bat)
if err != nil {
return err
}
for pk, elem := range batchResult {
f(pk, elem)
fromLoader[pk] = elem
}
}
}
if len(fromCache) > 0 {
p.config.LRUCache.MSet(fromCache, p.config.LRUExpiration)
}
if len(fromLoader) > 0 {
if p.config.CacheLoaderResultAsync {
go func() {
_ = p.MSetMap(ctx, fromLoader, p.config.CacheExpiration)
}()
} else {
err = p.MSetMap(ctx, fromLoader, p.config.CacheExpiration)
if err != nil {
return err
}
}
}
return nil
}
// Set writes a key value pair to Cache.
func (p *Cache[K, V]) Set(ctx context.Context, pk K, elem V, expiration time.Duration) error {
key := p.config.KeyFunc(pk)
buf, err := elem.MarshalBinary()
if err != nil {
return err
}
kvPairs := []KVPair{{K: key, V: buf}}
stor := p.config.Storage(ctx)
err = stor.MSet(ctx, kvPairs, expiration)
if err != nil {
return err
}
if p.config.LRUCache != nil {
p.config.LRUCache.Set(pk, elem, p.config.LRUExpiration)
}
return nil
}
// MSetSlice writes the given models to Cache.
func (p *Cache[K, V]) MSetSlice(ctx context.Context, models []V, expiration time.Duration) error {
if len(models) == 0 {
return nil
}
stor := p.config.Storage(ctx)
batchSize := min(p.config.MSetBatchSize, len(models))
kvPairs := make([]KVPair, 0, batchSize)
for _, batchModels := range easy.Split(models, batchSize) {
kvPairs = kvPairs[:0]
var kvMap map[K]V
if p.config.LRUCache != nil {
kvMap = make(map[K]V, len(batchModels))
}
for _, elem := range batchModels {
buf, err := elem.MarshalBinary()
if err != nil {
return err
}
pk := p.config.IDFunc(elem)
key := p.config.KeyFunc(pk)
kvPairs = append(kvPairs, KVPair{key, buf})
if p.config.LRUCache != nil {
kvMap[pk] = elem
}
}
err := stor.MSet(ctx, kvPairs, expiration)
if err != nil {
return err
}
if p.config.LRUCache != nil {
p.config.LRUCache.MSet(kvMap, p.config.LRUExpiration)
}
}
return nil
}
// MSetMap writes the given models to Cache.
func (p *Cache[K, V]) MSetMap(ctx context.Context, models map[K]V, expiration time.Duration) error {
if len(models) == 0 {
return nil
}
stor := p.config.Storage(ctx)
batchSize := min(p.config.MSetBatchSize, len(models))
kvPairs := make([]KVPair, 0, batchSize)
for pk, elem := range models {
buf, err := elem.MarshalBinary()
if err != nil {
return err
}
key := p.config.KeyFunc(pk)
kvPairs = append(kvPairs, KVPair{key, buf})
if len(kvPairs) == batchSize {
err = stor.MSet(ctx, kvPairs, expiration)
if err != nil {
return err
}
kvPairs = kvPairs[:0]
}
}
if len(kvPairs) > 0 {
err := stor.MSet(ctx, kvPairs, expiration)
if err != nil {
return err
}
}
if p.config.LRUCache != nil {
p.config.LRUCache.MSet(models, p.config.LRUExpiration)
}
return nil
}
// Delete deletes key values from Cache.
func (p *Cache[K, V]) Delete(ctx context.Context, pks ...K) error {
if len(pks) == 0 {
return nil
}
if len(pks) > 1 {
return p.mDelete(ctx, pks)
}
pk := pks[0]
key := p.config.KeyFunc(pk)
stor := p.config.Storage(ctx)
err := stor.Delete(ctx, key)
if err != nil {
return err
}
if p.config.LRUCache != nil {
p.config.LRUCache.Delete(pk)
}
return nil
}
// mDelete deletes multiple key values from Cache.
func (p *Cache[K, V]) mDelete(ctx context.Context, pks []K) error {
if len(pks) == 0 {
return nil
}
keys := make([]string, 0, len(pks))
for _, pk := range pks {
key := p.config.KeyFunc(pk)
keys = append(keys, key)
}
stor := p.config.Storage(ctx)
batches := easy.Split(keys, p.config.DeleteBatchSize)
for _, bat := range batches {
err := stor.Delete(ctx, bat...)
if err != nil {
return err
}
}
if p.config.LRUCache != nil {
p.config.LRUCache.MDelete(pks...)
}
return nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}