Documentation
¶
Index ¶
- type Loader
- func (l *Loader[KeyT, ValueT]) Clear(key KeyT)
- func (l *Loader[KeyT, ValueT]) Load(key KeyT) (ValueT, error)
- func (l *Loader[KeyT, ValueT]) LoadAll(keys []KeyT) ([]ValueT, []error)
- func (l *Loader[KeyT, ValueT]) LoadAllThunk(keys []KeyT) func() ([]ValueT, []error)
- func (l *Loader[KeyT, ValueT]) LoadThunk(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 Loader ¶
type Loader[KeyT comparable, ValueT any] struct { // contains filtered or unexported fields }
Loader batches and caches requests
Example ¶
package main import ( "fmt" "strconv" "time" "github.com/vikstrous/dataloadgen" ) func main() { loader := dataloadgen.NewLoader(func(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[string, int](1), dataloadgen.WithWait[string, int](16*time.Millisecond), ) one, err := loader.Load("1") if err != nil { panic(err) } fmt.Println(one) }
Output: 1
func NewLoader ¶
func NewLoader[KeyT comparable, ValueT any](fetch func(keys []KeyT) ([]ValueT, []error), options ...Option[KeyT, ValueT]) *Loader[KeyT, ValueT]
NewLoader creates a new GenreicLoader 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 ¶
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[KeyT comparable, ValueT any] func(*Loader[KeyT, ValueT])
Option allows for configuration of loader fields.
func WithBatchCapacity ¶ added in v0.0.2
func WithBatchCapacity[KeyT comparable, ValueT any](c int) Option[KeyT, ValueT]
WithBatchCapacity sets the batch capacity. Default is 0 (unbounded)