Skip to content

Commit

Permalink
up map.go
Browse files Browse the repository at this point in the history
  • Loading branch information
mayiweb committed Dec 25, 2023
1 parent fef0c25 commit 1273581
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions safe/map.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* 安全 Map防止并发读写
* 安全 Map 防止并发读写
*/

package safe
Expand Down Expand Up @@ -36,8 +36,8 @@ func (p *Map) Set(name interface{}, value interface{}) {
// 获取 map
func (p *Map) Get(name interface{}) (interface{}, bool) {

p.Lock.Lock()
defer p.Lock.Unlock()
p.Lock.RLock()
defer p.Lock.RUnlock()

mVal, mOk := p.Map[name]
if !mOk {
Expand Down Expand Up @@ -67,20 +67,25 @@ func (p *Map) Clear() {

// 数据条数
func (p *Map) Size() int {
p.Lock.Lock()
defer p.Lock.Unlock()
p.Lock.RLock()
defer p.Lock.RUnlock()

return len(p.Map)
}

// 获取全部 map
func (p *Map) GetAll() map[interface{}]interface{} {
p.Lock.Lock()
defer p.Lock.Unlock()
p.Lock.RLock()
defer p.Lock.RUnlock()

// 申明一个新的 map 将 p.map 数据写入新 map 中,再访问数据就不是同一份数据了
Result := make(map[interface{}]interface{}, 0)

list := p.Map
for k, v := range p.Map {
Result[k] = v
}

return list
return Result
}

// ------------------- 二级 map -------------------
Expand All @@ -105,8 +110,8 @@ func (p *Map2) Set(key1 interface{}, key2 interface{}, value interface{}) {
// 获取 map
func (p *Map2) Get(key1 interface{}, key2 interface{}) (interface{}, bool) {

p.Lock.Lock()
defer p.Lock.Unlock()
p.Lock.RLock()
defer p.Lock.RUnlock()

mVal, mOk := p.Map[key1][key2]
if !mOk {
Expand All @@ -118,21 +123,28 @@ func (p *Map2) Get(key1 interface{}, key2 interface{}) (interface{}, bool) {

// 获取二级 key 列表
func (p *Map2) GetList(key1 interface{}) (map[interface{}]interface{}, bool) {
p.Lock.Lock()
defer p.Lock.Unlock()
p.Lock.RLock()
defer p.Lock.RUnlock()

mVal, mOk := p.Map[key1]
mLists, mOk := p.Map[key1]
if !mOk {
return nil, false
}

return mVal, true
// 申明一个新的 map 将 p.map 数据写入新 map 中,再访问数据就不是同一份数据了
Result := make(map[interface{}]interface{}, 0)

for k, v := range mLists {
Result[k] = v
}

return Result, true
}

// 获取二级 key 列数据条数
func (p *Map2) GetListSize(key1 interface{}) int {
p.Lock.Lock()
defer p.Lock.Unlock()
p.Lock.RLock()
defer p.Lock.RUnlock()

_, mOk := p.Map[key1]
if !mOk {
Expand Down Expand Up @@ -171,16 +183,30 @@ func (p *Map2) Clear() {

// 数据条数
func (p *Map2) Size() int {
p.Lock.Lock()
defer p.Lock.Unlock()
p.Lock.RLock()
defer p.Lock.RUnlock()

return len(p.Map)
}

// 获取全部 map
func (p *Map2) GetAll() map[interface{}]map[interface{}]interface{} {
p.Lock.Lock()
defer p.Lock.Unlock()
p.Lock.RLock()
defer p.Lock.RUnlock()

// 申明一个新的 map 将 p.map 数据写入新 map 中,再访问数据就不是同一份数据了
Result := make(map[interface{}]map[interface{}]interface{}, 0)

for k, v := range p.Map {

ResultList := make(map[interface{}]interface{}, 0)

for sk, sv := range v {
ResultList[sk] = sv
}

Result[k] = ResultList
}

return p.Map
return Result
}

0 comments on commit 1273581

Please sign in to comment.