Files
core/cache/mapsync/map.go

63 lines
886 B
Go
Raw Normal View History

2026-06-20 23:29:58 +08:00
package mapsync
import "sync"
type syncMap[T any] struct {
sync.RWMutex
Data map[string]T
}
func newSyncMap[T any]() *syncMap[T] {
return &syncMap[T]{
Data: make(map[string]T),
}
}
func (c *syncMap[T]) Set(key string, val T) {
c.Lock()
defer c.Unlock()
if c.Data == nil {
c.Data = make(map[string]T)
}
c.Data[key] = val
}
func (c *syncMap[T]) Get(key string) T {
c.RLock()
defer c.RUnlock()
return c.Data[key]
}
func (c *syncMap[T]) Del(key string) {
c.Lock()
defer c.Unlock()
delete(c.Data, key)
}
func (c *syncMap[T]) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
keys = make([]string, 0, len(c.Data))
for k := range c.Data {
keys = append(keys, k)
}
return keys
}
func (c *syncMap[T]) All() map[string]T {
c.RLock()
defer c.RUnlock()
out := make(map[string]T, len(c.Data))
for k, v := range c.Data {
out[k] = v
}
return out
}