cachedvalue

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CachedValue

type CachedValue[T any] struct {
	// contains filtered or unexported fields
}

CachedValue is a thread-safe, TTL-based cache for a single value. It supports lazy fetching and returns stale values on fetch failure.

func New

func New[T any](ttl time.Duration) *CachedValue[T]

New creates a CachedValue with the given TTL.

func (*CachedValue[T]) GetOrFetch

func (c *CachedValue[T]) GetOrFetch(force bool, clone func(T) T, fetch func() (T, error)) (T, error)

GetOrFetch returns the cached value if fresh, otherwise calls fetch and updates the cache. On fetch error, returns stale cached data (if any) along with the error. The clone function is applied to the returned value to prevent callers from mutating the cache.

func (*CachedValue[T]) Read

func (c *CachedValue[T]) Read(clone func(T) T) T

Read returns the cached value without fetching. The clone function is applied before returning. If no value has been cached, returns the zero value of T.

func (*CachedValue[T]) Update

func (c *CachedValue[T]) Update(value T)

Update stores a value and resets the TTL timer.

Jump to

Keyboard shortcuts

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