Skip to content

Commit

Permalink
fix 使用线程安全的map
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Apr 24, 2019
1 parent 639923f commit 96b0f69
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions GoroutineSessionMap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import (
)

type GoroutineSessionMap struct {
m map[int64]Session
lock sync.RWMutex
m sync.Map
}

func (it GoroutineSessionMap) New() GoroutineSessionMap {
return GoroutineSessionMap{
m: make(map[int64]Session),
m: sync.Map{},
}
}
func (it *GoroutineSessionMap) Put(k int64, session Session) {
it.lock.Lock()
defer it.lock.Unlock()
it.m[k] = session
it.m.Store(k, session)
}
func (it *GoroutineSessionMap) Get(k int64) Session {
return it.m[k]
var v, ok = it.m.Load(k)
if ok {
return v.(Session)
} else {
return nil
}
}

0 comments on commit 96b0f69

Please sign in to comment.