example

package
v0.4.18 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StringLoaderCacheKeyPrefix = "{DataLoaderStringLoader}:"
)
View Source
const (
	UserByIDAndOrgLoaderCacheKeyPrefix = "{DataLoaderUserByIDAndOrgLoader}:"
)
View Source
const (
	UserFloatLoaderCacheKeyPrefix = "{DataLoaderUserFloatLoader}:"
)
View Source
const (
	UserIntLoaderCacheKeyPrefix = "{DataLoaderUserIntLoader}:"
)
View Source
const (
	UserLoaderCacheKeyPrefix = "{DataLoaderUserLoader}:"
)
View Source
const (
	UserValueByIDAndOrgLoaderCacheKeyPrefix = "{DataLoaderUserValueByIDAndOrgLoader}:"
)

Variables

View Source
var (
	ErrStringLoaderGetManyLength = errors.New("redis error, invalid length returned from GetManyFunc")
)
View Source
var (
	ErrUserByIDAndOrgLoaderGetManyLength = errors.New("redis error, invalid length returned from GetManyFunc")
)
View Source
var (
	ErrUserFloatLoaderGetManyLength = errors.New("redis error, invalid length returned from GetManyFunc")
)
View Source
var (
	ErrUserIntLoaderGetManyLength = errors.New("redis error, invalid length returned from GetManyFunc")
)
View Source
var (
	ErrUserLoaderGetManyLength = errors.New("redis error, invalid length returned from GetManyFunc")
)
View Source
var (
	ErrUserValueByIDAndOrgLoaderGetManyLength = errors.New("redis error, invalid length returned from GetManyFunc")
)

Functions

This section is empty.

Types

type StringLoader added in v0.4.5

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

StringLoader batches and caches requests

func NewStringLoader added in v0.4.5

func NewStringLoader(config StringLoaderConfig) *StringLoader

NewStringLoader creates a new StringLoader given a fetch, wait, and maxBatch

func NewStringLoaderExample added in v0.4.5

func NewStringLoaderExample() *StringLoader

NewStringLoaderExample 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 (*StringLoader) Clear added in v0.4.5

func (l *StringLoader) Clear(key string)

Clear the value at key from the cache, if it exists

func (*StringLoader) ClearAll added in v0.4.5

func (l *StringLoader) ClearAll()

ClearAll clears all values from the cache

func (*StringLoader) ClearExpired added in v0.4.5

func (l *StringLoader) ClearExpired()

ClearExpired clears all expired values from the cache if cache expiration is being used

func (*StringLoader) ForcePrime added in v0.4.5

func (l *StringLoader) ForcePrime(key string, value string)

ForcePrime the cache with the provided key and value. If the key already exists, value is replaced (This removes the requirement to clear the key first with loader.clear(key).prime(key, value))

func (*StringLoader) Load added in v0.4.5

func (l *StringLoader) Load(key string) (string, error)

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

func (*StringLoader) LoadAll added in v0.4.5

func (l *StringLoader) LoadAll(keys []string) ([]string, []error)

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

func (*StringLoader) LoadAllThunk added in v0.4.5

func (l *StringLoader) LoadAllThunk(keys []string) func() ([]string, []error)

LoadAllThunk returns a function that when called will block waiting for a strings. 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. TODO: Add support for Redis GetManyFunc

func (*StringLoader) LoadThunk added in v0.4.5

func (l *StringLoader) LoadThunk(key string) (string, func() (string, error))

LoadThunk returns a function that when called will block waiting for a string. 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 (*StringLoader) MarshalStringLoaderToString added in v0.4.5

func (l *StringLoader) MarshalStringLoaderToString(v string) string

MarshalStringLoaderToString is a helper method to marshal a StringLoader to a string

func (*StringLoader) Prime added in v0.4.5

func (l *StringLoader) Prime(key string, value string) bool

Prime the cache with the provided key and value. If the key already exists, no change is made and false is returned. (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)

func (*StringLoader) PrimeMany added in v0.4.5

func (l *StringLoader) PrimeMany(keys []string, values []string) []bool

PrimeMany will prime the cache with the given keys and values. Value index is matched to key index.

type StringLoaderCacheItem added in v0.4.5

type StringLoaderCacheItem struct {
	// Expires contains the time this CacheItem expires
	Expires int64

	// Value contains the cached string
	Value string
}

StringLoaderCacheItem defines a cache item when using dataloader cache expiration where expireAfter > 0

type StringLoaderConfig added in v0.4.5

type StringLoaderConfig struct {
	// Fetch is a method that provides the data for the loader
	Fetch func(keys []string) ([]string, []error)

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

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

	// ExpireAfter determines how long until cached items expire. Set to 0 to disable expiration
	ExpireAfter time.Duration

	// HookExternalCacheGet is a method that provides the ability to lookup a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	// If the key is found in the external cache, the value should be returned along with true.
	// If the key is not found in the external cache, an empty/nil value should be returned along with false.
	// Both HookExternalCacheGet, HookExternalCacheSet, HookExternalCacheDelete, and HookExternalCacheClearAll should be set if using an external cache.
	HookExternalCacheGet func(key string) (string, bool)

	// HookExternalCacheSet is a method that provides the ability to set a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheSet func(key string, value string) error

	// HookBeforeFetch is a method that provides the ability to delete/clear a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheDelete func(key string) error

	// HookExternalCacheClearAll is a method that provides the ability to clear all keys in an external cache with an external hook.
	HookExternalCacheClearAll func() error

	// HookBeforeFetch is called right before a fetch is performed
	HookBeforeFetch func(keys []string, loaderName string)

	// HookAfterFetch is called right after a fetch is performed
	HookAfterFetch func(keys []string, loaderName string)

	// HookAfterSet is called after a value is set in the cache
	HookAfterSet func(key string, value string)

	// HookAfterPrime is called after a value is primed in the cache using Prime or ForcePrime
	HookAfterPrime func(key string, value string)

	// HookAfterClear is called after a value is cleared from the cache
	HookAfterClear func(key string)

	// HookAfterClearAll is called after all values are cleared from the cache
	HookAfterClearAll func()

	// HookAfterExpired is called after a value is cleared in the cache due to expiration
	HookAfterExpired func(key string)

	// RedisConfig is used to configure a StringLoader backed by Redis, disabling the internal cache.
	RedisConfig *StringLoaderRedisConfig
}

StringLoaderConfig captures the config to create a new StringLoader

type StringLoaderRedisConfig added in v0.4.5

type StringLoaderRedisConfig struct {
	// SetTTL is the TTL (Time To Live) for a key to live in Redis on set. If nil, no TTL will be set.
	SetTTL *time.Duration

	// GetFunc should get a value from Redis given a key and return the raw string value.
	GetFunc func(ctx context.Context, key string) (string, error)

	// GetManyFunc should get one or more values from Redis given a set of keys and return the raw string values, errors the size of keys with non nil values for keys not found, and an error if any other error occurred running the command
	// If not set then GetFunc will be used instead, but will be called one at a time for each key
	GetManyFunc func(ctx context.Context, keys []string) ([]string, []error, error)

	// SetFunc should set a value in Redis given a key and value with an optional ttl (Time To Live)
	SetFunc func(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

	// DeleteFunc should delete a value in Redis given a key
	DeleteFunc func(ctx context.Context, key string) error

	// DeleteManyFunc should delete one or more values in Redis given a set of keys
	DeleteManyFunc func(ctx context.Context, key []string) error

	// GetKeysFunc should return all keys in Redis matching the given pattern. If not set then ClearAll() for this dataloader will not be supported.
	GetKeysFunc func(ctx context.Context, pattern string) ([]string, error)

	// ObjMarshal provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjMarshal func(any) ([]byte, error)

	// ObjUnmarshaler provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjUnmarshal func([]byte, any) error

	// KeyToStringFunc provides you the ability to specify your own function to convert a key to a string, which will be used instead of serialization.
	// This is only used for non standard types that need to be serialized. If not set, the ObjMarshal function (user defined or default) will be used to serialize a key into a string value
	// Example: If you have a struct with a String() function that returns a string representation of the struct, you can set this function to that function.
	//
	// type MyStruct struct {
	//     ID string
	//     OrgID string
	// }
	// ...
	// StringLoaderRedisConfig{
	//		KeyToStringFunc = func(key string) string { return m.ID + ":" + m.OrgID }
	// }
	// ...
	// Or if your key type has a String() function that returns a string representation of the key, you can set this function like this:
	// StringLoaderRedisConfig{
	//		KeyToStringFunc = func(key string) string { return key.String() }
	// }
	KeyToStringFunc func(key string) string
}

StringLoaderRedisConfig is used to configure a StringLoader backed by Redis. GetFunc, SetFunc, and DeleteFunc are required if using Redis. If any function is not provided, Redis will be disabled and internal caching will be used.

type User

type User struct {
	ID    string
	OrgID string
	Name  string
}

User is some kind of database backed model

type UserByIDAndOrg added in v0.4.0

type UserByIDAndOrg struct {
	ID    string `json:"id"`
	OrgID string `json:"oid"`
}

type UserByIDAndOrgLoader added in v0.4.0

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

UserByIDAndOrgLoader batches and caches requests

func NewUserByIDAndOrgLoader added in v0.4.0

func NewUserByIDAndOrgLoader(config UserByIDAndOrgLoaderConfig) *UserByIDAndOrgLoader

NewUserByIDAndOrgLoader creates a new UserByIDAndOrgLoader given a fetch, wait, and maxBatch

func (*UserByIDAndOrgLoader) Clear added in v0.4.0

func (l *UserByIDAndOrgLoader) Clear(key UserByIDAndOrg)

Clear the value at key from the cache, if it exists

func (*UserByIDAndOrgLoader) ClearAll added in v0.4.0

func (l *UserByIDAndOrgLoader) ClearAll()

ClearAll clears all values from the cache

func (*UserByIDAndOrgLoader) ForcePrime added in v0.4.0

func (l *UserByIDAndOrgLoader) ForcePrime(key UserByIDAndOrg, value *User)

ForcePrime the cache with the provided key and value. If the key already exists, value is replaced (This removes the requirement to clear the key first with loader.clear(key).prime(key, value))

func (*UserByIDAndOrgLoader) Load added in v0.4.0

func (l *UserByIDAndOrgLoader) Load(key UserByIDAndOrg) (*User, error)

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

func (*UserByIDAndOrgLoader) LoadAll added in v0.4.0

func (l *UserByIDAndOrgLoader) LoadAll(keys []UserByIDAndOrg) ([]*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 (*UserByIDAndOrgLoader) LoadAllThunk added in v0.4.0

func (l *UserByIDAndOrgLoader) LoadAllThunk(keys []UserByIDAndOrg) 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. TODO: Add support for Redis GetManyFunc

func (*UserByIDAndOrgLoader) LoadThunk added in v0.4.0

func (l *UserByIDAndOrgLoader) LoadThunk(key UserByIDAndOrg) (*User, 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.

func (*UserByIDAndOrgLoader) MarshalUserByIDAndOrgLoaderToString added in v0.4.4

func (l *UserByIDAndOrgLoader) MarshalUserByIDAndOrgLoaderToString(v UserByIDAndOrg) string

MarshalUserByIDAndOrgLoaderToString is a helper method to marshal a UserByIDAndOrgLoader to a string

func (*UserByIDAndOrgLoader) Prime added in v0.4.0

func (l *UserByIDAndOrgLoader) Prime(key UserByIDAndOrg, value *User) bool

Prime the cache with the provided key and value. If the key already exists, no change is made and false is returned. (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)

func (*UserByIDAndOrgLoader) PrimeMany added in v0.4.0

func (l *UserByIDAndOrgLoader) PrimeMany(keys []UserByIDAndOrg, values []*User) []bool

PrimeMany will prime the cache with the given keys and values. Value index is matched to key index.

type UserByIDAndOrgLoaderConfig added in v0.4.0

type UserByIDAndOrgLoaderConfig struct {
	// Fetch is a method that provides the data for the loader
	Fetch func(keys []UserByIDAndOrg) ([]*User, []error)

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

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

	// HookExternalCacheGet is a method that provides the ability to lookup a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	// If the key is found in the external cache, the value should be returned along with true.
	// If the key is not found in the external cache, an empty/nil value should be returned along with false.
	// Both HookExternalCacheGet, HookExternalCacheSet, HookExternalCacheDelete, and HookExternalCacheClearAll should be set if using an external cache.
	HookExternalCacheGet func(key UserByIDAndOrg) (*User, bool)

	// HookExternalCacheSet is a method that provides the ability to set a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheSet func(key UserByIDAndOrg, value *User) error

	// HookBeforeFetch is a method that provides the ability to delete/clear a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheDelete func(key UserByIDAndOrg) error

	// HookExternalCacheClearAll is a method that provides the ability to clear all keys in an external cache with an external hook.
	HookExternalCacheClearAll func() error

	// HookBeforeFetch is called right before a fetch is performed
	HookBeforeFetch func(keys []UserByIDAndOrg, loaderName string)

	// HookAfterFetch is called right after a fetch is performed
	HookAfterFetch func(keys []UserByIDAndOrg, loaderName string)

	// HookAfterSet is called after a value is set in the cache
	HookAfterSet func(key UserByIDAndOrg, value *User)

	// HookAfterPrime is called after a value is primed in the cache using Prime or ForcePrime
	HookAfterPrime func(key UserByIDAndOrg, value *User)

	// HookAfterClear is called after a value is cleared from the cache
	HookAfterClear func(key UserByIDAndOrg)

	// HookAfterClearAll is called after all values are cleared from the cache
	HookAfterClearAll func()

	// HookAfterExpired is called after a value is cleared in the cache due to expiration
	HookAfterExpired func(key UserByIDAndOrg)

	// RedisConfig is used to configure a UserByIDAndOrgLoader backed by Redis, disabling the internal cache.
	RedisConfig *UserByIDAndOrgLoaderRedisConfig
}

UserByIDAndOrgLoaderConfig captures the config to create a new UserByIDAndOrgLoader

type UserByIDAndOrgLoaderRedisConfig added in v0.4.4

type UserByIDAndOrgLoaderRedisConfig struct {
	// SetTTL is the TTL (Time To Live) for a key to live in Redis on set. If nil, no TTL will be set.
	SetTTL *time.Duration

	// GetFunc should get a value from Redis given a key and return the raw string value.
	GetFunc func(ctx context.Context, key string) (string, error)

	// GetManyFunc should get one or more values from Redis given a set of keys and return the raw string values, errors the size of keys with non nil values for keys not found, and an error if any other error occurred running the command
	// If not set then GetFunc will be used instead, but will be called one at a time for each key
	GetManyFunc func(ctx context.Context, keys []string) ([]string, []error, error)

	// SetFunc should set a value in Redis given a key and value with an optional ttl (Time To Live)
	SetFunc func(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

	// DeleteFunc should delete a value in Redis given a key
	DeleteFunc func(ctx context.Context, key string) error

	// DeleteManyFunc should delete one or more values in Redis given a set of keys
	DeleteManyFunc func(ctx context.Context, key []string) error

	// GetKeysFunc should return all keys in Redis matching the given pattern. If not set then ClearAll() for this dataloader will not be supported.
	GetKeysFunc func(ctx context.Context, pattern string) ([]string, error)

	// ObjMarshal provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjMarshal func(any) ([]byte, error)

	// ObjUnmarshaler provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjUnmarshal func([]byte, any) error

	// KeyToStringFunc provides you the ability to specify your own function to convert a key to a string, which will be used instead of serialization.
	// This is only used for non standard types that need to be serialized. If not set, the ObjMarshal function (user defined or default) will be used to serialize a key into a string value
	// Example: If you have a struct with a String() function that returns a string representation of the struct, you can set this function to that function.
	//
	// type MyStruct struct {
	//     ID string
	//     OrgID string
	// }
	// ...
	// UserByIDAndOrgLoaderRedisConfig{
	//		KeyToStringFunc = func(key UserByIDAndOrg) string { return m.ID + ":" + m.OrgID }
	// }
	// ...
	// Or if your key type has a String() function that returns a string representation of the key, you can set this function like this:
	// UserByIDAndOrgLoaderRedisConfig{
	//		KeyToStringFunc = func(key UserByIDAndOrg) string { return key.String() }
	// }
	KeyToStringFunc func(key UserByIDAndOrg) string
}

UserByIDAndOrgLoaderRedisConfig is used to configure a UserByIDAndOrgLoader backed by Redis. GetFunc, SetFunc, and DeleteFunc are required if using Redis. If any function is not provided, Redis will be disabled and internal caching will be used.

type UserFloatLoader added in v0.4.4

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

UserFloatLoader batches and caches requests

func NewUserFloatLoader added in v0.4.4

func NewUserFloatLoader(config UserFloatLoaderConfig) *UserFloatLoader

NewUserFloatLoader creates a new UserFloatLoader given a fetch, wait, and maxBatch

func (*UserFloatLoader) Clear added in v0.4.4

func (l *UserFloatLoader) Clear(key float64)

Clear the value at key from the cache, if it exists

func (*UserFloatLoader) ClearAll added in v0.4.4

func (l *UserFloatLoader) ClearAll()

ClearAll clears all values from the cache

func (*UserFloatLoader) ClearExpired added in v0.4.4

func (l *UserFloatLoader) ClearExpired()

ClearExpired clears all expired values from the cache if cache expiration is being used

func (*UserFloatLoader) ForcePrime added in v0.4.4

func (l *UserFloatLoader) ForcePrime(key float64, value *User)

ForcePrime the cache with the provided key and value. If the key already exists, value is replaced (This removes the requirement to clear the key first with loader.clear(key).prime(key, value))

func (*UserFloatLoader) Load added in v0.4.4

func (l *UserFloatLoader) Load(key float64) (*User, error)

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

func (*UserFloatLoader) LoadAll added in v0.4.4

func (l *UserFloatLoader) LoadAll(keys []float64) ([]*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 (*UserFloatLoader) LoadAllThunk added in v0.4.4

func (l *UserFloatLoader) LoadAllThunk(keys []float64) 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. TODO: Add support for Redis GetManyFunc

func (*UserFloatLoader) LoadThunk added in v0.4.4

func (l *UserFloatLoader) LoadThunk(key float64) (*User, 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.

func (*UserFloatLoader) MarshalUserFloatLoaderToString added in v0.4.4

func (l *UserFloatLoader) MarshalUserFloatLoaderToString(v float64) string

MarshalUserFloatLoaderToString is a helper method to marshal a UserFloatLoader to a string

func (*UserFloatLoader) Prime added in v0.4.4

func (l *UserFloatLoader) Prime(key float64, value *User) bool

Prime the cache with the provided key and value. If the key already exists, no change is made and false is returned. (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)

func (*UserFloatLoader) PrimeMany added in v0.4.4

func (l *UserFloatLoader) PrimeMany(keys []float64, values []*User) []bool

PrimeMany will prime the cache with the given keys and values. Value index is matched to key index.

type UserFloatLoaderCacheItem added in v0.4.4

type UserFloatLoaderCacheItem struct {
	// Expires contains the time this CacheItem expires
	Expires int64

	// Value contains the cached *User
	Value *User
}

UserFloatLoaderCacheItem defines a cache item when using dataloader cache expiration where expireAfter > 0

type UserFloatLoaderConfig added in v0.4.4

type UserFloatLoaderConfig struct {
	// Fetch is a method that provides the data for the loader
	Fetch func(keys []float64) ([]*User, []error)

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

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

	// ExpireAfter determines how long until cached items expire. Set to 0 to disable expiration
	ExpireAfter time.Duration

	// HookExternalCacheGet is a method that provides the ability to lookup a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	// If the key is found in the external cache, the value should be returned along with true.
	// If the key is not found in the external cache, an empty/nil value should be returned along with false.
	// Both HookExternalCacheGet, HookExternalCacheSet, HookExternalCacheDelete, and HookExternalCacheClearAll should be set if using an external cache.
	HookExternalCacheGet func(key float64) (*User, bool)

	// HookExternalCacheSet is a method that provides the ability to set a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheSet func(key float64, value *User) error

	// HookBeforeFetch is a method that provides the ability to delete/clear a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheDelete func(key float64) error

	// HookExternalCacheClearAll is a method that provides the ability to clear all keys in an external cache with an external hook.
	HookExternalCacheClearAll func() error

	// HookBeforeFetch is called right before a fetch is performed
	HookBeforeFetch func(keys []float64, loaderName string)

	// HookAfterFetch is called right after a fetch is performed
	HookAfterFetch func(keys []float64, loaderName string)

	// HookAfterSet is called after a value is set in the cache
	HookAfterSet func(key float64, value *User)

	// HookAfterPrime is called after a value is primed in the cache using Prime or ForcePrime
	HookAfterPrime func(key float64, value *User)

	// HookAfterClear is called after a value is cleared from the cache
	HookAfterClear func(key float64)

	// HookAfterClearAll is called after all values are cleared from the cache
	HookAfterClearAll func()

	// HookAfterExpired is called after a value is cleared in the cache due to expiration
	HookAfterExpired func(key float64)

	// RedisConfig is used to configure a UserFloatLoader backed by Redis, disabling the internal cache.
	RedisConfig *UserFloatLoaderRedisConfig
}

UserFloatLoaderConfig captures the config to create a new UserFloatLoader

type UserFloatLoaderRedisConfig added in v0.4.4

type UserFloatLoaderRedisConfig struct {
	// SetTTL is the TTL (Time To Live) for a key to live in Redis on set. If nil, no TTL will be set.
	SetTTL *time.Duration

	// GetFunc should get a value from Redis given a key and return the raw string value.
	GetFunc func(ctx context.Context, key string) (string, error)

	// GetManyFunc should get one or more values from Redis given a set of keys and return the raw string values, errors the size of keys with non nil values for keys not found, and an error if any other error occurred running the command
	// If not set then GetFunc will be used instead, but will be called one at a time for each key
	GetManyFunc func(ctx context.Context, keys []string) ([]string, []error, error)

	// SetFunc should set a value in Redis given a key and value with an optional ttl (Time To Live)
	SetFunc func(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

	// DeleteFunc should delete a value in Redis given a key
	DeleteFunc func(ctx context.Context, key string) error

	// DeleteManyFunc should delete one or more values in Redis given a set of keys
	DeleteManyFunc func(ctx context.Context, key []string) error

	// GetKeysFunc should return all keys in Redis matching the given pattern. If not set then ClearAll() for this dataloader will not be supported.
	GetKeysFunc func(ctx context.Context, pattern string) ([]string, error)

	// ObjMarshal provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjMarshal func(any) ([]byte, error)

	// ObjUnmarshaler provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjUnmarshal func([]byte, any) error

	// KeyToStringFunc provides you the ability to specify your own function to convert a key to a string, which will be used instead of serialization.
	// This is only used for non standard types that need to be serialized. If not set, the ObjMarshal function (user defined or default) will be used to serialize a key into a string value
	// Example: If you have a struct with a String() function that returns a string representation of the struct, you can set this function to that function.
	//
	// type MyStruct struct {
	//     ID string
	//     OrgID string
	// }
	// ...
	// UserFloatLoaderRedisConfig{
	//		KeyToStringFunc = func(key float64) string { return m.ID + ":" + m.OrgID }
	// }
	// ...
	// Or if your key type has a String() function that returns a string representation of the key, you can set this function like this:
	// UserFloatLoaderRedisConfig{
	//		KeyToStringFunc = func(key float64) string { return key.String() }
	// }
	KeyToStringFunc func(key float64) string
}

UserFloatLoaderRedisConfig is used to configure a UserFloatLoader backed by Redis. GetFunc, SetFunc, and DeleteFunc are required if using Redis. If any function is not provided, Redis will be disabled and internal caching will be used.

type UserIntLoader added in v0.4.4

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

UserIntLoader batches and caches requests

func NewUserIntLoader added in v0.4.4

func NewUserIntLoader(config UserIntLoaderConfig) *UserIntLoader

NewUserIntLoader creates a new UserIntLoader given a fetch, wait, and maxBatch

func (*UserIntLoader) Clear added in v0.4.4

func (l *UserIntLoader) Clear(key int)

Clear the value at key from the cache, if it exists

func (*UserIntLoader) ClearAll added in v0.4.4

func (l *UserIntLoader) ClearAll()

ClearAll clears all values from the cache

func (*UserIntLoader) ClearExpired added in v0.4.4

func (l *UserIntLoader) ClearExpired()

ClearExpired clears all expired values from the cache if cache expiration is being used

func (*UserIntLoader) ForcePrime added in v0.4.4

func (l *UserIntLoader) ForcePrime(key int, value *User)

ForcePrime the cache with the provided key and value. If the key already exists, value is replaced (This removes the requirement to clear the key first with loader.clear(key).prime(key, value))

func (*UserIntLoader) Load added in v0.4.4

func (l *UserIntLoader) Load(key int) (*User, error)

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

func (*UserIntLoader) LoadAll added in v0.4.4

func (l *UserIntLoader) LoadAll(keys []int) ([]*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 (*UserIntLoader) LoadAllThunk added in v0.4.4

func (l *UserIntLoader) LoadAllThunk(keys []int) 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. TODO: Add support for Redis GetManyFunc

func (*UserIntLoader) LoadThunk added in v0.4.4

func (l *UserIntLoader) LoadThunk(key int) (*User, 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.

func (*UserIntLoader) MarshalUserIntLoaderToString added in v0.4.4

func (l *UserIntLoader) MarshalUserIntLoaderToString(v int) string

MarshalUserIntLoaderToString is a helper method to marshal a UserIntLoader to a string

func (*UserIntLoader) Prime added in v0.4.4

func (l *UserIntLoader) Prime(key int, value *User) bool

Prime the cache with the provided key and value. If the key already exists, no change is made and false is returned. (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)

func (*UserIntLoader) PrimeMany added in v0.4.4

func (l *UserIntLoader) PrimeMany(keys []int, values []*User) []bool

PrimeMany will prime the cache with the given keys and values. Value index is matched to key index.

type UserIntLoaderCacheItem added in v0.4.4

type UserIntLoaderCacheItem struct {
	// Expires contains the time this CacheItem expires
	Expires int64

	// Value contains the cached *User
	Value *User
}

UserIntLoaderCacheItem defines a cache item when using dataloader cache expiration where expireAfter > 0

type UserIntLoaderConfig added in v0.4.4

type UserIntLoaderConfig struct {
	// Fetch is a method that provides the data for the loader
	Fetch func(keys []int) ([]*User, []error)

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

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

	// ExpireAfter determines how long until cached items expire. Set to 0 to disable expiration
	ExpireAfter time.Duration

	// HookExternalCacheGet is a method that provides the ability to lookup a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	// If the key is found in the external cache, the value should be returned along with true.
	// If the key is not found in the external cache, an empty/nil value should be returned along with false.
	// Both HookExternalCacheGet, HookExternalCacheSet, HookExternalCacheDelete, and HookExternalCacheClearAll should be set if using an external cache.
	HookExternalCacheGet func(key int) (*User, bool)

	// HookExternalCacheSet is a method that provides the ability to set a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheSet func(key int, value *User) error

	// HookBeforeFetch is a method that provides the ability to delete/clear a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheDelete func(key int) error

	// HookExternalCacheClearAll is a method that provides the ability to clear all keys in an external cache with an external hook.
	HookExternalCacheClearAll func() error

	// HookBeforeFetch is called right before a fetch is performed
	HookBeforeFetch func(keys []int, loaderName string)

	// HookAfterFetch is called right after a fetch is performed
	HookAfterFetch func(keys []int, loaderName string)

	// HookAfterSet is called after a value is set in the cache
	HookAfterSet func(key int, value *User)

	// HookAfterPrime is called after a value is primed in the cache using Prime or ForcePrime
	HookAfterPrime func(key int, value *User)

	// HookAfterClear is called after a value is cleared from the cache
	HookAfterClear func(key int)

	// HookAfterClearAll is called after all values are cleared from the cache
	HookAfterClearAll func()

	// HookAfterExpired is called after a value is cleared in the cache due to expiration
	HookAfterExpired func(key int)

	// RedisConfig is used to configure a UserIntLoader backed by Redis, disabling the internal cache.
	RedisConfig *UserIntLoaderRedisConfig
}

UserIntLoaderConfig captures the config to create a new UserIntLoader

type UserIntLoaderRedisConfig added in v0.4.4

type UserIntLoaderRedisConfig struct {
	// SetTTL is the TTL (Time To Live) for a key to live in Redis on set. If nil, no TTL will be set.
	SetTTL *time.Duration

	// GetFunc should get a value from Redis given a key and return the raw string value.
	GetFunc func(ctx context.Context, key string) (string, error)

	// GetManyFunc should get one or more values from Redis given a set of keys and return the raw string values, errors the size of keys with non nil values for keys not found, and an error if any other error occurred running the command
	// If not set then GetFunc will be used instead, but will be called one at a time for each key
	GetManyFunc func(ctx context.Context, keys []string) ([]string, []error, error)

	// SetFunc should set a value in Redis given a key and value with an optional ttl (Time To Live)
	SetFunc func(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

	// DeleteFunc should delete a value in Redis given a key
	DeleteFunc func(ctx context.Context, key string) error

	// DeleteManyFunc should delete one or more values in Redis given a set of keys
	DeleteManyFunc func(ctx context.Context, key []string) error

	// GetKeysFunc should return all keys in Redis matching the given pattern. If not set then ClearAll() for this dataloader will not be supported.
	GetKeysFunc func(ctx context.Context, pattern string) ([]string, error)

	// ObjMarshal provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjMarshal func(any) ([]byte, error)

	// ObjUnmarshaler provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjUnmarshal func([]byte, any) error

	// KeyToStringFunc provides you the ability to specify your own function to convert a key to a string, which will be used instead of serialization.
	// This is only used for non standard types that need to be serialized. If not set, the ObjMarshal function (user defined or default) will be used to serialize a key into a string value
	// Example: If you have a struct with a String() function that returns a string representation of the struct, you can set this function to that function.
	//
	// type MyStruct struct {
	//     ID string
	//     OrgID string
	// }
	// ...
	// UserIntLoaderRedisConfig{
	//		KeyToStringFunc = func(key int) string { return m.ID + ":" + m.OrgID }
	// }
	// ...
	// Or if your key type has a String() function that returns a string representation of the key, you can set this function like this:
	// UserIntLoaderRedisConfig{
	//		KeyToStringFunc = func(key int) string { return key.String() }
	// }
	KeyToStringFunc func(key int) string
}

UserIntLoaderRedisConfig is used to configure a UserIntLoader backed by Redis. GetFunc, SetFunc, and DeleteFunc are required if using Redis. If any function is not provided, Redis will be disabled and internal caching will be used.

type UserLoader

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

UserLoader batches and caches requests

func NewUserLoader

func NewUserLoader(config UserLoaderConfig) *UserLoader

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

func NewUserLoaderExample added in v0.4.5

func NewUserLoaderExample() *UserLoader

NewUserLoaderExample 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 (*UserLoader) Clear

func (l *UserLoader) Clear(key string)

Clear the value at key from the cache, if it exists

func (*UserLoader) ClearAll added in v0.3.9

func (l *UserLoader) ClearAll()

ClearAll clears all values from the cache

func (*UserLoader) ClearExpired added in v0.3.9

func (l *UserLoader) ClearExpired()

ClearExpired clears all expired values from the cache if cache expiration is being used

func (*UserLoader) ForcePrime added in v0.3.10

func (l *UserLoader) ForcePrime(key string, value *User)

ForcePrime the cache with the provided key and value. If the key already exists, value is replaced (This removes the requirement to clear the key first with loader.clear(key).prime(key, value))

func (*UserLoader) Load

func (l *UserLoader) Load(key string) (*User, error)

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

func (*UserLoader) LoadAll

func (l *UserLoader) LoadAll(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.3.1

func (l *UserLoader) LoadAllThunk(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. TODO: Add support for Redis GetManyFunc

func (*UserLoader) LoadThunk

func (l *UserLoader) LoadThunk(key string) (*User, 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.

func (*UserLoader) MarshalUserLoaderToString added in v0.4.4

func (l *UserLoader) MarshalUserLoaderToString(v string) string

MarshalUserLoaderToString is a helper method to marshal a UserLoader to a string

func (*UserLoader) Prime

func (l *UserLoader) Prime(key string, value *User) bool

Prime the cache with the provided key and value. If the key already exists, no change is made and false is returned. (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)

func (*UserLoader) PrimeMany added in v0.3.9

func (l *UserLoader) PrimeMany(keys []string, values []*User) []bool

PrimeMany will prime the cache with the given keys and values. Value index is matched to key index.

type UserLoaderCacheItem added in v0.3.9

type UserLoaderCacheItem struct {
	// Expires contains the time this CacheItem expires
	Expires int64

	// Value contains the cached *User
	Value *User
}

UserLoaderCacheItem defines a cache item when using dataloader cache expiration where expireAfter > 0

type UserLoaderConfig

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

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

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

	// ExpireAfter determines how long until cached items expire. Set to 0 to disable expiration
	ExpireAfter time.Duration

	// HookExternalCacheGet is a method that provides the ability to lookup a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	// If the key is found in the external cache, the value should be returned along with true.
	// If the key is not found in the external cache, an empty/nil value should be returned along with false.
	// Both HookExternalCacheGet, HookExternalCacheSet, HookExternalCacheDelete, and HookExternalCacheClearAll should be set if using an external cache.
	HookExternalCacheGet func(key string) (*User, bool)

	// HookExternalCacheSet is a method that provides the ability to set a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheSet func(key string, value *User) error

	// HookBeforeFetch is a method that provides the ability to delete/clear a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheDelete func(key string) error

	// HookExternalCacheClearAll is a method that provides the ability to clear all keys in an external cache with an external hook.
	HookExternalCacheClearAll func() error

	// HookBeforeFetch is called right before a fetch is performed
	HookBeforeFetch func(keys []string, loaderName string)

	// HookAfterFetch is called right after a fetch is performed
	HookAfterFetch func(keys []string, loaderName string)

	// HookAfterSet is called after a value is set in the cache
	HookAfterSet func(key string, value *User)

	// HookAfterPrime is called after a value is primed in the cache using Prime or ForcePrime
	HookAfterPrime func(key string, value *User)

	// HookAfterClear is called after a value is cleared from the cache
	HookAfterClear func(key string)

	// HookAfterClearAll is called after all values are cleared from the cache
	HookAfterClearAll func()

	// HookAfterExpired is called after a value is cleared in the cache due to expiration
	HookAfterExpired func(key string)

	// RedisConfig is used to configure a UserLoader backed by Redis, disabling the internal cache.
	RedisConfig *UserLoaderRedisConfig
}

UserLoaderConfig captures the config to create a new UserLoader

type UserLoaderRedisConfig added in v0.4.4

type UserLoaderRedisConfig struct {
	// SetTTL is the TTL (Time To Live) for a key to live in Redis on set. If nil, no TTL will be set.
	SetTTL *time.Duration

	// GetFunc should get a value from Redis given a key and return the raw string value.
	GetFunc func(ctx context.Context, key string) (string, error)

	// GetManyFunc should get one or more values from Redis given a set of keys and return the raw string values, errors the size of keys with non nil values for keys not found, and an error if any other error occurred running the command
	// If not set then GetFunc will be used instead, but will be called one at a time for each key
	GetManyFunc func(ctx context.Context, keys []string) ([]string, []error, error)

	// SetFunc should set a value in Redis given a key and value with an optional ttl (Time To Live)
	SetFunc func(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

	// DeleteFunc should delete a value in Redis given a key
	DeleteFunc func(ctx context.Context, key string) error

	// DeleteManyFunc should delete one or more values in Redis given a set of keys
	DeleteManyFunc func(ctx context.Context, key []string) error

	// GetKeysFunc should return all keys in Redis matching the given pattern. If not set then ClearAll() for this dataloader will not be supported.
	GetKeysFunc func(ctx context.Context, pattern string) ([]string, error)

	// ObjMarshal provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjMarshal func(any) ([]byte, error)

	// ObjUnmarshaler provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjUnmarshal func([]byte, any) error

	// KeyToStringFunc provides you the ability to specify your own function to convert a key to a string, which will be used instead of serialization.
	// This is only used for non standard types that need to be serialized. If not set, the ObjMarshal function (user defined or default) will be used to serialize a key into a string value
	// Example: If you have a struct with a String() function that returns a string representation of the struct, you can set this function to that function.
	//
	// type MyStruct struct {
	//     ID string
	//     OrgID string
	// }
	// ...
	// UserLoaderRedisConfig{
	//		KeyToStringFunc = func(key string) string { return m.ID + ":" + m.OrgID }
	// }
	// ...
	// Or if your key type has a String() function that returns a string representation of the key, you can set this function like this:
	// UserLoaderRedisConfig{
	//		KeyToStringFunc = func(key string) string { return key.String() }
	// }
	KeyToStringFunc func(key string) string
}

UserLoaderRedisConfig is used to configure a UserLoader backed by Redis. GetFunc, SetFunc, and DeleteFunc are required if using Redis. If any function is not provided, Redis will be disabled and internal caching will be used.

type UserValueByIDAndOrgLoader added in v0.4.4

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

UserValueByIDAndOrgLoader batches and caches requests

func NewUserValueByIDAndOrgLoader added in v0.4.4

func NewUserValueByIDAndOrgLoader(config UserValueByIDAndOrgLoaderConfig) *UserValueByIDAndOrgLoader

NewUserValueByIDAndOrgLoader creates a new UserValueByIDAndOrgLoader given a fetch, wait, and maxBatch

func (*UserValueByIDAndOrgLoader) Clear added in v0.4.4

Clear the value at key from the cache, if it exists

func (*UserValueByIDAndOrgLoader) ClearAll added in v0.4.4

func (l *UserValueByIDAndOrgLoader) ClearAll()

ClearAll clears all values from the cache

func (*UserValueByIDAndOrgLoader) ForcePrime added in v0.4.4

func (l *UserValueByIDAndOrgLoader) ForcePrime(key UserByIDAndOrg, value User)

ForcePrime the cache with the provided key and value. If the key already exists, value is replaced (This removes the requirement to clear the key first with loader.clear(key).prime(key, value))

func (*UserValueByIDAndOrgLoader) Load added in v0.4.4

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

func (*UserValueByIDAndOrgLoader) LoadAll added in v0.4.4

func (l *UserValueByIDAndOrgLoader) LoadAll(keys []UserByIDAndOrg) ([]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 (*UserValueByIDAndOrgLoader) LoadAllThunk added in v0.4.4

func (l *UserValueByIDAndOrgLoader) LoadAllThunk(keys []UserByIDAndOrg) 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. TODO: Add support for Redis GetManyFunc

func (*UserValueByIDAndOrgLoader) LoadThunk added in v0.4.4

func (l *UserValueByIDAndOrgLoader) LoadThunk(key UserByIDAndOrg) (User, 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.

func (*UserValueByIDAndOrgLoader) MarshalUserValueByIDAndOrgLoaderToString added in v0.4.4

func (l *UserValueByIDAndOrgLoader) MarshalUserValueByIDAndOrgLoaderToString(v UserByIDAndOrg) string

MarshalUserValueByIDAndOrgLoaderToString is a helper method to marshal a UserValueByIDAndOrgLoader to a string

func (*UserValueByIDAndOrgLoader) Prime added in v0.4.4

func (l *UserValueByIDAndOrgLoader) Prime(key UserByIDAndOrg, value User) bool

Prime the cache with the provided key and value. If the key already exists, no change is made and false is returned. (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)

func (*UserValueByIDAndOrgLoader) PrimeMany added in v0.4.4

func (l *UserValueByIDAndOrgLoader) PrimeMany(keys []UserByIDAndOrg, values []User) []bool

PrimeMany will prime the cache with the given keys and values. Value index is matched to key index.

type UserValueByIDAndOrgLoaderConfig added in v0.4.4

type UserValueByIDAndOrgLoaderConfig struct {
	// Fetch is a method that provides the data for the loader
	Fetch func(keys []UserByIDAndOrg) ([]User, []error)

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

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

	// HookExternalCacheGet is a method that provides the ability to lookup a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	// If the key is found in the external cache, the value should be returned along with true.
	// If the key is not found in the external cache, an empty/nil value should be returned along with false.
	// Both HookExternalCacheGet, HookExternalCacheSet, HookExternalCacheDelete, and HookExternalCacheClearAll should be set if using an external cache.
	HookExternalCacheGet func(key UserByIDAndOrg) (User, bool)

	// HookExternalCacheSet is a method that provides the ability to set a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheSet func(key UserByIDAndOrg, value User) error

	// HookBeforeFetch is a method that provides the ability to delete/clear a key in an external cache with an external hook.
	// This replaces the use of the internal cache.
	HookExternalCacheDelete func(key UserByIDAndOrg) error

	// HookExternalCacheClearAll is a method that provides the ability to clear all keys in an external cache with an external hook.
	HookExternalCacheClearAll func() error

	// HookBeforeFetch is called right before a fetch is performed
	HookBeforeFetch func(keys []UserByIDAndOrg, loaderName string)

	// HookAfterFetch is called right after a fetch is performed
	HookAfterFetch func(keys []UserByIDAndOrg, loaderName string)

	// HookAfterSet is called after a value is set in the cache
	HookAfterSet func(key UserByIDAndOrg, value User)

	// HookAfterPrime is called after a value is primed in the cache using Prime or ForcePrime
	HookAfterPrime func(key UserByIDAndOrg, value User)

	// HookAfterClear is called after a value is cleared from the cache
	HookAfterClear func(key UserByIDAndOrg)

	// HookAfterClearAll is called after all values are cleared from the cache
	HookAfterClearAll func()

	// HookAfterExpired is called after a value is cleared in the cache due to expiration
	HookAfterExpired func(key UserByIDAndOrg)

	// RedisConfig is used to configure a UserValueByIDAndOrgLoader backed by Redis, disabling the internal cache.
	RedisConfig *UserValueByIDAndOrgLoaderRedisConfig
}

UserValueByIDAndOrgLoaderConfig captures the config to create a new UserValueByIDAndOrgLoader

type UserValueByIDAndOrgLoaderRedisConfig added in v0.4.4

type UserValueByIDAndOrgLoaderRedisConfig struct {
	// SetTTL is the TTL (Time To Live) for a key to live in Redis on set. If nil, no TTL will be set.
	SetTTL *time.Duration

	// GetFunc should get a value from Redis given a key and return the raw string value.
	GetFunc func(ctx context.Context, key string) (string, error)

	// GetManyFunc should get one or more values from Redis given a set of keys and return the raw string values, errors the size of keys with non nil values for keys not found, and an error if any other error occurred running the command
	// If not set then GetFunc will be used instead, but will be called one at a time for each key
	GetManyFunc func(ctx context.Context, keys []string) ([]string, []error, error)

	// SetFunc should set a value in Redis given a key and value with an optional ttl (Time To Live)
	SetFunc func(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

	// DeleteFunc should delete a value in Redis given a key
	DeleteFunc func(ctx context.Context, key string) error

	// DeleteManyFunc should delete one or more values in Redis given a set of keys
	DeleteManyFunc func(ctx context.Context, key []string) error

	// GetKeysFunc should return all keys in Redis matching the given pattern. If not set then ClearAll() for this dataloader will not be supported.
	GetKeysFunc func(ctx context.Context, pattern string) ([]string, error)

	// ObjMarshal provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjMarshal func(any) ([]byte, error)

	// ObjUnmarshaler provides you the ability to specify your own encoding package. If not set, the default encoding/json package will be used.
	ObjUnmarshal func([]byte, any) error

	// KeyToStringFunc provides you the ability to specify your own function to convert a key to a string, which will be used instead of serialization.
	// This is only used for non standard types that need to be serialized. If not set, the ObjMarshal function (user defined or default) will be used to serialize a key into a string value
	// Example: If you have a struct with a String() function that returns a string representation of the struct, you can set this function to that function.
	//
	// type MyStruct struct {
	//     ID string
	//     OrgID string
	// }
	// ...
	// UserValueByIDAndOrgLoaderRedisConfig{
	//		KeyToStringFunc = func(key UserByIDAndOrg) string { return m.ID + ":" + m.OrgID }
	// }
	// ...
	// Or if your key type has a String() function that returns a string representation of the key, you can set this function like this:
	// UserValueByIDAndOrgLoaderRedisConfig{
	//		KeyToStringFunc = func(key UserByIDAndOrg) string { return key.String() }
	// }
	KeyToStringFunc func(key UserByIDAndOrg) string
}

UserValueByIDAndOrgLoaderRedisConfig is used to configure a UserValueByIDAndOrgLoader backed by Redis. GetFunc, SetFunc, and DeleteFunc are required if using Redis. If any function is not provided, Redis will be disabled and internal caching will be used.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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