mirror of
https://github.com/cirruslabs/orchard.git
synced 2026-10-18 22:48:57 +02:00
Refactorings for simplify readability (#35)
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package concurrentmap
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ConcurrentMap[T any] struct {
|
||||
nonConcurrentMap map[any]T
|
||||
mtx sync.Mutex
|
||||
}
|
||||
|
||||
func NewConcurrentMap[T any]() *ConcurrentMap[T] {
|
||||
return &ConcurrentMap[T]{
|
||||
nonConcurrentMap: map[any]T{},
|
||||
}
|
||||
}
|
||||
|
||||
func (cmap *ConcurrentMap[T]) Load(key any) (T, bool) {
|
||||
cmap.mtx.Lock()
|
||||
defer cmap.mtx.Unlock()
|
||||
|
||||
result, ok := cmap.nonConcurrentMap[key]
|
||||
|
||||
return result, ok
|
||||
}
|
||||
|
||||
func (cmap *ConcurrentMap[T]) Store(id any, value T) {
|
||||
cmap.mtx.Lock()
|
||||
defer cmap.mtx.Unlock()
|
||||
|
||||
cmap.nonConcurrentMap[id] = value
|
||||
}
|
||||
|
||||
func (cmap *ConcurrentMap[T]) Delete(key any) {
|
||||
cmap.mtx.Lock()
|
||||
defer cmap.mtx.Unlock()
|
||||
|
||||
delete(cmap.nonConcurrentMap, key)
|
||||
}
|
||||
Reference in New Issue
Block a user