
syncutil
Package syncutil provides small concurrency primitives the rest of the codebase shares: AtomicConfig wraps atomic.Pointer[T] for hot-reloadable config, and TTLCache implements a generic time-bounded cache with background eviction.
Index
- type AtomicConfig
- type TTLCache
- func NewTTLCache[K comparable, V any](ttl time.Duration) *TTLCache[K, V]
- func (c *TTLCache[K, V]) Clear()
- func (c *TTLCache[K, V]) Close()
- func (c *TTLCache[K, V]) Delete(key K)
- func (c *TTLCache[K, V]) Get(key K) (V, bool)
- func (c *TTLCache[K, V]) Len() int
- func (c *TTLCache[K, V]) Set(key K, value V)
- func (c *TTLCache[K, V]) SetWithTTL(key K, value V, ttl time.Duration)
type AtomicConfig
AtomicConfig is a generic thread-safe holder for a pointer to T. It wraps atomic.Pointer[T] for hot-reloadable configuration or any value that must be read and written concurrently without a mutex. The zero value is usable; Load returns nil before any Store.
func (*AtomicConfig[T]) Load
Load atomically returns the current pointer, or nil if none was stored.
func (*AtomicConfig[T]) Store
Store atomically replaces the stored pointer.
type TTLCache
TTLCache is a generic in-memory cache with time-based expiry and background eviction. The zero value is not usable; create instances with NewTTLCache.
func NewTTLCache
NewTTLCache creates a cache with the given default TTL. If ttl > 0, a background goroutine sweeps expired entries at the TTL interval.
func (*TTLCache[K, V]) Clear
Clear removes all entries from the cache.
func (*TTLCache[K, V]) Close
Close stops the background eviction goroutine. Safe to call multiple times.
func (*TTLCache[K, V]) Delete
Delete removes a single key from the cache.
func (*TTLCache[K, V]) Get
Get returns the cached value for the key, or the zero value and false if the key is missing or expired.
func (*TTLCache[K, V]) Len
Len returns the number of entries in the cache, including expired entries that have not yet been swept by the background eviction goroutine.
func (*TTLCache[K, V]) Set
Set stores a key-value pair with the cache’s default TTL.
func (*TTLCache[K, V]) SetWithTTL
SetWithTTL stores a key-value pair with a custom TTL. Use this when callers need per-entry TTL variation (e.g. jitter to prevent expiry storms).
Generated by gomarkdoc