上传用户缓存锁

This commit is contained in:
liwei1dao 2024-01-11 19:05:21 +08:00
parent bea34c2b0e
commit b6d15f2b60

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"sync"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
@ -31,6 +32,7 @@ type UserSession struct {
UserId string UserId string
service IService service IService
msgqueue []*pb.UserMessage msgqueue []*pb.UserMessage
lock sync.RWMutex
mate map[string]interface{} mate map[string]interface{}
} }
@ -117,12 +119,16 @@ func (this *UserSession) UnBind() (err error) {
//写入元数据 //写入元数据
func (this *UserSession) SetMate(name string, value interface{}) { func (this *UserSession) SetMate(name string, value interface{}) {
this.lock.Lock()
this.mate[name] = value this.mate[name] = value
this.lock.Unlock()
} }
//写入元数据 //写入元数据
func (this *UserSession) GetMate(name string) (ok bool, value interface{}) { func (this *UserSession) GetMate(name string) (ok bool, value interface{}) {
this.lock.RLock()
value, ok = this.mate[name] value, ok = this.mate[name]
this.lock.RUnlock()
return return
} }
@ -189,9 +195,11 @@ func (this *UserSession) SyncPush() (err error) {
func (this *UserSession) Clone() (session IUserSession) { func (this *UserSession) Clone() (session IUserSession) {
session = this.service.GetUserSession() session = this.service.GetUserSession()
session.SetSession(this.IP, this.SessionId, this.ServiceTag, this.GatewayServiceId, this.UserId) session.SetSession(this.IP, this.SessionId, this.ServiceTag, this.GatewayServiceId, this.UserId)
this.lock.RLock()
for k, v := range this.mate { for k, v := range this.mate {
session.SetMate(k, v) session.SetMate(k, v)
} }
this.lock.RUnlock()
return return
} }