fix(platform): use built-in sync.Map

relates to #4116
This commit is contained in:
Jan De Dobbeleer 2023-08-04 08:17:30 +02:00 committed by Jan De Dobbeleer
parent 8554fb66a6
commit 2007f9d1ab

View file

@ -2,47 +2,35 @@ package platform
import "sync" import "sync"
type ConcurrentMap struct {
values map[string]interface{}
sync.RWMutex
}
func NewConcurrentMap() *ConcurrentMap { func NewConcurrentMap() *ConcurrentMap {
return &ConcurrentMap{ var cm ConcurrentMap
values: make(map[string]interface{}), return &cm
}
} }
func (c *ConcurrentMap) Set(key string, value interface{}) { type ConcurrentMap sync.Map
c.Lock()
defer c.Unlock() func (cm *ConcurrentMap) Set(key string, value any) {
c.values[key] = value (*sync.Map)(cm).Store(key, value)
} }
func (c *ConcurrentMap) Get(key string) (interface{}, bool) { func (cm *ConcurrentMap) Get(key string) (any, bool) {
c.RLock() return (*sync.Map)(cm).Load(key)
defer c.RUnlock()
if val, ok := c.values[key]; ok {
return val, true
}
return "", false
} }
func (c *ConcurrentMap) Delete(key string) { func (cm *ConcurrentMap) Delete(key string) {
c.RLock() (*sync.Map)(cm).Delete(key)
defer c.RUnlock()
delete(c.values, key)
} }
func (c *ConcurrentMap) List() map[string]interface{} { func (cm *ConcurrentMap) Contains(key string) bool {
return c.values _, ok := (*sync.Map)(cm).Load(key)
return ok
} }
func (c *ConcurrentMap) Contains(key string) bool { func (cm *ConcurrentMap) List() map[string]any {
c.RLock() list := make(map[string]any)
defer c.RUnlock() (*sync.Map)(cm).Range(func(key, value any) bool {
if _, ok := c.values[key]; ok { list[key.(string)] = value
return true return true
} })
return false return list
} }