Documentation
¶
Index ¶
- func Init[T comparable, L *sync.RWMutex | *sync.Mutex | *sync.Once](l L, ptr *T, init func() T) T
- func InitErr[T comparable, L *sync.RWMutex | *sync.Mutex](l L, ptr *T, init func() (T, error)) (T, error)
- type Map
- func (m *Map[K, V]) Borrow(key K) (ptr *V, release func(), ok bool)
- func (m *Map[K, V]) BorrowWithInit(key K, init func() V) (ptr *V, release func())
- func (m *Map[K, V]) Delete(key K)
- func (m *Map[K, V]) Do(fn func(vs map[K]V) error) error
- func (m *Map[K, V]) Get(key K) V
- func (m *Map[K, V]) GetOrInit(key K, init func() V) V
- func (m *Map[K, V]) GetOrInitErr(key K, init func() (V, error)) (V, error)
- func (m *Map[K, V]) Keys() []K
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Lookup(key K) (V, bool)
- func (m *Map[K, V]) Reset()
- func (m *Map[K, V]) Set(key K, val V)
- type RWLocker
- type RWLockerFactory
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Map ¶ added in v0.261.0
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Example ¶
package main
import (
"go.llib.dev/frameless/pkg/synckit"
)
func main() {
var m synckit.Map[string, int]
m.Set("foo", 42) // 42 set for "foo" key
m.Get("foo") // -> 42
m.Lookup("foo") // -> 42, true
m.Lookup("bar") // -> 0, false
if ptr, release, ok := m.Borrow("foo"); ok { // the value of "foo" is borrowed
*ptr = 24
release()
}
m.Reset() // map is cleared
m.GetOrInit("foo", func() int { // -> 42
return 42
})
}
func (*Map[K, V]) BorrowWithInit ¶ added in v0.261.0
func (m *Map[K, V]) BorrowWithInit(key K, init func() V) (ptr *V, release func())
func (*Map[K, V]) Do ¶ added in v0.293.0
Example ¶
package main
import (
"errors"
"go.llib.dev/frameless/pkg/synckit"
)
func main() {
var m synckit.Map[string, int]
m.Set("foo", 42) // 42 set for "foo" key
m.Get("foo") // -> 42
m.Lookup("foo") // -> 42, true
m.Lookup("bar") // -> 0, false
err := m.Do(func(vs map[string]int) error {
// this is protected by the map mutex
_ = vs["foo"] // 42, true
return errors.New("the-error")
})
_ = err // &errors.errorString{s:"the-error"}
}
func (*Map[K, V]) GetOrInit ¶ added in v0.261.0
func (m *Map[K, V]) GetOrInit(key K, init func() V) V
func (*Map[K, V]) GetOrInitErr ¶ added in v0.280.0
type RWLockerFactory ¶
type RWLockerFactory[K comparable] struct { // ReadOptimised will make RLocker and rsync perform much faster, // at the expense of having a global read locking when a read lock is made before a write locking. // This cause a side effect that acquiring write locks for other keys will hang until the read operation is done. // // In the end, this shortcut leads to a whopping <350% increase in read operation speed, // while also supporting write operations as well. // However, it causes only a 0.17% decrease in write speed performance. ReadOptimised bool // contains filtered or unexported fields }
func (*RWLockerFactory[K]) RWLocker ¶
func (l *RWLockerFactory[K]) RWLocker(key K) RWLocker
Click to show internal directories.
Click to hide internal directories.