@@ -20,18 +20,18 @@ import (
2020// different access permissions and consider creating a new instance per
2121// web request.
2222type 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
2626 ClearAll () Interface
27- Prime (ctx context.Context , key interface {} , value interface {}) Interface
27+ Prime (ctx context.Context , key Key , value interface {}) Interface
2828}
2929
3030// BatchFunc is a function, which when given a slice of keys (string), returns an slice of `results`.
3131// It's important that the length of the input keys matches the length of the output results.
3232//
3333// 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
3535
3636// Result is the data structure that a BatchFunc returns.
3737// 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)
100100
101101// type used to on input channel
102102type batchRequest struct {
103- key interface {}
103+ key Key
104104 channel chan * Result
105105}
106106
@@ -191,7 +191,7 @@ func NewBatchedLoader(batchFn BatchFunc, opts ...Option) *Loader {
191191}
192192
193193// 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 {
195195 ctx , finish := l .tracer .TraceLoad (originalContext , key )
196196
197197 c := make (chan * Result , 1 )
@@ -267,7 +267,7 @@ func (l *Loader) Load(originalContext context.Context, key interface{}) Thunk {
267267}
268268
269269// 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 {
271271 ctx , finish := l .tracer .TraceLoadMany (originalContext , keys )
272272
273273 var (
@@ -278,15 +278,17 @@ func (l *Loader) LoadMany(originalContext context.Context, keys []interface{}) T
278278 wg sync.WaitGroup
279279 )
280280
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+
281289 wg .Add (length )
282290 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 )
290292 }
291293
292294 go func () {
@@ -333,7 +335,7 @@ func (l *Loader) LoadMany(originalContext context.Context, keys []interface{}) T
333335}
334336
335337// 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 {
337339 l .cacheLock .Lock ()
338340 l .cache .Delete (ctx , key )
339341 l .cacheLock .Unlock ()
@@ -351,7 +353,7 @@ func (l *Loader) ClearAll() Interface {
351353
352354// Prime adds the provided key and value to the cache. If the key already exists, no change is made.
353355// 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 {
355357 if _ , ok := l .cache .Get (ctx , key ); ! ok {
356358 thunk := func () (interface {}, error ) {
357359 return value , nil
@@ -399,10 +401,12 @@ func (b *batcher) end() {
399401
400402// execute the batch of all items in queue
401403func (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+ )
406410
407411 for item := range b .input {
408412 keys = append (keys , item .key )
0 commit comments