codex optz.

This commit is contained in:
2026-06-20 23:29:58 +08:00
parent 40afc6c0d5
commit ebed85772f
11 changed files with 282 additions and 422 deletions

62
cache/mapsync/map.go vendored Normal file
View 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
}