diff --git a/.travis.yml b/.travis.yml index c29df16..0a73339 100644 --- a/.travis.yml +++ b/.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 diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json deleted file mode 100644 index 77e8314..0000000 --- a/Godeps/Godeps.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "ImportPath": "github.com/graph-gophers/dataloader", - "GoVersion": "go1.10", - "GodepVersion": "v80", - "Deps": [ - { - "ImportPath": "github.com/opentracing/opentracing-go", - "Comment": "v1.0.2-5-g1361b9c", - "Rev": "1361b9cd60be79c4c3a7fa9841b3c132e40066a7" - }, - { - "ImportPath": "github.com/opentracing/opentracing-go/log", - "Comment": "v1.0.2-5-g1361b9c", - "Rev": "1361b9cd60be79c4c3a7fa9841b3c132e40066a7" - }, - { - "ImportPath": "golang.org/x/net/context", - "Rev": "5ccada7d0a7ba9aeb5d3aca8d3501b4c2a509fec" - } - ] -} diff --git a/Godeps/Readme b/Godeps/Readme deleted file mode 100644 index 4cdaa53..0000000 --- a/Godeps/Readme +++ /dev/null @@ -1,5 +0,0 @@ -This directory tree is generated automatically by godep. - -Please do not edit. - -See https://github.com/tools/godep for more information. diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index 2930c69..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,33 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - branch = "master" - name = "github.com/hashicorp/golang-lru" - packages = [".","simplelru"] - revision = "0a025b7e63adc15a622f29b0b2c4c3848243bbf6" - -[[projects]] - name = "github.com/opentracing/opentracing-go" - packages = [".","log"] - revision = "1949ddbfd147afd4d964a9f00b24eb291e0e7c38" - version = "v1.0.2" - -[[projects]] - name = "github.com/patrickmn/go-cache" - packages = ["."] - revision = "a3647f8e31d79543b2d0f0ae2fe5c379d72cedc0" - version = "v2.1.0" - -[[projects]] - branch = "master" - name = "golang.org/x/net" - packages = ["context"] - revision = "a8b9294777976932365dabb6640cf1468d95c70f" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - inputs-digest = "a0b8606d9f2ed9df7e69cae570c65c7d7b090bb7a08f58d3535b584693d44da9" - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index 167458d..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,34 +0,0 @@ - -# Gopkg.toml example -# -# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" - - -[[constraint]] - branch = "master" - name = "github.com/hashicorp/golang-lru" - -[[constraint]] - name = "github.com/opentracing/opentracing-go" - version = "1.0.2" - -[[constraint]] - name = "github.com/patrickmn/go-cache" - version = "2.1.0" diff --git a/MIGRATE.md b/MIGRATE.md index aad37e8..11dadb2 100644 --- a/MIGRATE.md +++ b/MIGRATE.md @@ -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" +``` diff --git a/example/lru-cache/golang-lru.go b/example/lru_cache/golang_lru_test.go similarity index 66% rename from example/lru-cache/golang-lru.go rename to example/lru_cache/golang_lru_test.go index a77a39d..81a9236 100644 --- a/example/lru-cache/golang-lru.go +++ b/example/lru_cache/golang_lru_test.go @@ -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 @@ -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 @@ -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 @@ -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 } diff --git a/example/no-cache/no-cache.go b/example/no_cache/no_cache_test.go similarity index 69% rename from example/no-cache/no-cache.go rename to example/no_cache/no_cache_test.go index 059bc14..877edb0 100644 --- a/example/no-cache/no-cache.go +++ b/example/no_cache/no_cache_test.go @@ -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)) @@ -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 } diff --git a/example/ttl-cache/go-cache.go b/example/ttl_cache/go_cache_test.go similarity index 86% rename from example/ttl-cache/go-cache.go rename to example/ttl_cache/go_cache_test.go index 1980fb7..a4e505f 100644 --- a/example/ttl-cache/go-cache.go +++ b/example/ttl_cache/go_cache_test.go @@ -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" ) @@ -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} @@ -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 } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..31d2a41 --- /dev/null +++ b/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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3a5a48c --- /dev/null +++ b/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= diff --git a/inMemoryCache_go19.go b/inMemoryCache_go19.go deleted file mode 100644 index 085d36d..0000000 --- a/inMemoryCache_go19.go +++ /dev/null @@ -1,57 +0,0 @@ -// +build go1.9 - -package dataloader - -import ( - "context" - "sync" -) - -// InMemoryCache is an in memory implementation of Cache interface. -// This simple implementation is well suited for -// a "per-request" dataloader (i.e. one that only lives -// for the life of an http request) but it's not well suited -// for long lived cached items. -type InMemoryCache struct { - items *sync.Map -} - -// NewCache constructs a new InMemoryCache -func NewCache() *InMemoryCache { - return &InMemoryCache{ - items: &sync.Map{}, - } -} - -// Set sets the `value` at `key` in the cache -func (c *InMemoryCache) Set(_ context.Context, key Key, value Thunk) { - c.items.Store(key.String(), value) -} - -// Get gets the value at `key` if it exsits, returns value (or nil) and bool -// indicating of value was found -func (c *InMemoryCache) Get(_ context.Context, key Key) (Thunk, bool) { - item, found := c.items.Load(key.String()) - if !found { - return nil, false - } - - return item.(Thunk), true -} - -// Delete deletes item at `key` from cache -func (c *InMemoryCache) Delete(_ context.Context, key Key) bool { - if _, found := c.items.Load(key.String()); found { - c.items.Delete(key.String()) - return true - } - return false -} - -// Clear clears the entire cache -func (c *InMemoryCache) Clear() { - c.items.Range(func(key, _ interface{}) bool { - c.items.Delete(key) - return true - }) -} diff --git a/inMemoryCache.go b/in_memory_cache.go similarity index 98% rename from inMemoryCache.go rename to in_memory_cache.go index 0615a6e..e5252a2 100644 --- a/inMemoryCache.go +++ b/in_memory_cache.go @@ -1,5 +1,3 @@ -// +build !go1.9 - package dataloader import (