Documentation ¶
Index ¶
- type ErrorSlice
- type Loader
- func (l *Loader[KeyT, ValueT]) Clear(key KeyT)
- 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 Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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) } fmt.Println(one) }
Output: 1
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 (*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]) 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 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)