-
Notifications
You must be signed in to change notification settings - Fork 76
/
no_cache_test.go
42 lines (34 loc) · 1.02 KB
/
no_cache_test.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
package no_cache_test
import (
"context"
"fmt"
dataloader "github.com/graph-gophers/dataloader/v7"
)
func ExampleNoCache() {
type User struct {
ID int
Email string
FirstName string
LastName string
}
m := map[int]*User{
5: &User{ID: 5, FirstName: "John", LastName: "Smith", Email: "john@example.com"},
}
batchFunc := func(_ context.Context, keys []int) []*dataloader.Result[*User] {
var results []*dataloader.Result[*User]
// do some pretend work to resolve keys
for _, k := range keys {
results = append(results, &dataloader.Result[*User]{Data: m[k]})
}
return results
}
// go-cache will automaticlly cleanup expired items on given diration
cache := &dataloader.NoCache[int, *User]{}
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithCache[int, *User](cache))
result, err := loader.Load(context.Background(), 5)()
if err != nil {
// handle error
}
fmt.Printf("result: %+v", result)
// Output: result: &{ID:5 Email:john@example.com FirstName:John LastName:Smith}
}