maputils

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func (r *AtomicMap[K, V]) Get(key K) (V, bool)

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.

func (*AtomicMap[K, V]) Len

func (r *AtomicMap[K, V]) Len() int

Len reports the number of entries, or 0 if nothing was stored yet.

func (*AtomicMap[K, V]) Store

func (r *AtomicMap[K, V]) Store(v map[K]V)

Store atomically replaces the current map with v. The caller must not read or write v afterwards, since it is now shared.

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]) Get

func (r *RWMap[K, T]) Get(k K) (T, bool)

Get returns the value for k and whether it was present.

func (*RWMap[K, T]) Keys

func (r *RWMap[K, T]) Keys() []K

Keys returns a snapshot of the current keys in unspecified order.

func (*RWMap[K, T]) Len

func (r *RWMap[K, T]) Len() int

Len reports the number of entries.

func (*RWMap[K, T]) Set

func (r *RWMap[K, T]) Set(k K, t T)

Set stores t under key k, overwriting any existing value.

func (*RWMap[K, T]) Store

func (r *RWMap[K, T]) Store(mapping map[K]T)

Store replaces the whole backing map with mapping. The caller must not mutate mapping afterwards, as it is adopted by reference.

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.

func (*RefreshingAtomicMap[K, V]) Refresh

func (r *RefreshingAtomicMap[K, V]) Refresh(ctx context.Context) error

Refresh runs the refresher once and atomically swaps in the new contents, leaving the current map untouched if the refresher returns an error.

Jump to

Keyboard shortcuts

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