@@ -20,18 +20,18 @@ import (
20
20
// different access permissions and consider creating a new instance per
21
21
// web request.
22
22
type Interface interface {
23
- Load (context.Context , interface {} ) Thunk
24
- LoadMany (context.Context , [] interface {} ) ThunkMany
25
- Clear (context.Context , string ) Interface
23
+ Load (context.Context , Key ) Thunk
24
+ LoadMany (context.Context , Keys ) ThunkMany
25
+ Clear (context.Context , Key ) Interface
26
26
ClearAll () Interface
27
- Prime (ctx context.Context , key interface {} , value interface {}) Interface
27
+ Prime (ctx context.Context , key Key , value interface {}) Interface
28
28
}
29
29
30
30
// BatchFunc is a function, which when given a slice of keys (string), returns an slice of `results`.
31
31
// It's important that the length of the input keys matches the length of the output results.
32
32
//
33
33
// The keys passed to this function are guaranteed to be unique
34
- type BatchFunc func (context.Context , [] interface {} ) []* Result
34
+ type BatchFunc func (context.Context , Keys ) []* Result
35
35
36
36
// Result is the data structure that a BatchFunc returns.
37
37
// It contains the resolved data, and any errors that may have occurred while fetching the data.
@@ -100,7 +100,7 @@ type ThunkMany func() ([]interface{}, []error)
100
100
101
101
// type used to on input channel
102
102
type batchRequest struct {
103
- key interface {}
103
+ key Key
104
104
channel chan * Result
105
105
}
106
106
@@ -191,7 +191,7 @@ func NewBatchedLoader(batchFn BatchFunc, opts ...Option) *Loader {
191
191
}
192
192
193
193
// Load load/resolves the given key, returning a channel that will contain the value and error
194
- func (l * Loader ) Load (originalContext context.Context , key interface {} ) Thunk {
194
+ func (l * Loader ) Load (originalContext context.Context , key Key ) Thunk {
195
195
ctx , finish := l .tracer .TraceLoad (originalContext , key )
196
196
197
197
c := make (chan * Result , 1 )
@@ -267,7 +267,7 @@ func (l *Loader) Load(originalContext context.Context, key interface{}) Thunk {
267
267
}
268
268
269
269
// LoadMany loads mulitiple keys, returning a thunk (type: ThunkMany) that will resolve the keys passed in.
270
- func (l * Loader ) LoadMany (originalContext context.Context , keys [] interface {} ) ThunkMany {
270
+ func (l * Loader ) LoadMany (originalContext context.Context , keys Keys ) ThunkMany {
271
271
ctx , finish := l .tracer .TraceLoadMany (originalContext , keys )
272
272
273
273
var (
@@ -278,15 +278,17 @@ func (l *Loader) LoadMany(originalContext context.Context, keys []interface{}) T
278
278
wg sync.WaitGroup
279
279
)
280
280
281
+ resolve := func (ctx context.Context , i int ) {
282
+ defer wg .Done ()
283
+ thunk := l .Load (ctx , keys [i ])
284
+ result , err := thunk ()
285
+ data [i ] = result
286
+ errors [i ] = err
287
+ }
288
+
281
289
wg .Add (length )
282
290
for i := range keys {
283
- go func (ctx context.Context , i int ) {
284
- defer wg .Done ()
285
- thunk := l .Load (ctx , keys [i ])
286
- result , err := thunk ()
287
- data [i ] = result
288
- errors [i ] = err
289
- }(ctx , i )
291
+ go resolve (ctx , i )
290
292
}
291
293
292
294
go func () {
@@ -333,7 +335,7 @@ func (l *Loader) LoadMany(originalContext context.Context, keys []interface{}) T
333
335
}
334
336
335
337
// Clear clears the value at `key` from the cache, it it exsits. Returs self for method chaining
336
- func (l * Loader ) Clear (ctx context.Context , key string ) Interface {
338
+ func (l * Loader ) Clear (ctx context.Context , key Key ) Interface {
337
339
l .cacheLock .Lock ()
338
340
l .cache .Delete (ctx , key )
339
341
l .cacheLock .Unlock ()
@@ -351,7 +353,7 @@ func (l *Loader) ClearAll() Interface {
351
353
352
354
// Prime adds the provided key and value to the cache. If the key already exists, no change is made.
353
355
// Returns self for method chaining
354
- func (l * Loader ) Prime (ctx context.Context , key interface {} , value interface {}) Interface {
356
+ func (l * Loader ) Prime (ctx context.Context , key Key , value interface {}) Interface {
355
357
if _ , ok := l .cache .Get (ctx , key ); ! ok {
356
358
thunk := func () (interface {}, error ) {
357
359
return value , nil
@@ -399,10 +401,12 @@ func (b *batcher) end() {
399
401
400
402
// execute the batch of all items in queue
401
403
func (b * batcher ) batch (originalContext context.Context ) {
402
- var keys []interface {}
403
- var reqs []* batchRequest
404
- var items []* Result
405
- var panicErr interface {}
404
+ var (
405
+ keys = make (Keys , 0 )
406
+ reqs = make ([]* batchRequest , 0 )
407
+ items = make ([]* Result , 0 )
408
+ panicErr interface {}
409
+ )
406
410
407
411
for item := range b .input {
408
412
keys = append (keys , item .key )
0 commit comments