1 Star 2 Fork 3

kristas/booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
concurrent_map.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-09-17 17:21 . feat: rebuild log
package ccmap
import "sync"
type ConcurrentMap struct {
innerMap map[string]interface{}
locker sync.RWMutex
}
func NewConcurrentMap() *ConcurrentMap {
return &ConcurrentMap{innerMap: make(map[string]interface{})}
}
func (m *ConcurrentMap) Set(k string, v interface{}) {
m.locker.Lock()
defer m.locker.Unlock()
m.innerMap[k] = v
}
func (m *ConcurrentMap) Delete(k string) {
m.locker.Lock()
defer m.locker.Unlock()
delete(m.innerMap, k)
}
func (m *ConcurrentMap) Get(k string) interface{} {
m.locker.RLock()
defer m.locker.RUnlock()
return m.innerMap[k]
}
func (m *ConcurrentMap) GetOrDefault(k string, _default interface{}) interface{} {
m.locker.RLock()
defer m.locker.RUnlock()
i, ok := m.innerMap[k]
if !ok {
return _default
}
return i
}
func (m *ConcurrentMap) Exists(k string) bool {
m.locker.RLock()
defer m.locker.RUnlock()
_, ok := m.innerMap[k]
return ok
}
func (m *ConcurrentMap) ForEach(accept func(k string, v interface{})) {
m.locker.RLock()
defer m.locker.RUnlock()
for k := range m.innerMap {
accept(k, m.innerMap[k])
}
}
func (m *ConcurrentMap) Find(accept func(k string, v interface{}) bool) (find bool) {
for k := range m.innerMap {
find = accept(k, m.innerMap[k])
if find {
return
}
}
return
}
func (m *ConcurrentMap) Size() int {
m.locker.RLock()
defer m.locker.RUnlock()
return len(m.innerMap)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.3.8

搜索帮助