1 Star 0 Fork 0

地瓜2015 / go-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
counter.go 840 Bytes
一键复制 编辑 原始数据 按行查看 历史
kai.zhang1 提交于 2021-11-28 23:10 . add storage
package utils
import (
"sync"
)
// Map exported
type Map struct {
sync.Map
mu sync.RWMutex
}
// Incr map
func (m *Map) Incr(key string, amount int) {
m.mu.Lock()
if count, ok := m.LoadOrStore(key, amount); ok {
m.Store(key, count.(int)+amount)
}
m.mu.Unlock()
}
// Counter exported
type Counter struct {
sync.RWMutex
Data map[string]int
}
// Get exported
func (c *Counter) Get(key string) int {
c.RLock()
count := c.Data[key]
c.RUnlock()
return count
}
// Set exported
func (c *Counter) Set(key string, value int) {
c.Lock()
c.Data[key] = value
c.Unlock()
}
// Incr exported
func (c *Counter) Incr(key string, amount int) int {
c.Lock()
c.Data[key] += amount
count := c.Data[key]
c.Unlock()
return count
}
// Delete exported
func (c *Counter) Delete(key string) {
c.Lock()
delete(c.Data, key)
c.Unlock()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zkdfbb/go-utils.git
git@gitee.com:zkdfbb/go-utils.git
zkdfbb
go-utils
go-utils
v0.1.8

搜索帮助

344bd9b3 5694891 D2dac590 5694891