core/cache/mapsync/map_float.go

65 lines
810 B
Go
Raw Normal View History

package mapsync
2025-05-03 15:49:16 +08:00
import (
"sync"
)
var (
// sync map
MapFloat *mapFloat
2025-05-03 15:49:16 +08:00
)
// lock
type mapFloat struct {
2025-05-03 15:49:16 +08:00
sync.RWMutex
Data map[string]float64
}
func NewMapFloat() *mapFloat {
return &mapFloat{
2025-05-03 15:49:16 +08:00
Data: make(map[string]float64),
}
}
func (c *mapFloat) Set(key string, val float64) {
c.Lock()
defer c.Unlock()
c.Data[key] = val
2025-05-03 15:49:16 +08:00
}
func (c *mapFloat) Get(key string) float64 {
2025-05-03 15:49:16 +08:00
c.RLock()
defer c.RUnlock()
vals, ok := c.Data[key]
if !ok {
return 0
}
return vals
}
func (c *mapFloat) Del(key string) {
2025-05-03 15:49:16 +08:00
c.Lock()
defer c.Unlock()
delete(c.Data, key)
2025-05-03 15:49:16 +08:00
}
func (c *mapFloat) Keys() (keys []string) {
2025-05-03 15:49:16 +08:00
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
}