Session结构
//登陆用户Map:用于保存当前服务器登录用户的基本信息
var LoginUserSessionsMap map[int]*dataImpl.LoginUserSession
//登陆用户List:用户保存当前服务器登录用户的uid
var LoginUserSessionsList *list.List
由于map、list不支持并发,而使用场景需要用到很多并发,故采用map、list的线程安全写法如下:
map:
//登陆用户Map:用于保存当前服务器登录用户的基本信息
//登陆用户Map
var LoginUserSessionsMap LoginUserSessionsMapImpl
type LoginUserSessionsMapImpl struct {
data map[int]*dataImpl.LoginUserSession
sync.RWMutex
}
//登陆用户List
var LoginUserSessionsList *list.List
//线程安全的Map Put操作
func (this *LoginUserSessionsMapImpl) Put(key int, value *dataImpl.LoginUserSession) {
this.Lock()
defer this.Unlock()
this.data[key] = value
}
//线程安全的Map Get操作
func (this *LoginUserSessionsMapImpl) Get(key int) *dataImpl.LoginUserSession {
this.Lock()
defer this.Unlock()
return this.data[key]
}
list:
/*
登陆用户List
*/
var LoginUserSessionsList LoginUserSessionsListImpl
type LoginUserSessionsListImpl struct {
data *list.List
sync.RWMutex
}
func (this *LoginUserSessionsListImpl) PushFront(value int) {
this.Lock()
defer this.Unlock()
this.data.PushFront(value)
}
func (this *LoginUserSessionsListImpl) PushBack(value int) {
this.Lock()
defer this.Unlock()
this.data.PushBack(value)
}
func (this *LoginUserSessionsListImpl) Front() *list.Element {
this.Lock()
defer this.Unlock()
return this.data.Front()
}
func (this *LoginUserSessionsListImpl) Back() *list.Element {
this.Lock()
defer this.Unlock()
return this.data.Back()
}