ezcache/cache.go
2025-04-21 11:07:53 -05:00

31 lines
997 B
Go

package ezcache
import (
"errors"
"time"
)
// ErrInvalidFetcher is returned by Cache.SetFetcher if the fetcher is invalid.
// This is probably only going to happen if it's nil.
var ErrInvalidFetcher = errors.New("invalid fetcher")
// ErrInvalidDuration is returned by Cache.SetExpiry if the duration is invalid.
// This is usually if it is <= 0.
var ErrInvalidDuration = errors.New("invalid duration")
type Fetcher[K comparable, V any] func(K) (V, error)
// Cache represents a Cache for values.
type Cache[K comparable, V any] interface {
// Get will fetch the value for key. If in the cache, it will fetch the cached value.
// If not in the cache, it will use the Fetcher.
// It may return [ErrInvalidFetcher], but if an error is returned, it's probably returned by the
// [Fetcher] itself.
Get(key K) (V, error)
// SetFetcher sets the fetcher for this [Cache].
SetFetcher(f Fetcher[K, V]) error
// SetExpiry sets the expiry for this [Cache].
SetExpiry(d time.Duration) error
}