dataloader

Implementation of Facebook's DataLoader in Golang

Github星跟踪图

DataLoader

GoDoc
Build Status

This is an implementation of Facebook's DataLoader in Golang.

Install

go get -u github.com/graph-gophers/dataloader

Usage

// setup batch function
batchFn := func(ctx context.Context, keys dataloader.Keys) []*dataloader.Result {
  var results []*dataloader.Result
  // do some async work to get data for specified keys
  // append to this list resolved values
  return results
}

// create Loader with an in-memory cache
loader := dataloader.NewBatchedLoader(batchFn)

/**
 * Use loader
 *
 * A thunk is a function returned from a function that is a
 * closure over a value (in this case an interface value and error).
 * When called, it will block until the value is resolved.
 */
thunk := loader.Load(context.TODO(), dataloader.StringKey("key1")) // StringKey is a convenience method that make wraps string to implement `Key` interface
result, err := thunk()
if err != nil {
  // handle data error
}

log.Printf("value: %#v", result)

Don't need/want to use context?

You're welcome to install the v1 version of this library.

Cache

This implementation contains a very basic cache that is intended only to be used for short lived DataLoaders (i.e. DataLoaders that ony exsist for the life of an http request). You may use your own implementation if you want.

it also has a NoCache type that implements the cache interface but all methods are noop. If you do not wish to cache anything.

Examples

There are a few basic examples in the example folder.

主要指标

概览
名称与所有者graph-gophers/dataloader
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2016-11-23 17:40:48
推送于2024-05-27 15:39:56
最后一次提交2023-03-19 02:48:52
发布数10
最新版本名称v7.1.0 (发布于 )
第一版名称v4.0.0 (发布于 2017-12-05 16:49:05)
用户参与
星数1.2k
关注者数13
派生数77
提交数130
已启用问题?
问题数53
打开的问题数11
拉请求数36
打开的拉请求数10
关闭的拉请求数15
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?