forked from vektah/dataloaden
/
template.go
906 lines (810 loc) · 30.1 KB
/
template.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
package generator
import "text/template"
var tpl = template.Must(template.New("generated").
Funcs(template.FuncMap{
"lcFirst": lcFirst,
"loadThunkMissReturnType": LoadThunkMissReturnType,
"LoadThunkMarshalType": LoadThunkMarshalType,
"LoadAllMarshalType": LoadAllMarshalType,
"ToRedisKey": ToRedisKey,
}).
Parse(`
// Code generated by github.com/viocle/dataloaden, DO NOT EDIT.
package {{.Package}}
import (
"context"
"encoding/json"
"strconv"
"sync"
"time"
{{if .KeyType.ImportPath}}"{{.KeyType.ImportPath}}"{{end}}
{{if .ValType.ImportPath}}"{{.ValType.ImportPath}}"{{end}}
)
const (
{{.Name}}CacheKeyPrefix = "{DataLoader{{.Name}}}:"
)
var (
Err{{.Name}}GetManyLength = errors.New("redis error, invalid length returned from GetManyFunc")
)
// {{.Name}}Config captures the config to create a new {{.Name}}
type {{.Name}}Config struct {
// Fetch is a method that provides the data for the loader
Fetch func(keys []{{.KeyType.String}}) ([]{{.ValType.String}}, []error)
// Wait is how long to wait before sending a batch
Wait time.Duration
// MaxBatch will limit the maximum number of keys to send in one batch, 0 = no limit
MaxBatch int
{{ if not .DisableCacheExpiration }}
// ExpireAfter determines how long until cached items expire. Set to 0 to disable expiration
ExpireAfter time.Duration
{{ end }}
// HookExternalCacheGet is a method that provides the ability to lookup a key in an external cache with an external hook.
// This replaces the use of the internal cache.
// If the key is found in the external cache, the value should be returned along with true.
// If the key is not found in the external cache, an empty/nil value should be returned along with false.
// Both HookExternalCacheGet, HookExternalCacheSet, HookExternalCacheDelete, and HookExternalCacheClearAll should be set if using an external cache.
HookExternalCacheGet func(key {{.KeyType.String}}) ({{.ValType.String}}, bool)
// HookExternalCacheSet is a method that provides the ability to set a key in an external cache with an external hook.
// This replaces the use of the internal cache.
HookExternalCacheSet func(key {{.KeyType.String}}, value {{.ValType.String}}) error
// HookBeforeFetch is a method that provides the ability to delete/clear a key in an external cache with an external hook.
// This replaces the use of the internal cache.
HookExternalCacheDelete func(key {{.KeyType.String}}) error
// HookExternalCacheClearAll is a method that provides the ability to clear all keys in an external cache with an external hook.
HookExternalCacheClearAll func() error
// HookBeforeFetch is called right before a fetch is performed
HookBeforeFetch func(keys []{{.KeyType.String}}, loaderName string)
// HookAfterFetch is called right after a fetch is performed
HookAfterFetch func(keys []{{.KeyType.String}}, loaderName string)
// HookAfterSet is called after a value is set in the cache
HookAfterSet func(key {{.KeyType.String}}, value {{.ValType.String}})
// HookAfterPrime is called after a value is primed in the cache using Prime or ForcePrime
HookAfterPrime func(key {{.KeyType.String}}, value {{.ValType.String}})
// HookAfterClear is called after a value is cleared from the cache
HookAfterClear func(key {{.KeyType.String}})
// HookAfterClearAll is called after all values are cleared from the cache
HookAfterClearAll func()
// HookAfterExpired is called after a value is cleared in the cache due to expiration
HookAfterExpired func(key {{.KeyType.String}})
// RedisConfig is used to configure a {{.Name}} backed by Redis, disabling the internal cache.
RedisConfig *{{.Name}}RedisConfig
}
{{ if not .DisableCacheExpiration }}
// {{.Name}}CacheItem defines a cache item when using dataloader cache expiration where expireAfter > 0
type {{.Name}}CacheItem struct {
// Expires contains the time this CacheItem expires
Expires int64
// Value contains the cached {{.ValType.String}}
Value {{.ValType.String}}
}
// expired returns true if the cache item has expired
func (c *{{.Name}}CacheItem) expired(now int64) bool {
return c.Expires < now
}
{{ end }}
// New{{.Name}} creates a new {{.Name}} given a fetch, wait, and maxBatch
func New{{.Name}}(config {{.Name}}Config) *{{.Name}} {
l := &{{.Name}}{
fetch: config.Fetch,
wait: config.Wait,
maxBatch: config.MaxBatch,{{ if not .DisableCacheExpiration }}
expireAfter:config.ExpireAfter.Nanoseconds(),{{ end }}
hookExternalCacheGet: config.HookExternalCacheGet,
hookExternalCacheSet: config.HookExternalCacheSet,
hookExternalCacheDelete: config.HookExternalCacheDelete,
hookExternalCacheClearAll: config.HookExternalCacheClearAll,
hookBeforeFetch: config.HookBeforeFetch,
hookAfterFetch: config.HookAfterFetch,
hookAfterSet: config.HookAfterSet,
hookAfterPrime: config.HookAfterPrime,
hookAfterClear: config.HookAfterClear,
hookAfterClearAll: config.HookAfterClearAll,
hookAfterExpired: config.HookAfterExpired,
redisConfig: config.RedisConfig,
}
if config.RedisConfig != nil {
// validate we have all the required Redis functions. If not, force disable Redis
if l.redisConfig.GetFunc != nil && l.redisConfig.SetFunc != nil && l.redisConfig.DeleteFunc != nil {
// all required Redis functions are present, enable Redis
l.redisConfig = &{{.Name}}RedisConfig{
SetTTL: config.RedisConfig.SetTTL, // optional
GetFunc: config.RedisConfig.GetFunc, // (GET)
GetManyFunc: config.RedisConfig.GetManyFunc, // (MGET) optional, but recommended for LoadAll performance
SetFunc: config.RedisConfig.SetFunc, // (SET)
DeleteFunc: config.RedisConfig.DeleteFunc, // (DEL)
DeleteManyFunc: config.RedisConfig.DeleteManyFunc, // (DEL) optional, but recommended for ClearAll performance
GetKeysFunc: config.RedisConfig.GetKeysFunc, // optional, but recommended for ClearAll support
ObjMarshal: config.RedisConfig.ObjMarshal, // optional
ObjUnmarshal: config.RedisConfig.ObjUnmarshal, // optional
KeyToStringFunc: config.RedisConfig.KeyToStringFunc, // optional, but recommended for complex types that need to be serialized
}
if l.redisConfig.ObjMarshal == nil || l.redisConfig.ObjUnmarshal == nil {
// missing ObjMarshal or ObjUnmarshal, force use of json package
l.redisConfig.ObjMarshal = json.Marshal
l.redisConfig.ObjUnmarshal = json.Unmarshal
}
// set batchResultSet to just call the SetFunc directly, no locks needed
l.batchResultSet = func(key {{.KeyType.String}}, value {{.ValType.String}}) {
l.redisConfig.SetFunc(context.Background(), {{.Name}}CacheKeyPrefix+{{ToRedisKey .KeyType.String }}, value, l.redisConfig.SetTTL)
}
if l.redisConfig.KeyToStringFunc == nil {
l.redisConfig.KeyToStringFunc = l.Marshal{{.Name}}ToString
}
}
}
if l.redisConfig == nil {
// set the default batchResultSet
l.batchResultSet = func(key {{.KeyType.String}}, value {{.ValType.String}}) {
l.mu.Lock()
l.unsafeSet(key, value)
l.mu.Unlock()
}
}
l.batchPool = sync.Pool{
New: func() interface{} {
return l.createNewBatch()
},
}
return l
}
// {{.Name}}RedisConfig is used to configure a {{.Name}} backed by Redis. GetFunc, SetFunc, and DeleteFunc are required if using Redis. If any function is not provided, Redis will be disabled and internal caching will be used.
type {{.Name}}RedisConfig struct {
// SetTTL is the TTL (Time To Live) for a key to live in Redis on set. If nil, no TTL will be set.
SetTTL *time.Duration
// GetFunc should get a value from Redis given a key and return the raw string value.
GetFunc func(ctx context.Context, key string) (string, error)
// GetManyFunc should get one or more values from Redis given a set of keys and return the raw string values, errors the size of keys with non nil values for keys not found, and an error if any other error occurred running the command
// If not set then GetFunc will be used instead, but will be called one at a time for each key
GetManyFunc func(ctx context.Context, keys []string) ([]string, []error, error)
// SetFunc should set a value in Redis given a key and value with an optional ttl (Time To Live)
SetFunc func(ctx context.Context, key string, value interface{}, ttl *time.Duration) error
// DeleteFunc should delete a value in Redis given a key
DeleteFunc func(ctx context.Context, key string) error
// DeleteManyFunc should delete one or more values in Redis given a set of keys
DeleteManyFunc func(ctx context.Context, key []string) error
// GetKeysFunc should return all keys in Redis matching the given pattern. If not set then ClearAll() for this dataloader will not be supported.
GetKeysFunc func(ctx context.Context, pattern string) ([]string, error)
// ObjMarshal provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
ObjMarshal func(any) ([]byte, error)
// ObjUnmarshaler provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
ObjUnmarshal func([]byte, any) error
// KeyToStringFunc provides you the ability to specify your own function to convert a key to a string, which will be used instead of serialization.
// This is only used for non standard types that need to be serialized. If not set, the ObjMarshal function (user defined or default) will be used to serialize a key into a string value
// Example: If you have a struct with a String() function that returns a string representation of the struct, you can set this function to that function.
//
// type MyStruct struct {
// ID string
// OrgID string
// }
// ...
// {{.Name}}RedisConfig{
// KeyToStringFunc = func(key {{.KeyType.String}}) string { return m.ID + ":" + m.OrgID }
// }
// ...
// Or if your key type has a String() function that returns a string representation of the key, you can set this function like this:
// {{.Name}}RedisConfig{
// KeyToStringFunc = func(key {{.KeyType.String}}) string { return key.String() }
// }
KeyToStringFunc func(key {{.KeyType.String}}) string
}
// {{.Name}} batches and caches requests
type {{.Name}} struct {
// this method provides the data for the loader
fetch func(keys []{{.KeyType.String}}) ([]{{.ValType.String}}, []error)
// optional Redis configuration
redisConfig *{{.Name}}RedisConfig
// lazily created cache
{{ if not .DisableCacheExpiration }}
cacheExpire map[{{.KeyType.String}}]*{{.Name}}CacheItem
{{ end }}
cache map[{{.KeyType.String}}]{{.ValType.String}}
// the current batch. keys will continue to be collected until timeout is hit,
// then everything will be sent to the fetch method and out to the listeners
batch *{{.Name|lcFirst}}Batch
// batchResultSet sets the batch result
batchResultSet func({{.KeyType.String}}, {{.ValType.String}})
// how long to done before sending a batch
wait time.Duration
// this will limit the maximum number of keys to send in one batch, 0 = no limit
maxBatch int
{{ if not .DisableCacheExpiration }}
// the amount of nanoseconds a cache item should remain valid. This will determine if cache expiration will be used, 0 = no expiration
expireAfter int64
{{ end }}
// mutex to prevent races
mu sync.Mutex
// hookExternalCacheGet is a method that provides the ability to lookup a key in an external cache with an external hook.
// If the key is found in the external cache, the value should be returned along with true.
// If the key is not found in the external cache, an empty/nil value should be returned along with false.
hookExternalCacheGet func(key {{.KeyType.String}}) ({{.ValType.String}}, bool)
// hookExternalCacheSet is a method that provides the ability to set a key in an external cache with an external hook.
// This replaces the use of the internal cache.
hookExternalCacheSet func(key {{.KeyType.String}}, value {{.ValType.String}}) error
// hookBeforeFetch is a method that provides the ability to delete/clear a key in an external cache with an external hook.
// This replaces the use of the internal cache.
hookExternalCacheDelete func(key {{.KeyType.String}}) error
// hookExternalCacheClearAll is a method that provides the ability to clear all keys in an external cache with an external hook.
hookExternalCacheClearAll func() error
// hookBeforeFetch is called right before a fetch is performed
hookBeforeFetch func(keys []{{.KeyType.String}}, loaderName string)
// hookAfterFetch is called right after a fetch is performed
hookAfterFetch func(keys []{{.KeyType.String}}, loaderName string)
// hookAfterSet is called after a value is set in the cache
hookAfterSet func(key {{.KeyType.String}}, value {{.ValType.String}})
// hookAfterPrime is called after a value is primed in the cache using Prime or ForcePrime
hookAfterPrime func(key {{.KeyType.String}}, value {{.ValType.String}})
// hookAfterClear is called after a value is cleared from the cache
hookAfterClear func(key {{.KeyType.String}})
// hookAfterClearAll is called after all values are cleared from the cache
hookAfterClearAll func()
// hookAfterExpired is called after a value is cleared in the cache due to expiration
hookAfterExpired func(key {{.KeyType.String}})
// pool of batches
batchPool sync.Pool
}
type {{.Name|lcFirst}}Batch struct {
loader *{{.Name}}
now int64
done chan struct{}
keysMap map[{{.KeyType}}]int
keys []{{.KeyType}}
data []{{.ValType.String}}
errors []error
closing bool
lock sync.Mutex
reqCount int
checkedIn int
}
// Load a {{.ValType.Name}} by key, batching and caching will be applied automatically
func (l *{{.Name}}) Load(key {{.KeyType.String}}) ({{.ValType.String}}, error) {
v, f := l.LoadThunk(key)
if f != nil {
return f()
}
return v, nil
}
// unsafeBatchSet creates a new batch if one does not exist, otherwise it will reuse the existing batch
func (l *{{.Name}}) unsafeBatchSet() {
if l.batch == nil {
b := l.batchPool.Get().(*{{.Name|lcFirst}}Batch)
// create new batch re-using our keysMap and keys fields
l.batch = &{{.Name|lcFirst}}Batch{loader: l, now: 0, done: make(chan struct{}), keysMap: b.keysMap, keys: b.keys[:0], data: nil, errors: nil, reqCount: 0, checkedIn: 0, lock: sync.Mutex{}}
} else if l.batch.now == 0 {
// have a batch but first use, set the start time
l.batch.now = time.Now().UnixNano()
}
}
// createNewBatch creates a new batch
func (l *{{.Name}}) createNewBatch() *{{.Name|lcFirst}}Batch {
return &{{.Name|lcFirst}}Batch{
loader: l,
now: 0,
done: make(chan struct{}),
keysMap: make(map[{{.KeyType.String}}]int, l.maxBatch),
keys: make([]{{.KeyType.String}}, 0, l.maxBatch),
data: nil,
errors: nil,
lock: sync.Mutex{},
reqCount: 0,
checkedIn: 0,
}
}
// LoadThunk returns a function that when called will block waiting for a {{.ValType.Name}}.
// This method should be used if you want one goroutine to make requests to many
// different data loaders without blocking until the thunk is called.
func (l *{{.Name}}) LoadThunk(key {{.KeyType.String}}) ({{.ValType.String}}, func() ({{.ValType.String}}, error)) {
if l.redisConfig != nil {
// using Redis
v, err := l.redisConfig.GetFunc(context.Background(), {{.Name}}CacheKeyPrefix+{{ToRedisKey .KeyType.String }})
if err == nil {
{{ if eq .KeyType.String "string" }}{{.ValType.String|LoadThunkMarshalType}}
{{else}}// found in Redis, attempt to return value
{{.ValType.String|LoadThunkMarshalType}}
// error unmarshalling, just add to batch{{end}}
}
// not found in Redis or error, continue
l.mu.Lock() // unsafeAddToBatch will unlock
} else {
if l.hookExternalCacheGet != nil {
if v, ok := l.hookExternalCacheGet(key); ok {
return v, nil
}
// not found in external cache, continue
l.mu.Lock() // unsafeAddToBatch will unlock
} else {
l.mu.Lock() // unsafeAddToBatch will unlock
{{ if not .DisableCacheExpiration }}
if l.expireAfter <= 0 && len(l.cache) > 0 {
// not using cache expiration
if it, ok := l.cache[key]; ok {
l.mu.Unlock()
return it, nil
}
} else if l.expireAfter > 0 && len(l.cacheExpire) > 0 {
// using cache expiration
l.unsafeBatchSet()
if it, ok := l.cacheExpire[key]; ok {
if it != nil && !it.expired(l.batch.now) {
l.mu.Unlock()
return it.Value, nil
}
// cache item has expired, clear from cache
delete(l.cacheExpire, key)
if l.hookAfterExpired != nil {
l.hookAfterExpired(key)
}
}
}
{{ else }}
if len(l.cache) > 0 {
if it, ok := l.cache[key]; ok {
l.mu.Unlock()
return it, nil
}
}
{{ end }}
}
}
return l.unsafeAddToBatch(key)
}
// unsafeAddToBatch adds the key to the current batch and returns a thunk to be called later. This method is not thread safe. Expects l.mu.lock() to have been called prior to calling this method.
func (l *{{.Name}}) unsafeAddToBatch(key {{.KeyType.String}}) ({{.ValType.String}}, func() ({{.ValType.String}}, error)) {
l.unsafeBatchSet()
batch := l.batch
pos := batch.keyIndex(l, key)
l.mu.Unlock()
return {{.ValType.String|loadThunkMissReturnType}}, func() ({{.ValType.String}}, error) {
<-batch.done
// batch has been closed, pull result
data, err := batch.getResult(pos)
if err == nil {
l.batchResultSet(key, data)
}
return data, err
}
}
// LoadAll fetches many keys at once. It will be broken into appropriate sized
// sub batches depending on how the loader is configured
func (l *{{.Name}}) LoadAll(keys []{{.KeyType}}) ([]{{.ValType.String}}, []error) {
if len(keys) == 0 {
return nil, nil
}
retVals := make([]{{.ValType.String}}, len(keys))
thunks := make(map[int]func() ({{.ValType.String}}, error), len(keys))
errors := make([]error, len(keys))
if l.redisConfig != nil && l.redisConfig.GetManyFunc != nil {
// using Redis and GetManyFunc is set
rKeys := make([]string, len(keys))
for idx, key := range keys {
rKeys[idx] = {{.Name}}CacheKeyPrefix + {{ToRedisKey .KeyType.String }}
}
vS, errs, err := l.redisConfig.GetManyFunc(context.Background(), rKeys)
if err != nil {
// return errors for all keys
for i := range errors {
errors[i] = err
}
return retVals, errors
} else if len(vS) != len(keys) || len(errs) != len(keys) {
// return errors for all keys, invalid lengths returned
for i := range errors {
errors[i] = Err{{.Name}}GetManyLength
}
} else {
for i, err := range errs {
if err != nil {
l.mu.Lock() // unsafeAddToBatch will unlock
if _, thunk := l.unsafeAddToBatch(keys[i]); thunk != nil {
thunks[i] = thunk
}
} else {
{{.ValType.String|LoadAllMarshalType}}
}
}
}
} else {
// not using Redis or GetManyFunc is not set
for i, key := range keys {
if v, thunk := l.LoadThunk(key); thunk != nil {
thunks[i] = thunk
} else {
retVals[i] = v
}
}
}
for i, thunk := range thunks {
retVals[i], errors[i] = thunk()
}
return retVals, errors
}
// LoadAllThunk returns a function that when called will block waiting for a {{.ValType.Name}}s.
// This method should be used if you want one goroutine to make requests to many
// different data loaders without blocking until the thunk is called.
// TODO: Add support for Redis GetManyFunc
func (l *{{.Name}}) LoadAllThunk(keys []{{.KeyType}}) (func() ([]{{.ValType.String}}, []error)) {
thunks := make(map[int]func() ({{.ValType.String}}, error), len(keys))
{{.ValType.Name|lcFirst}}s := make([]{{.ValType.String}}, len(keys))
for i, key := range keys {
if v, thunk := l.LoadThunk(key); thunk != nil {
thunks[i] = thunk
} else {
{{.ValType.Name|lcFirst}}s[i] = v
}
}
return func() ([]{{.ValType.String}}, []error) {
errors := make([]error, len(keys))
for i, thunk := range thunks {
{{.ValType.Name|lcFirst}}s[i], errors[i] = thunk()
}
return {{.ValType.Name|lcFirst}}s, errors
}
}
// redisPrime will set the key value pair in Redis
func (l *{{.Name}}) redisPrime(key {{.KeyType}}, value {{.ValType.String}}) bool {
if err := l.redisConfig.SetFunc(context.Background(), {{.Name}}CacheKeyPrefix+{{ToRedisKey .KeyType.String }}, value, l.redisConfig.SetTTL); err != nil {
return false
} else if l.hookAfterSet != nil {
l.hookAfterSet(key, value)
}
return true
}
// unsafePrime will prime the cache with the given key and value if the key does not exist. This method is not thread safe.
func (l *{{.Name}}) unsafePrime(key {{.KeyType}}, value {{.ValType.String}}, forceReplace bool) bool {
if l.redisConfig != nil {
// using Redis
return l.redisPrime(key, value)
}
if l.hookExternalCacheSet != nil {
{{- if .ValType.IsPtr }}
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := *value
if err := l.hookExternalCacheSet(key, &cpy); err != nil {
return false
}
{{- else if .ValType.IsSlice }}
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := make({{.ValType.String}}, len(value))
copy(cpy, value)
if err := l.hookExternalCacheSet(key, cpy); err != nil {
return false
}
{{- else }}
if err := l.hookExternalCacheSet(key, value); err != nil {
return false
}
{{- end }}
if l.hookAfterSet != nil {
l.hookAfterSet(key, value)
}
return true
}
var found bool
{{ if not .DisableCacheExpiration }}
if l.expireAfter <= 0 {
// not using cache expiration
{{ end }}
if _, found = l.cache[key]; found && forceReplace {
delete(l.cache, key)
}
if !found || forceReplace {
{{- if .ValType.IsPtr }}
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := *value
l.unsafeSet(key, &cpy)
{{- else if .ValType.IsSlice }}
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := make({{.ValType.String}}, len(value))
copy(cpy, value)
l.unsafeSet(key, cpy)
{{- else }}
l.unsafeSet(key, value)
{{- end }}
}
{{ if not .DisableCacheExpiration }}
} else {
// using cache expiration
if _, found = l.cacheExpire[key]; found && forceReplace {
delete(l.cacheExpire, key)
}
if !found || forceReplace {
{{- if .ValType.IsPtr }}
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := *value
l.unsafeSet(key, &cpy)
{{- else if .ValType.IsSlice }}
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
// and end up with the whole cache pointing to the same value.
cpy := make({{.ValType.String}}, len(value))
copy(cpy, value)
l.unsafeSet(key, cpy)
{{- else }}
l.unsafeSet(key, value)
{{- end }}
}
}
{{ end }}
return !found || forceReplace
}
// PrimeMany will prime the cache with the given keys and values. Value index is matched to key index.
func (l *{{.Name}}) PrimeMany(keys []{{.KeyType}}, values []{{.ValType.String}}) []bool {
if len(keys) != len(values) {
// keys and values must be the same length
return make([]bool, len(keys))
}
ret := make([]bool, len(keys))
if l.redisConfig != nil {
// using Redis
for i, key := range keys {
ret[i] = l.redisPrime(key, values[i])
}
} else {
l.mu.Lock()
for i, key := range keys {
ret[i] = l.unsafePrime(key, values[i], false)
}
l.mu.Unlock()
}
return ret
}
// Prime the cache with the provided key and value. If the key already exists, no change is made
// and false is returned.
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
func (l *{{.Name}}) Prime(key {{.KeyType}}, value {{.ValType.String}}) bool {
if l.redisConfig != nil {
// using Redis
b := l.redisPrime(key, value)
if l.hookAfterPrime != nil {
l.hookAfterPrime(key, value)
}
return b
} else {
l.mu.Lock()
found := l.unsafePrime(key, value, false)
l.mu.Unlock()
if l.hookAfterPrime != nil {
l.hookAfterPrime(key, value)
}
return found
}
}
// ForcePrime the cache with the provided key and value. If the key already exists, value is replaced
// (This removes the requirement to clear the key first with loader.clear(key).prime(key, value))
func (l *{{.Name}}) ForcePrime(key {{.KeyType}}, value {{.ValType.String}}) {
l.batchResultSet(key, value)
if l.hookAfterPrime != nil {
l.hookAfterPrime(key, value)
}
}
// Clear the value at key from the cache, if it exists
func (l *{{.Name}}) Clear(key {{.KeyType}}) {
if l.redisConfig != nil {
// using Redis
l.redisConfig.DeleteFunc(context.Background(), {{.Name}}CacheKeyPrefix+{{ToRedisKey .KeyType.String }})
if l.hookAfterClear != nil {
l.hookAfterClear(key)
}
return
}
if l.hookExternalCacheDelete != nil {
l.hookExternalCacheDelete(key)
if l.hookAfterClear != nil {
l.hookAfterClear(key)
}
return
}
{{ if not .DisableCacheExpiration }}
if l.expireAfter <= 0 {
// not using cache expiration
{{ end }}
l.mu.Lock()
delete(l.cache, key)
l.mu.Unlock()
{{ if not .DisableCacheExpiration }}
} else {
// using cache expiration
l.mu.Lock()
delete(l.cacheExpire, key)
l.mu.Unlock()
}
{{ end }}
if l.hookAfterClear != nil {
l.hookAfterClear(key)
}
}
// ClearAll clears all values from the cache
func (l *{{.Name}}) ClearAll() {
if l.redisConfig != nil {
// using Redis
if l.redisConfig.GetKeysFunc != nil {
// get all keys from Redis
keys, _ := l.redisConfig.GetKeysFunc(context.Background(), {{.Name}}CacheKeyPrefix+"*")
// delete all these keys from Redis
if l.redisConfig.DeleteManyFunc != nil {
l.redisConfig.DeleteManyFunc(context.Background(), keys)
} else {
for _, key := range keys {
l.redisConfig.DeleteFunc(context.Background(), key)
}
}
if l.hookAfterClearAll != nil {
l.hookAfterClearAll()
}
}
return
}
if l.hookExternalCacheClearAll != nil {
l.hookExternalCacheClearAll()
if l.hookAfterClearAll != nil {
l.hookAfterClearAll()
}
return
}
{{ if not .DisableCacheExpiration }}
if l.expireAfter <= 0 {
// not using cache expiration
{{ end}}
l.mu.Lock()
l.cache = make(map[{{.KeyType}}]{{.ValType.String}}, l.maxBatch)
l.mu.Unlock()
{{ if not .DisableCacheExpiration }}
} else {
// using cache expiration
l.mu.Lock()
l.cacheExpire = make(map[{{.KeyType}}]*{{.Name}}CacheItem, l.maxBatch)
l.mu.Unlock()
}
{{ end }}
if l.hookAfterClearAll != nil {
l.hookAfterClearAll()
}
}
{{ if not .DisableCacheExpiration }}
// ClearExpired clears all expired values from the cache if cache expiration is being used
func (l *{{.Name}}) ClearExpired() {
if l.redisConfig != nil {
// using Redis. Nothing to do, TTL will handle this
return
}
if l.expireAfter > 0 {
// using cache expiration
tNow := time.Now().UnixNano()
l.mu.Lock()
for cacheKey, cacheItem := range l.cacheExpire {
if cacheItem != nil && tNow > cacheItem.Expires {
// value has expired
delete(l.cacheExpire, cacheKey)
if l.hookAfterExpired != nil {
l.hookAfterExpired(cacheKey)
}
}
}
l.mu.Unlock()
}
}
{{ end }}
// unsafeSet will set the key to value without any locks or checks. This method is not thread safe.
func (l *{{.Name}}) unsafeSet(key {{.KeyType}}, value {{.ValType.String}}) {
if l.redisConfig != nil {
// using Redis
l.redisPrime(key, value)
return
}
if l.hookExternalCacheSet != nil {
l.hookExternalCacheSet(key, value)
if l.hookAfterSet != nil {
l.hookAfterSet(key, value)
}
return
}
{{ if not .DisableCacheExpiration }}
if l.expireAfter <= 0 {
// not using cache expiration
{{ end }}
if l.cache == nil {
l.cache = make(map[{{.KeyType}}]{{.ValType.String}}, l.maxBatch)
}
l.cache[key] = value
{{ if not .DisableCacheExpiration }}
} else {
// using cache expiration
if l.cacheExpire == nil {
l.cacheExpire = make(map[{{.KeyType}}]*{{.Name}}CacheItem, l.maxBatch)
}
l.cacheExpire[key] = &{{.Name}}CacheItem{Expires: time.Now().UnixNano()+l.expireAfter, Value: value}
}
{{ end }}
if l.hookAfterSet != nil {
l.hookAfterSet(key, value)
}
}
// keyIndex will return the location of the key in the batch, if its not found
// it will add the key to the batch
func (b *{{.Name|lcFirst}}Batch) keyIndex(l *{{.Name}}, key {{.KeyType}}) int {
b.reqCount++
if i, ok := b.keysMap[key]; ok {
return i
}
pos := len(b.keysMap)
b.keysMap[key] = pos
b.keys = append(b.keys, key)
if pos == 0 {
go b.startTimer(l)
}
// have we reached out max batch size?
if l.maxBatch != 0 && pos >= l.maxBatch-1 {
if !b.closing {
// not already closing, close the batch and call end
b.closing = true
l.batch = nil
go b.end(l)
}
}
return pos
}
// startTimer will wait the desired wait time before sending the batch unless another batch limit had been reached
func (b *{{.Name|lcFirst}}Batch) startTimer(l *{{.Name}}) {
time.Sleep(l.wait)
l.mu.Lock()
// we must have hit a batch limit and are already finalizing this batch
if b.closing {
l.mu.Unlock()
return
}
l.batch = nil
l.mu.Unlock()
b.end(l)
}
// end calls fetch and closes the done channel to unblock all thunks
func (b *{{.Name|lcFirst}}Batch) end(l *{{.Name}}) {
if l.hookBeforeFetch != nil {
l.hookBeforeFetch(b.keys, "{{.Name}}")
}
b.data, b.errors = l.fetch(b.keys)
if l.hookAfterFetch != nil {
l.hookAfterFetch(b.keys, "{{.Name}}")
}
// close done channel to signal all thunks to unblock
close(b.done)
}
// getResult will return the result for the given position from the batch
func (b *{{.Name|lcFirst}}Batch) getResult(pos int) ({{.ValType.String}}, error) {
var data {{.ValType.String}}
b.lock.Lock()
if pos < len(b.data) {
data = b.data[pos]
}
var err error
// its convenient to be able to return a single error for everything
if len(b.errors) == 1 {
err = b.errors[0]
} else if b.errors != nil {
err = b.errors[pos]
}
// check if all thunks have checked in and if so, return batch to pool
b.checkedIn++
if b.checkedIn >= b.reqCount {
// reset
b.reqCount = 0
b.checkedIn = 0
clear(b.keysMap)
clear(b.keys)
b.lock.Unlock()
// all thunks have checked in, return batch to pool for re-use
b.loader.batchPool.Put(b)
} else {
b.lock.Unlock()
}
// return data and error
return data, err
}
// Marshal{{.Name}}ToString is a helper method to marshal a {{.Name}} to a string
func (l *{{.Name}}) Marshal{{.Name}}ToString(v {{.KeyType.String}}) string {
ret, _ := l.redisConfig.ObjMarshal(v)
return string(ret)
}
`))