Documentation
¶
Index ¶
- Variables
- type ErrorSlice
- type Loader
- func (l *Loader[KeyT, ValueT]) Clear(key KeyT)
- func (l *Loader[KeyT, ValueT]) ClearAll()
- func (l *Loader[KeyT, ValueT]) Load(ctx context.Context, key KeyT) (ValueT, error)
- func (l *Loader[KeyT, ValueT]) LoadAll(ctx context.Context, keys []KeyT) ([]ValueT, error)
- func (l *Loader[KeyT, ValueT]) LoadAllThunk(ctx context.Context, keys []KeyT) func() ([]ValueT, error)
- func (l *Loader[KeyT, ValueT]) LoadThunk(ctx context.Context, key KeyT) func() (ValueT, error)
- func (l *Loader[KeyT, ValueT]) Prime(key KeyT, value ValueT) bool
- type MappedFetchError
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("dataloadgen: not found")
ErrNotFound is generated for you when using NewMappedLoader and not returning any data for a given key
Functions ¶
This section is empty.
Types ¶
type ErrorSlice ¶ added in v0.0.5
type ErrorSlice []error
ErrorSlice represents a list of errors that contains at least one error
func (ErrorSlice) Error ¶ added in v0.0.5
func (e ErrorSlice) Error() string
Error implements the error interface
type Loader ¶
type Loader[KeyT comparable, ValueT any] struct { // contains filtered or unexported fields }
Loader batches and caches requests
Example ¶
package main import ( "context" "fmt" "strconv" "time" "github.com/vikstrous/dataloadgen" ) func main() { ctx := context.Background() loader := dataloadgen.NewLoader(func(ctx context.Context, keys []string) (ret []int, errs []error) { for _, key := range keys { num, err := strconv.ParseInt(key, 10, 32) ret = append(ret, int(num)) errs = append(errs, err) } return }, dataloadgen.WithBatchCapacity(1), dataloadgen.WithWait(16*time.Millisecond), ) one, err := loader.Load(ctx, "1") if err != nil { panic(err) } mappedLoader := dataloadgen.NewMappedLoader(func(ctx context.Context, keys []string) (ret map[string]int, err error) { ret = make(map[string]int, len(keys)) errs := make(map[string]error, len(keys)) for _, key := range keys { num, err := strconv.ParseInt(key, 10, 32) ret[key] = int(num) errs[key] = err } err = dataloadgen.MappedFetchError[string](errs) return }, dataloadgen.WithBatchCapacity(1), dataloadgen.WithWait(16*time.Millisecond), ) two, err := mappedLoader.Load(ctx, "2") if err != nil { panic(err) } fmt.Println(one, ",", two) }
Output: 1 , 2
func NewLoader ¶
func NewLoader[KeyT comparable, ValueT any](fetch func(ctx context.Context, keys []KeyT) ([]ValueT, []error), options ...Option) *Loader[KeyT, ValueT]
NewLoader creates a new GenericLoader given a fetch, wait, and maxBatch
func NewMappedLoader ¶ added in v0.0.7
func NewMappedLoader[KeyT comparable, ValueT any](mappedFetch func(ctx context.Context, keys []KeyT) (map[KeyT]ValueT, error), options ...Option) *Loader[KeyT, ValueT]
NewMappedLoader creates a new GenericLoader given a mappedFetch, wait and maxBatch
func (*Loader[KeyT, ValueT]) Clear ¶
func (l *Loader[KeyT, ValueT]) Clear(key KeyT)
Clear the value at key from the cache, if it exists
func (*Loader[KeyT, ValueT]) ClearAll ¶ added in v0.0.9
func (l *Loader[KeyT, ValueT]) ClearAll()
ClearAll clears all values from the cache
func (*Loader[KeyT, ValueT]) Load ¶
Load a ValueT by key, batching and caching will be applied automatically
func (*Loader[KeyT, ValueT]) LoadAll ¶
LoadAll fetches many keys at once. It will be broken into appropriate sized sub batches depending on how the loader is configured
func (*Loader[KeyT, ValueT]) LoadAllThunk ¶
func (l *Loader[KeyT, ValueT]) LoadAllThunk(ctx context.Context, keys []KeyT) func() ([]ValueT, error)
LoadAllThunk returns a function that when called will block waiting for a ValueT. This method should be used if you want one goroutine to make requests to many different data loaders without blocking until the thunk is called.
func (*Loader[KeyT, ValueT]) LoadThunk ¶
LoadThunk returns a function that when called will block waiting for a ValueT. This method should be used if you want one goroutine to make requests to many different data loaders without blocking until the thunk is called.
type MappedFetchError ¶ added in v0.0.7
type MappedFetchError[KeyT comparable] map[KeyT]error
func (MappedFetchError[KeyT]) Error ¶ added in v0.0.7
func (e MappedFetchError[KeyT]) Error() string
type Option ¶ added in v0.0.2
type Option func(*loaderConfig)
Option allows for configuration of loader fields.
func WithBatchCapacity ¶ added in v0.0.2
WithBatchCapacity sets the batch capacity. Default is 0 (unbounded)