65 lines
768 B
Go
65 lines
768 B
Go
package mapsync
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
// sync map
|
|
MapInt *mapInt
|
|
)
|
|
|
|
// lock
|
|
type mapInt struct {
|
|
sync.RWMutex
|
|
Data map[string]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
|
|
}
|