聊天系统-系统架构设计

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()
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容