modulepackage
6.0.0
Repository: https://github.com/graph-gophers/dataloader.git
Documentation: pkg.go.dev
# README
DataLoader
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.
# Packages
No description provided by the author
# Functions
NewBatchedLoader constructs a new Loader with given options.
NewCache constructs a new InMemoryCache.
NewKeysFromStrings converts a `[]strings` to a `Keys` ([]Key).
WithBatchCapacity sets the batch capacity.
WithCache sets the BatchedLoader cache.
WithClearCacheOnBatch allows batching of items but no long term caching.
WithInputCapacity sets the input capacity.
WithOpenTracingTracer allows tracing of calls to Load and LoadMany.
WithTracer allows tracing of calls to Load and LoadMany.
WithWait sets the amount of time to wait before triggering a batch.
# Structs
InMemoryCache is an in memory implementation of Cache interface.
Loader implements the dataloader.Interface.
NoCache implements Cache interface where all methods are noops.
NoopTracer is the default (noop) tracer.
OpenTracing Tracer implements a tracer that can be used with the Open Tracing standard.
Result is the data structure that a BatchFunc returns.
ResultMany is used by the LoadMany method.
# Interfaces
The Cache interface.
Interface is a `DataLoader` Interface which defines a public API for loading data from a particular data back-end with unique keys such as the `id` column of a SQL table or document name in a MongoDB database, given a batch loading function.
Key is the interface that all keys need to implement.
Tracer is an interface that may be used to implement tracing.
# Type aliases
BatchFunc is a function, which when given a slice of keys (string), returns a slice of `results`.
Keys wraps a slice of Key types to provide some convenience methods.
Option allows for configuration of Loader fields.
StringKey implements the Key interface for a string.
Thunk is a function that will block until the value (*Result) it contains is resolved.
ThunkMany is much like the Thunk func type but it contains a list of results.
No description provided by the author
No description provided by the author
No description provided by the author