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 ¶
- type BatchFunc
- type Config
- type DataLoader
- func (dl *DataLoader[K, V]) Clear(key K)
- func (dl *DataLoader[K, V]) ClearAll()
- func (dl *DataLoader[K, V]) GetStats() LoaderStats
- func (dl *DataLoader[K, V]) Load(ctx context.Context, key K) (V, error)
- func (dl *DataLoader[K, V]) LoadMany(ctx context.Context, keys []K) ([]V, []error)
- func (dl *DataLoader[K, V]) Prime(key K, value V, err error)
- type LoaderStats
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