example

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type User

type User struct {
	ID   string
	Name string
}

User is some kind of database backed model

type UserLoader

type UserLoader struct {
	// contains filtered or unexported fields
}

UserLoader batches

func NewLoader

func NewLoader() *UserLoader

NewLoader will collect user requests for 2 milliseconds and send them as a single batch to the fetch func normally fetch would be a database call.

func NewUserLoader

func NewUserLoader(config UserLoaderConfig) *UserLoader

NewUserLoader creates a new UserLoader given a fetch, wait, and maxBatch

func (*UserLoader) Load

func (l *UserLoader) Load(ctx context.Context, key string) (*User, error)

Load a User by key, batching and caching will be applied automatically

func (*UserLoader) LoadAll

func (l *UserLoader) LoadAll(ctx context.Context, keys []string) ([]*User, []error)

LoadAll fetches many keys at once. It will be broken into appropriate sized sub batches depending on how the loader is configured

func (*UserLoader) LoadAllThunk added in v0.4.0

func (l *UserLoader) LoadAllThunk(ctx context.Context, keys []string) func() ([]*User, []error)

LoadAllThunk returns a function that when called will block waiting for a Users. 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 (*UserLoader) LoadThunk

func (l *UserLoader) LoadThunk(ctx context.Context, key string) func() (*User, error)

LoadThunk returns a function that when called will block waiting for a User. 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 UserLoaderConfig

type UserLoaderConfig struct {
	// Fetch is a method that provides the data for the loader
	Fetch func(ctx context.Context, keys []string) ([]*User, []error)

	// Wait is how long wait before sending a batch
	Wait time.Duration

	// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit
	MaxBatch int
}

UserLoaderConfig captures the config to create a new UserLoader

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL