mirror of
https://github.com/muety/wakapi.git
synced 2025-12-05 22:20:24 -08:00
45 lines
783 B
Go
45 lines
783 B
Go
package lib
|
|
|
|
import "sync"
|
|
|
|
type ConcurrentMap[K comparable, V any] struct {
|
|
mu sync.RWMutex
|
|
items map[K]V
|
|
}
|
|
|
|
func NewConcurrentMap[K comparable, V any]() *ConcurrentMap[K, V] {
|
|
return &ConcurrentMap[K, V]{
|
|
items: make(map[K]V),
|
|
}
|
|
}
|
|
|
|
func (m *ConcurrentMap[K, V]) Set(key K, value V) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.items[key] = value
|
|
}
|
|
|
|
func (m *ConcurrentMap[K, V]) Get(key K) (V, bool) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
value, ok := m.items[key]
|
|
return value, ok
|
|
}
|
|
|
|
func (m *ConcurrentMap[K, V]) MustGet(key K) V {
|
|
val, _ := m.Get(key)
|
|
return val
|
|
}
|
|
|
|
func (m *ConcurrentMap[K, V]) Delete(key K) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
delete(m.items, key)
|
|
}
|
|
|
|
func (m *ConcurrentMap[K, V]) Len() int {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return len(m.items)
|
|
}
|