Documentation
¶
Overview ¶
Package syncutil provides concurrency utilities.
Index ¶
- func MapGetOrCreate[K comparable, V any](mu *sync.RWMutex, cache map[K]V, key K, create func() (V, error)) (V, error)
- type Registry
- func (r *Registry[K, V]) Get(key K) (V, bool)
- func (r *Registry[K, V]) GetOrCreate(key K, create func() V) V
- func (r *Registry[K, V]) Has(key K) bool
- func (r *Registry[K, V]) Keys() []K
- func (r *Registry[K, V]) Len() int
- func (r *Registry[K, V]) Range(fn func(K, V) bool)
- func (r *Registry[K, V]) Remove(key K)
- func (r *Registry[K, V]) Set(key K, value V)
- type TTLCache
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]) 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]) 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]) Range ¶ added in v0.4.24
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.
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
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.