dataloader

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package dataloader provides efficient batching and caching mechanisms for database operations. This implements the DataLoader pattern to solve N+1 query problems commonly found in GraphQL and REST API implementations, with specific optimizations for DynamoDB access patterns.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchFunc

type BatchFunc[K comparable, V any] func(context.Context, []K) ([]V, []error)

BatchFunc defines the signature for batch loading functions

type Config

type Config struct {
	Wait        time.Duration // How long to wait before executing a batch (default: 1ms)
	MaxBatch    int           // Maximum batch size (default: 100)
	Cache       bool          // Enable caching (default: true)
	CacheExpiry time.Duration // Cache expiry time (default: 5 minutes)
}

Config holds configuration for DataLoader

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns default DataLoader configuration

type DataLoader

type DataLoader[K comparable, V any] struct {
	// contains filtered or unexported fields
}

DataLoader provides batching and caching for database operations

func NewDataLoader

func NewDataLoader[K comparable, V any](batchFn BatchFunc[K, V], cfg Config, logger *zap.Logger) *DataLoader[K, V]

NewDataLoader creates a new DataLoader with the given batch function

func (*DataLoader[K, V]) Clear

func (dl *DataLoader[K, V]) Clear(key K)

Clear removes a key from the cache

func (*DataLoader[K, V]) ClearAll

func (dl *DataLoader[K, V]) ClearAll()

ClearAll clears the entire cache

func (*DataLoader[K, V]) GetStats

func (dl *DataLoader[K, V]) GetStats() LoaderStats

GetStats returns loader statistics

func (*DataLoader[K, V]) Load

func (dl *DataLoader[K, V]) Load(ctx context.Context, key K) (V, error)

Load loads a single value by key, batching with other concurrent requests

func (*DataLoader[K, V]) LoadMany

func (dl *DataLoader[K, V]) LoadMany(ctx context.Context, keys []K) ([]V, []error)

LoadMany loads multiple values by keys, batching efficiently

func (*DataLoader[K, V]) Prime

func (dl *DataLoader[K, V]) Prime(key K, value V, err error)

Prime adds a value to the cache without loading

type LoaderStats

type LoaderStats struct {
	Hits      int64 `json:"hits"`
	Misses    int64 `json:"misses"`
	Batches   int64 `json:"batches"`
	Errors    int64 `json:"errors"`
	CacheSize int   `json:"cache_size"`
}

LoaderStats holds statistics about loader performance

Jump to

Keyboard shortcuts

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