zworld/engine/util/sync_map.go
2024-01-14 22:56:06 +08:00

31 lines
490 B
Go

package util
import (
"sync"
)
// Type-safe sync.Map implementation
// Read sync.Map documentation for caveats
type SyncMap[K comparable, V any] struct {
m sync.Map
}
func NewSyncMap[K comparable, V any]() *SyncMap[K, V] {
return &SyncMap[K, V]{
m: sync.Map{},
}
}
func (m *SyncMap[K, V]) Load(key K) (value V, exists bool) {
var v any
v, exists = m.m.Load(key)
if exists {
value = v.(V)
}
return
}
func (m *SyncMap[K, V]) Store(key K, value V) {
m.m.Store(key, value)
}