57 lines
989 B
Go
57 lines
989 B
Go
package concurrentmap
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type ConcurrentMap[T any] struct {
|
|
nonConcurrentMap map[string]T
|
|
mtx sync.Mutex
|
|
}
|
|
|
|
func NewConcurrentMap[T any]() *ConcurrentMap[T] {
|
|
return &ConcurrentMap[T]{
|
|
nonConcurrentMap: map[string]T{},
|
|
}
|
|
}
|
|
|
|
func (cmap *ConcurrentMap[T]) Load(key string) (T, bool) {
|
|
cmap.mtx.Lock()
|
|
defer cmap.mtx.Unlock()
|
|
|
|
result, ok := cmap.nonConcurrentMap[key]
|
|
|
|
return result, ok
|
|
}
|
|
|
|
func (cmap *ConcurrentMap[T]) Store(id string, value T) {
|
|
cmap.mtx.Lock()
|
|
defer cmap.mtx.Unlock()
|
|
|
|
cmap.nonConcurrentMap[id] = value
|
|
}
|
|
|
|
func (cmap *ConcurrentMap[T]) Delete(key string) {
|
|
cmap.mtx.Lock()
|
|
defer cmap.mtx.Unlock()
|
|
|
|
delete(cmap.nonConcurrentMap, key)
|
|
}
|
|
|
|
func (cmap *ConcurrentMap[T]) DeleteIf(key string, predicate func(T) bool) bool {
|
|
cmap.mtx.Lock()
|
|
defer cmap.mtx.Unlock()
|
|
|
|
value, ok := cmap.nonConcurrentMap[key]
|
|
if !ok {
|
|
return false
|
|
}
|
|
if !predicate(value) {
|
|
return false
|
|
}
|
|
|
|
delete(cmap.nonConcurrentMap, key)
|
|
|
|
return true
|
|
}
|