2025-04-21 11:07:53 -05:00
|
|
|
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")
|
|
|
|
|
|
2025-04-21 11:24:51 -05:00
|
|
|
// ErrInvalidExpiry is returned by Cache.SetExpiry if the duration is invalid.
|
2025-04-21 11:07:53 -05:00
|
|
|
// This is usually if it is <= 0.
|
2025-04-21 11:24:51 -05:00
|
|
|
var ErrInvalidExpiry = errors.New("invalid duration")
|
2025-04-21 11:07:53 -05:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|