syncutil

package
v0.4.24 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package syncutil provides concurrency utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapGetOrCreate added in v0.3.33

func MapGetOrCreate[K comparable, V any](
	mu *sync.RWMutex,
	cache map[K]V,
	key K,
	create func() (V, error),
) (V, error)

MapGetOrCreate looks up key in cache under a read lock; if missing, acquires a write lock, double-checks, and calls create to populate the entry. The entry is stored in cache only when create returns a nil error. create is called while the write lock is held, so it is safe for create to read or write other fields protected by the same mu.

Types

type Registry added in v0.4.24

type Registry[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Registry is a thread-safe key-value registry.

func NewRegistry added in v0.4.24

func NewRegistry[K comparable, V any]() *Registry[K, V]

NewRegistry creates an empty Registry.

func (*Registry[K, V]) Get added in v0.4.24

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

Get retrieves the value stored under key.

func (*Registry[K, V]) GetOrCreate added in v0.4.24

func (r *Registry[K, V]) GetOrCreate(key K, create func() V) V

GetOrCreate returns the value for key, creating and storing it when absent. create is called while the registry write lock is held and must not call a method on the same Registry.

func (*Registry[K, V]) Has added in v0.4.24

func (r *Registry[K, V]) Has(key K) bool

Has reports whether key is registered.

func (*Registry[K, V]) Keys added in v0.4.24

func (r *Registry[K, V]) Keys() []K

Keys returns a snapshot of all registered keys.

func (*Registry[K, V]) Len added in v0.4.24

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

Len returns the number of registered entries.

func (*Registry[K, V]) Range added in v0.4.24

func (r *Registry[K, V]) Range(fn func(K, V) bool)

Range calls fn for each registered entry. Iteration stops when fn returns false. Iteration runs over a snapshot taken while the read lock is held, so fn may call any Registry method without deadlocking, but it may observe entries that were concurrently removed or miss entries that were concurrently added.

func (*Registry[K, V]) Remove added in v0.4.24

func (r *Registry[K, V]) Remove(key K)

Remove deletes the value stored under key.

func (*Registry[K, V]) Set added in v0.4.24

func (r *Registry[K, V]) Set(key K, value V)

Set stores value under key.

type TTLCache added in v0.3.26

type TTLCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

TTLCache is a thread-safe generic get-or-create cache that combines lazy TTL eviction with an LRU size cap.

On every TTLCache.GetOrCreate call, all entries whose idle time exceeds the configured TTL are evicted first. If the cache is still at capacity after TTL eviction, the least-recently-used entry is evicted to make room.

func NewTTLCache added in v0.3.26

func NewTTLCache[K comparable, V any](ttl time.Duration, maxSize int) *TTLCache[K, V]

NewTTLCache creates a new TTLCache with the given entry TTL and maximum size. Entries idle longer than ttl are evicted lazily; when the cache reaches maxSize the least-recently-used entry is evicted on the next GetOrCreate call.

func (*TTLCache[K, V]) GetOrCreate added in v0.3.26

func (c *TTLCache[K, V]) GetOrCreate(key K, create func() V) V

GetOrCreate returns the cached value for key. If the key is not present, or has been evicted, create is called to produce a new value which is then stored and returned.

On each call, all expired entries are lazily evicted before the lookup. If the cache has reached its capacity after TTL eviction, the LRU entry is removed to make room.

create is called while the cache lock is held.

func (*TTLCache[K, V]) Len added in v0.3.26

func (c *TTLCache[K, V]) Len() int

Len returns the current number of entries held in the cache.

Jump to

Keyboard shortcuts

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