Documentation
¶
Overview ¶
Package syncmap provides a typed thread-safe map implementation.
Index ¶
- type SyncMap
- func (s *SyncMap[K, V]) Clone() SyncMap[K, V]
- func (s *SyncMap[K, V]) Delete(key K)
- func (s *SyncMap[K, V]) Keys() []K
- func (s *SyncMap[K, V]) Load(key K) (value V, ok bool)
- func (s *SyncMap[K, V]) LoadOrStoreFunc(key K, f func() (V, error)) (actual V, created bool, err error)
- func (s *SyncMap[K, V]) Range(f func(key K, value V) bool)
- func (s *SyncMap[K, V]) RemoveIf(f func(key K, value V) bool)
- func (s *SyncMap[K, V]) Replace(key K, value V) (previous V, ok bool)
- func (s *SyncMap[K, V]) ReplaceFunc(key K, f func(current V) (V, error)) (actual V, replaced bool, err error)
- func (s *SyncMap[K, V]) SetSize(size int)
- func (s *SyncMap[K, V]) Store(key K, value V)
- func (s *SyncMap[K, V]) Touch(key K, f func(value V) V) (found bool)
- func (s *SyncMap[K, V]) Values() []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SyncMap ¶
type SyncMap[K comparable, V any] struct { sync.Mutex // contains filtered or unexported fields }
SyncMap is a thread-safe map that uses Load and Store semantics. The zero value of SyncMap is an empty map ready to use. It must not be copied after first use.
func (*SyncMap[K, V]) Clone ¶
Clone returns a clone of the SyncMap. The clone has a separate underlying map, but depending on how the values are stored, the values may be shared between the two maps. The new map has its own lock from the source.
func (*SyncMap[K, V]) Delete ¶
func (s *SyncMap[K, V]) Delete(key K)
Delete deletes the value for a key.
func (*SyncMap[K, V]) Keys ¶
func (s *SyncMap[K, V]) Keys() []K
Keys returns a slice of all keys in the map.
func (*SyncMap[K, V]) Load ¶
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (*SyncMap[K, V]) LoadOrStoreFunc ¶ added in v0.20.0
func (s *SyncMap[K, V]) LoadOrStoreFunc(key K, f func() (V, error)) (actual V, created bool, err error)
LoadOrStoreFunc returns the existing value for the key if present, otherwise it calls f and stores the result of f() in the map. If f returns an error, the value is not stored and the error is returned. The lock is held while calling f, so f must not block.
func (*SyncMap[K, V]) Range ¶
Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration. The map is locked while calling f, so f must not block.
func (*SyncMap[K, V]) RemoveIf ¶ added in v0.19.0
RemoveIf calls a function for all keys in the map. The syncmap is locked while calling the function, so it must not block. If the function returns true, the key is removed from the map. It is safe to "pluck" out the values from within the function.
func (*SyncMap[K, V]) Replace ¶ added in v0.10.8
Replace replaces the value for a key, and returns the previous value.
func (*SyncMap[K, V]) ReplaceFunc ¶ added in v0.20.0
func (s *SyncMap[K, V]) ReplaceFunc(key K, f func(current V) (V, error)) (actual V, replaced bool, err error)
ReplaceFunc calls f for a key and replaces the value with the result of f. If the key does not exist, the function is not called and the map is not modified. If the function returns an error, the value is not stored and the error is returned. The lock is held while calling f, so f must not block.
func (*SyncMap[K, V]) SetSize ¶
SetSize ets the size of the map. This will pre-allocate the map to the specified size and must be called before any other operations or the call will be a no-op.
func (*SyncMap[K, V]) Store ¶
func (s *SyncMap[K, V]) Store(key K, value V)
Store sets the value for a key.
func (*SyncMap[K, V]) Touch ¶ added in v0.11.2
Touch calls a function for a specific key. The syncmap is locked while calling the function, so it must not block. This allows a safe way to update a value in the map without needing to lock the object itself. If the key does not exist, the funciton is not called and the map is not modified.