Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { // Once can be used to initialize values for lazy initialization. // Should be set before calling Get(). Once sync.Once // contains filtered or unexported fields }
Cache contains a synchronized value that is considered valid for a specific amount of time. An Update function must be set to provide an updated value when needed.
func NewFromFunc ¶
func NewFromFunc[T any](ttl time.Duration, opts Opts, update func(ctx context.Context) (T, error)) *Cache[T]
NewFromFunc allocates a new cached value instance and initializes it with an update function, making it ready for use.
func (*Cache[T]) Get ¶
Get will return a cached value or fetch a new one. Tf the Update function returns an error the value is forwarded as is and not cached.
func (*Cache[T]) GetWithCtx ¶
GetWithCtx will return a cached value or fetch a new one. passes a caller context, if caller context cancels nothing is cached. If the Update function returns an error the value is forwarded as is and not cached.
type Opts ¶
type Opts struct {
// When set to true, return the last cached value
// even if updating the value errors out.
// Returns the last good value AND the error.
ReturnLastGood bool
// If NoWait is set, Get() will return the last good value,
// if TTL has expired but 2x TTL has not yet passed,
// but will fetch a new value in the background.
NoWait bool
}
Opts contains options for the cache.