Documentation ¶
Overview ¶
Package dataloader is an implimentation of facebook's dataloader in go. See https://github.com/facebook/dataloader for more information
Index ¶
- type BatchFunc
- type Cache
- type InMemoryCache
- type Interface
- type Key
- type Keys
- type Loader
- func (l *Loader) Clear(ctx context.Context, key Key) Interface
- func (l *Loader) ClearAll() Interface
- func (l *Loader) Load(originalContext context.Context, key Key) Thunk
- func (l *Loader) LoadMany(originalContext context.Context, keys Keys) ThunkMany
- func (l *Loader) Prime(ctx context.Context, key Key, value interface{}) Interface
- type NoCache
- type NoopTracer
- func (NoopTracer) TraceBatch(ctx context.Context, keys Keys) (context.Context, TraceBatchFinishFunc)
- func (NoopTracer) TraceLoad(ctx context.Context, key Key) (context.Context, TraceLoadFinishFunc)
- func (NoopTracer) TraceLoadMany(ctx context.Context, keys Keys) (context.Context, TraceLoadManyFinishFunc)
- type OpenTracingTracer
- func (OpenTracingTracer) TraceBatch(ctx context.Context, keys Keys) (context.Context, TraceBatchFinishFunc)
- func (OpenTracingTracer) TraceLoad(ctx context.Context, key Key) (context.Context, TraceLoadFinishFunc)
- func (OpenTracingTracer) TraceLoadMany(ctx context.Context, keys Keys) (context.Context, TraceLoadManyFinishFunc)
- type Option
- type Result
- type ResultMany
- type StringKey
- type Thunk
- type ThunkMany
- type TraceBatchFinishFunc
- type TraceLoadFinishFunc
- type TraceLoadManyFinishFunc
- type Tracer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BatchFunc ¶
BatchFunc is a function, which when given a slice of keys (string), returns an slice of `results`. It's important that the length of the input keys matches the length of the output results.
The keys passed to this function are guaranteed to be unique
type Cache ¶
type Cache interface { Get(context.Context, Key) (Thunk, bool) Set(context.Context, Key, Thunk) Delete(context.Context, Key) bool Clear() }
The Cache interface. If a custom cache is provided, it must implement this interface.
type InMemoryCache ¶
type InMemoryCache struct {
// contains filtered or unexported fields
}
InMemoryCache is an in memory implementation of Cache interface. this simple implementation is well suited for a "per-request" dataloader (i.e. one that only lives for the life of an http request) but it not well suited for long lived cached items.
func (*InMemoryCache) Delete ¶
func (c *InMemoryCache) Delete(_ context.Context, key Key) bool
Delete deletes item at `key` from cache
type Interface ¶
type Interface interface { Load(context.Context, Key) Thunk LoadMany(context.Context, Keys) ThunkMany Clear(context.Context, Key) Interface ClearAll() Interface Prime(ctx context.Context, key Key, value interface{}) 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.
Each `DataLoader` instance should contain a unique memoized cache. Use caution when used in long-lived applications or those which serve many users with different access permissions and consider creating a new instance per web request.
type Key ¶
type Key interface { // String returns a guaranteed unique string that can be used to identify an object String() string // Raw returns the raw, underlaying value of the key Raw() interface{} }
Key is the interface that all keys need to implement
type Keys ¶
type Keys []Key
Keys wraps a slice of Key types to provide some convenience methods.
func NewKeysFromStrings ¶
NewKeysFromStrings converts a `[]strings` to a `Keys` ([]Key)
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader implements the dataloader.Interface.
func NewBatchedLoader ¶
NewBatchedLoader constructs a new Loader with given options.
func (*Loader) Clear ¶
Clear clears the value at `key` from the cache, it it exsits. Returs self for method chaining
func (*Loader) ClearAll ¶
ClearAll clears the entire cache. To be used when some event results in unknown invalidations. Returns self for method chaining.
func (*Loader) Load ¶
Load load/resolves the given key, returning a channel that will contain the value and error
type NoCache ¶
type NoCache struct{}
NoCache implements Cache interface where all methods are noops. This is useful for when you don't want to cache items but still want to use a data loader
type NoopTracer ¶
type NoopTracer struct{}
NoopTracer is the default (noop) tracer
func (NoopTracer) TraceBatch ¶
func (NoopTracer) TraceBatch(ctx context.Context, keys Keys) (context.Context, TraceBatchFinishFunc)
TraceBatch is a noop function
func (NoopTracer) TraceLoad ¶
func (NoopTracer) TraceLoad(ctx context.Context, key Key) (context.Context, TraceLoadFinishFunc)
TraceLoad is a noop function
func (NoopTracer) TraceLoadMany ¶
func (NoopTracer) TraceLoadMany(ctx context.Context, keys Keys) (context.Context, TraceLoadManyFinishFunc)
TraceLoadMany is a noop function
type OpenTracingTracer ¶
type OpenTracingTracer struct{}
OpenTracing Tracer implements a tracer that can be used with the Open Tracing standard.
func (OpenTracingTracer) TraceBatch ¶
func (OpenTracingTracer) TraceBatch(ctx context.Context, keys Keys) (context.Context, TraceBatchFinishFunc)
TraceBatch will trace a call to dataloader.LoadMany with Open Tracing
func (OpenTracingTracer) TraceLoad ¶
func (OpenTracingTracer) TraceLoad(ctx context.Context, key Key) (context.Context, TraceLoadFinishFunc)
TraceLoad will trace a call to dataloader.LoadMany with Open Tracing
func (OpenTracingTracer) TraceLoadMany ¶
func (OpenTracingTracer) TraceLoadMany(ctx context.Context, keys Keys) (context.Context, TraceLoadManyFinishFunc)
TraceLoadMany will trace a call to dataloader.LoadMany with Open Tracing
type Option ¶
type Option func(*Loader)
Option allows for configuration of Loader fields.
func WithBatchCapacity ¶
WithBatchCapacity sets the batch capacity. Default is 0 (unbounded).
func WithCache ¶
WithCache sets the BatchedLoader cache. Defaults to InMemoryCache if a Cache is not set.
func WithClearCacheOnBatch ¶
func WithClearCacheOnBatch() Option
WithClearCacheOnBatch allows batching of items but no long term caching. It accomplishes this by clearing the cache after each batch operation.
func WithInputCapacity ¶
WithInputCapacity sets the input capacity. Default is 1000.
func WithOpenTracingTracer ¶
func WithOpenTracingTracer() Option
WithOpenTracingTracer allows tracing of calls to Load and LoadMany
func WithTracer ¶
WithTracer allows tracing of calls to Load and LoadMany
type Result ¶
type Result struct { Data interface{} Error error }
Result is the data structure that a BatchFunc returns. It contains the resolved data, and any errors that may have occurred while fetching the data.
type ResultMany ¶
type ResultMany struct { Data []interface{} Error []error }
ResultMany is used by the LoadMany method. It contains a list of resolved data and a list of errors. The lengths of the data list and error list will match, and elements at each index correspond to each other.
type StringKey ¶
type StringKey string
StringKey implements the Key interface for a string
type Thunk ¶
type Thunk func() (interface{}, error)
Thunk is a function that will block until the value (*Result) it contains is resolved. After the value it contains is resolved, this function will return the result. This function can be called many times, much like a Promise is other languages. The value will only need to be resolved once so subsequent calls will return immediately.
type ThunkMany ¶
type ThunkMany func() ([]interface{}, []error)
ThunkMany is much like the Thunk func type but it contains a list of results.
type TraceBatchFinishFunc ¶
type TraceBatchFinishFunc func([]*Result)
type TraceLoadFinishFunc ¶
type TraceLoadFinishFunc func(Thunk)
type TraceLoadManyFinishFunc ¶
type TraceLoadManyFinishFunc func(ThunkMany)
type Tracer ¶
type Tracer interface { // TraceLoad will trace the calls to Load TraceLoad(ctx context.Context, key Key) (context.Context, TraceLoadFinishFunc) // TraceLoadMany will trace the calls to LoadMany TraceLoadMany(ctx context.Context, keys Keys) (context.Context, TraceLoadManyFinishFunc) // TraceBatch will trace data loader batches TraceBatch(ctx context.Context, keys Keys) (context.Context, TraceBatchFinishFunc) }
Tracer is an interface that may be used to implement tracing.