codex optz.
This commit is contained in:
62
cache/mapsync/map.go
vendored
Normal file
62
cache/mapsync/map.go
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user