Documentation
¶
Index ¶
- type CacheMap
- func (m *CacheMap[K, V]) Delete(key K)
- func (m *CacheMap[K, V]) Load(key K) (value V, ok bool)
- func (m *CacheMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (m *CacheMap[K, V]) LoadOrCompute(key K, fn func(K) V) (actual V, loaded bool)
- func (m *CacheMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *CacheMap[K, V]) Store(key K, value V)
- type Map
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Load(key K) (value V, ok bool)
- func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *Map[K, V]) Range(f func(key K, value V) bool)
- func (m *Map[K, V]) Store(key K, value V)
- type Pool
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheMap ¶
type CacheMap[K mapkey, V any] struct { // contains filtered or unexported fields }
A CacheMap is similar to a Map, but also has a .LoadOrCompute sibling to .LoadOrStore. This is useful for caching the results of computation where it is undesirable for concurrent calls to duplicate work.
Compared to a plain Map, a CacheMap has both more space overhead and more time overhead.
func (*CacheMap[K, V]) Load ¶
Load returns the value stored in the map for a key. If the value for that key is actively being computed by LoadOrCompute, this blocks until the value has been computed.
func (*CacheMap[K, V]) LoadAndDelete ¶
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present. If the value for that key was actively being computed by LoadOrCompute, this immediately removes the partial value from the CacheMap, but does not return until the computation has finished.
func (*CacheMap[K, V]) LoadOrCompute ¶
LoadOrCompute returns the existing value for the key if present. Otherwise, it computes and stores a value using the given function. The loaded result is true if the value was loaded, false if computed. If a prior call to LoadOrCompute is still computing the value for that key, a latter call blocks until the computation is complete, and then returns the initial call's value.
func (*CacheMap[K, V]) LoadOrStore ¶
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored. If the value for that key is actively being computed by LoadOrCompute, this blocks until the value has been computed.
type Map ¶
type Map[K mapkey, V any] struct { // contains filtered or unexported fields }
Map is a type-safe equivalent of the standard library's sync.Map.
With versions of Go prior to Go 1.20, Map is specified too loosely, as
Map[K any, V any]
while with Go 1.20 and later, Map is specified as
Map[K comparable, V any]
This is because with Go versions prior to 1.20, 'comparable' was overly strict, disallowing many types that are valid map-keys (see https://github.com/golang/go/issues/56548). The type used as K in a Map older versions of Go must be a valid map-key type, even though the type specification of Map does not enforce that.
func (*Map[K, V]) LoadOrStore ¶
type Value ¶
type Value[T comparable] struct { // contains filtered or unexported fields }
Value is a typed equivalent of sync/atomic.Value.
It is not actually a wrapper around sync/atomic.Value for allocation-performance reasons.