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
}

View File

@@ -1,64 +1,12 @@
package mapsync
import (
"sync"
)
var (
// sync map
MapFloat *mapFloat
)
// lock
type mapFloat struct {
sync.RWMutex
Data map[string]float64
}
type mapFloat = syncMap[float64]
func NewMapFloat() *mapFloat {
return &mapFloat{
Data: make(map[string]float64),
}
}
func (c *mapFloat) Set(key string, val float64) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
}
func (c *mapFloat) Get(key string) float64 {
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return 0
}
return vals
}
func (c *mapFloat) Del(key string) {
c.Lock()
defer c.Unlock()
delete(c.Data, key)
}
func (c *mapFloat) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
for k, _ := range c.Data {
keys = append(keys, k)
}
return
}
func (c *mapFloat) All() map[string]float64 {
c.RLock()
defer c.RUnlock()
return c.Data
return newSyncMap[float64]()
}

View File

@@ -1,64 +1,12 @@
package mapsync
import (
"sync"
)
var (
// sync map
MapInt *mapInt
)
// lock
type mapInt struct {
sync.RWMutex
Data map[string]int
}
type mapInt = syncMap[int]
func NewMapInt() *mapInt {
return &mapInt{
Data: make(map[string]int),
}
}
func (c *mapInt) Set(key string, val int) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
}
func (c *mapInt) Get(key string) int {
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return 0
}
return vals
}
func (c *mapInt) Del(key string) {
c.Lock()
defer c.Unlock()
delete(c.Data, key)
}
func (c *mapInt) All() map[string]int {
c.RLock()
defer c.RUnlock()
return c.Data
}
func (c *mapInt) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
for k, _ := range c.Data {
keys = append(keys, k)
}
return
return newSyncMap[int]()
}

View File

@@ -1,64 +1,12 @@
package mapsync
import (
"sync"
)
var (
// sync map
MapString *mapString
)
// lock
type mapString struct {
sync.RWMutex
Data map[string]string
}
type mapString = syncMap[string]
func NewMapString() *mapString {
return &mapString{
Data: make(map[string]string),
}
}
func (c *mapString) Set(key, val string) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
}
func (c *mapString) Get(key string) string {
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return ""
}
return vals
}
func (c *mapString) Del(key string) {
c.Lock()
defer c.Unlock()
delete(c.Data, key)
}
func (c *mapString) Keys() (keys []string) {
c.RLock()
defer c.RUnlock()
for k, _ := range c.Data {
keys = append(keys, k)
}
return
}
func (c *mapString) All() map[string]string {
c.RLock()
defer c.RUnlock()
return c.Data
return newSyncMap[string]()
}