Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AtomicMap ¶
type AtomicMap[K comparable, V any] struct { // contains filtered or unexported fields }
AtomicMap is a read-mostly, thread-safe map backed by an atomic.Pointer. It replaces the whole map on every write, so it avoids an RWMutex when writes are rare, done in the background and swap the entire map at once. The zero value is ready to use and safe for concurrent reads.
func (*AtomicMap[K, V]) Get ¶
Get returns the value stored for key and whether it was present. On a never-stored (zero) map it returns the zero value and false.
type RWMap ¶
type RWMap[K comparable, T any] struct { // contains filtered or unexported fields }
RWMap is a generic map guarded by an RWMutex, safe for concurrent use. Prefer it over AtomicMap when writes are frequent and touch single keys. The zero value is not usable; create one with NewRWMap.
func NewRWMap ¶
func NewRWMap[K comparable, T any](capacity int) *RWMap[K, T]
NewRWMap returns an RWMap whose backing map is pre-sized to capacity.
func (*RWMap[K, T]) Copy ¶
func (r *RWMap[K, T]) Copy() map[K]T
Copy returns a shallow copy of the map that is safe to read without locking.
func (*RWMap[K, T]) Keys ¶
func (r *RWMap[K, T]) Keys() []K
Keys returns a snapshot of the current keys in unspecified order.
type Refresher ¶
type Refresher[K comparable, V any] func() (map[K]V, error)
Refresher produces the full map contents for one refresh cycle.
type RefreshingAtomicMap ¶
type RefreshingAtomicMap[K comparable, V any] struct { // contains filtered or unexported fields }
RefreshingAtomicMap wraps an AtomicMap that is periodically rebuilt from a Refresher in the background. Reads are lock-free via Get.
func NewRefreshingAtomicMap ¶
func NewRefreshingAtomicMap[K comparable, V any]( ctx context.Context, period time.Duration, refresher Refresher[K, V], ) (*RefreshingAtomicMap[K, V], error)
NewRefreshingAtomicMap builds the map once synchronously (returning an error if that first refresh fails) and then spawns a goroutine that refreshes it every period. The goroutine runs until ctx is cancelled; failed background refreshes are logged and keep the previous contents.
func (*RefreshingAtomicMap[K, V]) Get ¶
func (r *RefreshingAtomicMap[K, V]) Get(key K) (V, bool)
Get returns the value for key from the most recent successful refresh.