Skip to content

Commit

Permalink
Merge pull request #67 from graph-gophers/use-modules
Browse files Browse the repository at this point in the history
Migrate from dep to modules
  • Loading branch information
tonyghita committed Sep 3, 2020
2 parents 07f793f + 07618e2 commit c87fdce
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 178 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
@@ -1,12 +1,11 @@
language: go

go:
- 1.8
- 1.x
- 1.15
- 1.14

install:
- go get -u github.com/golang/dep/...
- dep ensure
- go mod install

script:
- go test -v -race -coverprofile=coverage.txt -covermode=atomic
Expand Down
21 changes: 0 additions & 21 deletions Godeps/Godeps.json

This file was deleted.

5 changes: 0 additions & 5 deletions Godeps/Readme

This file was deleted.

33 changes: 0 additions & 33 deletions Gopkg.lock

This file was deleted.

34 changes: 0 additions & 34 deletions Gopkg.toml

This file was deleted.

11 changes: 11 additions & 0 deletions MIGRATE.md
Expand Up @@ -86,3 +86,14 @@ type Cache interface {
Clear()
}
```

## Upgrade from v5 to v6

We add major version release because we switched to using Go Modules from dep,
and drop build tags for older versions of Go (1.9).

The preferred import method includes the major version tag.

```go
import "github.com/graph-gophers/dataloader/v6"
```
@@ -1,22 +1,23 @@
package lru_cache_test

// This is an exmaple of using go-cache as a long term cache solution for
// dataloader.
package main

import (
"context"
"fmt"

dataloader "github.com/graph-gophers/dataloader/v6"
lru "github.com/hashicorp/golang-lru"
"github.com/nicksrandall/dataloader"
)

// Cache implements the dataloader.Cache interface
type Cache struct {
type cache struct {
*lru.ARCCache
}

// Get gets an item from the cache
func (c *Cache) Get(_ context.Context, key dataloader.Key) (dataloader.Thunk, bool) {
func (c *cache) Get(_ context.Context, key dataloader.Key) (dataloader.Thunk, bool) {
v, ok := c.ARCCache.Get(key)
if ok {
return v.(dataloader.Thunk), ok
Expand All @@ -25,12 +26,12 @@ func (c *Cache) Get(_ context.Context, key dataloader.Key) (dataloader.Thunk, bo
}

// Set sets an item in the cache
func (c *Cache) Set(_ context.Context, key dataloader.Key, value dataloader.Thunk) {
func (c *cache) Set(_ context.Context, key dataloader.Key, value dataloader.Thunk) {
c.ARCCache.Add(key, value)
}

// Delete deletes an item in the cache
func (c *Cache) Delete(_ context.Context, key dataloader.Key) bool {
func (c *cache) Delete(_ context.Context, key dataloader.Key) bool {
if c.ARCCache.Contains(key) {
c.ARCCache.Remove(key)
return true
Expand All @@ -39,14 +40,14 @@ func (c *Cache) Delete(_ context.Context, key dataloader.Key) bool {
}

// Clear cleasrs the cache
func (c *Cache) Clear() {
func (c *cache) Clear() {
c.ARCCache.Purge()
}

func main() {
// go-cache will automaticlly cleanup expired items on given diration
func ExampleGolangLRU() {
// go-cache will automaticlly cleanup expired items on given duration.
c, _ := lru.NewARC(100)
cache := &Cache{c}
cache := &cache{ARCCache: c}
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithCache(cache))

// immediately call the future function from loader
Expand All @@ -55,14 +56,15 @@ func main() {
// handle error
}

fmt.Printf("identity: %s\n", result)
fmt.Printf("identity: %s", result)
// Output: identity: some key
}

func batchFunc(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
// do some pretend work to resolve keys
for _, key := range keys {
results = append(results, &dataloader.Result{key.String(), nil})
results = append(results, &dataloader.Result{Data: key.String()})
}
return results
}
@@ -1,13 +1,13 @@
package main
package no_cache_test

import (
"context"
"fmt"

"github.com/graph-gophers/dataloader"
dataloader "github.com/graph-gophers/dataloader/v6"
)

func main() {
func ExampleNoCache() {
// go-cache will automaticlly cleanup expired items on given diration
cache := &dataloader.NoCache{}
loader := dataloader.NewBatchedLoader(batchFunc, dataloader.WithCache(cache))
Expand All @@ -17,14 +17,15 @@ func main() {
// handle error
}

fmt.Printf("identity: %s\n", result)
fmt.Printf("identity: %s", result)
// Output: identity: some key
}

func batchFunc(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
// do some pretend work to resolve keys
for _, key := range keys {
results = append(results, &dataloader.Result{key.String(), nil})
results = append(results, &dataloader.Result{Data: key.String()})
}
return results
}
@@ -1,13 +1,13 @@
// This is an exmaple of using go-cache as a long term cache solution for
// dataloader.
package main
package ttl_cache_test

import (
"context"
"fmt"
"time"

"github.com/nicksrandall/dataloader"
dataloader "github.com/graph-gophers/dataloader/v6"
cache "github.com/patrickmn/go-cache"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func (c *Cache) Clear() {
c.c.Flush()
}

func main() {
func ExampleTTLCache() {
// go-cache will automaticlly cleanup expired items on given diration
c := cache.New(15*time.Minute, 15*time.Minute)
cache := &Cache{c}
Expand All @@ -56,14 +56,15 @@ func main() {
// handle error
}

fmt.Printf("identity: %s\n", result)
fmt.Printf("identity: %s", result)
// Output: identity: some key
}

func batchFunc(_ context.Context, keys dataloader.Keys) []*dataloader.Result {
var results []*dataloader.Result
// do some pretend work to resolve keys
for _, key := range keys {
results = append(results, &dataloader.Result{key.String(), nil})
results = append(results, &dataloader.Result{Data: key.String()})
}
return results
}
10 changes: 10 additions & 0 deletions go.mod
@@ -0,0 +1,10 @@
module github.com/graph-gophers/dataloader/v6

go 1.15

require (
github.com/hashicorp/golang-lru v0.5.4
github.com/opentracing/opentracing-go v1.2.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/stretchr/testify v1.6.1 // indirect
)
18 changes: 18 additions & 0 deletions go.sum
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
57 changes: 0 additions & 57 deletions inMemoryCache_go19.go

This file was deleted.

2 changes: 0 additions & 2 deletions inMemoryCache.go → in_memory_cache.go
@@ -1,5 +1,3 @@
// +build !go1.9

package dataloader

import (
Expand Down

0 comments on commit c87fdce

Please sign in to comment.