Skip to content

Commit 3234b4f

Browse files
authored
Merge pull request #124 from graph-gophers/fix-failing-example-tests
test: fix failing example tests
2 parents b41dc14 + 50815f8 commit 3234b4f

5 files changed

Lines changed: 86 additions & 106 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ This implementation contains a very basic cache that is intended only to be used
5151
> it also has a `NoCache` type that implements the cache interface but all methods are noop. If you do not wish to cache anything.
5252
5353
## Examples
54-
There are a few basic examples in the example folder.
54+
There are a few basic examples in the package root.
5555

5656
## See also
5757
- [TRACE](TRACE.md)

‎example/ttl_cache/go_cache_test.go‎

Lines changed: 0 additions & 84 deletions
This file was deleted.

‎example_go_cache_test.go‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package dataloader_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
dataloader "github.com/graph-gophers/dataloader/v7"
9+
10+
cache "github.com/patrickmn/go-cache"
11+
)
12+
13+
type goCacheAdapter[K comparable, V any] struct {
14+
c *cache.Cache
15+
}
16+
17+
func (c *goCacheAdapter[K, V]) Get(_ context.Context, key K) (dataloader.Thunk[V], bool) {
18+
k := fmt.Sprintf("%v", key)
19+
v, ok := c.c.Get(k)
20+
if ok {
21+
return v.(dataloader.Thunk[V]), ok
22+
}
23+
return nil, ok
24+
}
25+
26+
func (c *goCacheAdapter[K, V]) Set(_ context.Context, key K, value dataloader.Thunk[V]) {
27+
k := fmt.Sprintf("%v", key)
28+
c.c.Set(k, value, 0)
29+
}
30+
31+
func (c *goCacheAdapter[K, V]) Delete(_ context.Context, key K) bool {
32+
k := fmt.Sprintf("%v", key)
33+
if _, found := c.c.Get(k); found {
34+
c.c.Delete(k)
35+
return true
36+
}
37+
return false
38+
}
39+
40+
func (c *goCacheAdapter[K, V]) Clear() {
41+
c.c.Flush()
42+
}
43+
44+
func ExampleNewBatchedLoader_goCache() {
45+
type User struct {
46+
ID int
47+
Email string
48+
FirstName string
49+
LastName string
50+
}
51+
52+
m := map[int]*User{
53+
5: {ID: 5, FirstName: "John", LastName: "Smith", Email: "john@example.com"},
54+
}
55+
56+
batchFunc := func(_ context.Context, keys []int) []*dataloader.Result[*User] {
57+
var results []*dataloader.Result[*User]
58+
for _, k := range keys {
59+
results = append(results, &dataloader.Result[*User]{Data: m[k]})
60+
}
61+
return results
62+
}
63+
64+
c := cache.New(15*time.Minute, 15*time.Minute)
65+
adapter := &goCacheAdapter[int, *User]{c}
66+
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithCache[int, *User](adapter))
67+
68+
result, err := loader.Load(context.Background(), 5)()
69+
if err != nil {
70+
// handle error
71+
}
72+
73+
fmt.Printf("result: %+v", result)
74+
// Output: result: &{ID:5 Email:john@example.com FirstName:John LastName:Smith}
75+
}
Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// package lru_cache_test contains an exmaple of using go-cache as a long term cache solution for dataloader.
2-
package lru_cache_test
1+
package dataloader_test
32

43
import (
54
"context"
@@ -10,40 +9,35 @@ import (
109
lru "github.com/hashicorp/golang-lru"
1110
)
1211

13-
// Cache implements the dataloader.Cache interface
14-
type cache[K comparable, V any] struct {
12+
type lruCacheAdapter[K comparable, V any] struct {
1513
*lru.ARCCache
1614
}
1715

18-
// Get gets an item from the cache
19-
func (c *cache[K, V]) Get(_ context.Context, key K) (dataloader.Thunk[V], bool) {
16+
func (c *lruCacheAdapter[K, V]) Get(_ context.Context, key K) (dataloader.Thunk[V], bool) {
2017
v, ok := c.ARCCache.Get(key)
2118
if ok {
2219
return v.(dataloader.Thunk[V]), ok
2320
}
2421
return nil, ok
2522
}
2623

27-
// Set sets an item in the cache
28-
func (c *cache[K, V]) Set(_ context.Context, key K, value dataloader.Thunk[V]) {
24+
func (c *lruCacheAdapter[K, V]) Set(_ context.Context, key K, value dataloader.Thunk[V]) {
2925
c.ARCCache.Add(key, value)
3026
}
3127

32-
// Delete deletes an item in the cache
33-
func (c *cache[K, V]) Delete(_ context.Context, key K) bool {
28+
func (c *lruCacheAdapter[K, V]) Delete(_ context.Context, key K) bool {
3429
if c.ARCCache.Contains(key) {
3530
c.ARCCache.Remove(key)
3631
return true
3732
}
3833
return false
3934
}
4035

41-
// Clear clears the cache
42-
func (c *cache[K, V]) Clear() {
36+
func (c *lruCacheAdapter[K, V]) Clear() {
4337
c.ARCCache.Purge()
4438
}
4539

46-
func ExampleGolangLRU() {
40+
func ExampleNewBatchedLoader_golangLRU() {
4741
type User struct {
4842
ID int
4943
Email string
@@ -57,19 +51,16 @@ func ExampleGolangLRU() {
5751

5852
batchFunc := func(_ context.Context, keys []int) []*dataloader.Result[*User] {
5953
var results []*dataloader.Result[*User]
60-
// do some pretend work to resolve keys
6154
for _, k := range keys {
6255
results = append(results, &dataloader.Result[*User]{Data: m[k]})
6356
}
6457
return results
6558
}
6659

67-
// go-cache will automatically cleanup expired items on given duration.
6860
c, _ := lru.NewARC(100)
69-
cache := &cache[int, *User]{ARCCache: c}
70-
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithCache[int, *User](cache))
61+
adapter := &lruCacheAdapter[int, *User]{ARCCache: c}
62+
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithCache[int, *User](adapter))
7163

72-
// immediately call the future function from loader
7364
result, err := loader.Load(context.TODO(), 5)()
7465
if err != nil {
7566
// handle error
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package no_cache_test
1+
package dataloader_test
22

33
import (
44
"context"
@@ -21,14 +21,12 @@ func ExampleNoCache() {
2121

2222
batchFunc := func(_ context.Context, keys []int) []*dataloader.Result[*User] {
2323
var results []*dataloader.Result[*User]
24-
// do some pretend work to resolve keys
2524
for _, k := range keys {
2625
results = append(results, &dataloader.Result[*User]{Data: m[k]})
2726
}
2827
return results
2928
}
3029

31-
// go-cache will automatically cleanup expired items on given duration
3230
cache := &dataloader.NoCache[int, *User]{}
3331
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithCache[int, *User](cache))
3432

0 commit comments

Comments
 (0)