This commit is contained in:
殷勇 2023-10-27 12:01:34 +08:00
parent 8357fbd28f
commit cd30872b4a
6 changed files with 55 additions and 22 deletions

View File

@ -35,7 +35,7 @@ func (m *Mail) Init() {
} }
func newMail() *Mail { func NewMail() *Mail {
m := new(Mail) m := new(Mail)
m.Init() m.Init()
return m return m

View File

@ -6,6 +6,7 @@ import (
"main/common" "main/common"
"main/constant" "main/constant"
"q5" "q5"
"time"
) )
type MailMgr struct { type MailMgr struct {
@ -46,8 +47,7 @@ func (mm *MailMgr) FetchMailFromDB() {
func (mm *MailMgr) LoadFromDB() { func (mm *MailMgr) LoadFromDB() {
lastIdx := int64(0) lastIdx := int64(0)
// unixSec := time.Now().Unix() unixSec := time.Now().Unix()
unixSec := 0
limit := 1000 limit := 1000
done := false done := false
@ -65,7 +65,7 @@ func (mm *MailMgr) LoadFromDB() {
for rows.Next() { for rows.Next() {
empty = false empty = false
m := newMail() m := NewMail()
m.GameId = q5.ToInt(*rows.GetByName("gameid")) m.GameId = q5.ToInt(*rows.GetByName("gameid"))
m.MailId = q5.ToInt64(*rows.GetByName("mailid")) m.MailId = q5.ToInt64(*rows.GetByName("mailid"))
m.From = q5.ToString(*rows.GetByName("_from")) m.From = q5.ToString(*rows.GetByName("_from"))

View File

@ -3,13 +3,14 @@ package mail
import ( import (
"main/common" "main/common"
"main/constant" "main/constant"
"time"
) )
func (mm *MailMgr) AddMail(m *Mail) { func (mm *MailMgr) AddMail(m *Mail) {
//unixSec := int(time.Now().Unix()) unixSec := int32(time.Now().Unix())
//if m.Expiretime < unixSec { if m.ExpireTime < unixSec {
// return return
//} }
if _, exists := mm.allMailHash[m.MailId]; exists { if _, exists := mm.allMailHash[m.MailId]; exists {
return return
} }

View File

@ -33,16 +33,13 @@ type Player struct {
RegisterTime int32 RegisterTime int32
ReadMailHash map[int64]*ReadMail ReadMailHash map[int64]*ReadMail
DeletedMailHash map[int64]*DeletedMail DeletedMailHash map[int64]*DeletedMail
} CacheExpiration time.Time
func (p *Player) Init() {
p.ReadMailHash = make(map[int64]*ReadMail)
p.DeletedMailHash = make(map[int64]*DeletedMail)
} }
func (p *Player) GetAccountId() string { func (p *Player) GetAccountId() string {
return p.AccountId return p.AccountId
} }
func (p *Player) GetRegisterTime() int32 { func (p *Player) GetRegisterTime() int32 {
return p.RegisterTime return p.RegisterTime
} }
@ -58,6 +55,9 @@ func (p *Player) IsDeletedMail(mailId int64) bool {
} }
func (p *Player) MarkMail(mailIds string) { func (p *Player) MarkMail(mailIds string) {
p.Lock()
defer p.Unlock()
nowUnixSec := time.Now().Unix() nowUnixSec := time.Now().Unix()
mailMgrPtr := global.GetMailMgr().(*mail.MailMgr) mailMgrPtr := global.GetMailMgr().(*mail.MailMgr)
mailIdStrings := strings.Split(mailIds, ",") mailIdStrings := strings.Split(mailIds, ",")
@ -80,9 +80,11 @@ func (p *Player) MarkMail(mailIds string) {
} }
func (p *Player) DeleteMails(mailIds string) { func (p *Player) DeleteMails(mailIds string) {
p.Lock()
defer p.Unlock()
nowUnixSec := time.Now().Unix() nowUnixSec := time.Now().Unix()
mailMgrPtr := global.GetMailMgr().(*mail.MailMgr) mailMgrPtr := global.GetMailMgr().(*mail.MailMgr)
mailIdStrings := strings.Split(mailIds, ",") mailIdStrings := strings.Split(mailIds, ",")
for _, mailId := range mailIdStrings { for _, mailId := range mailIdStrings {
if len(mailId) <= 0 { if len(mailId) <= 0 {
@ -105,6 +107,9 @@ func (p *Player) DeleteMails(mailIds string) {
func (p *Player) AddToReadList(mailIds string) {} func (p *Player) AddToReadList(mailIds string) {}
func (p *Player) GetAttachment(mailIds string) []interface{} { func (p *Player) GetAttachment(mailIds string) []interface{} {
p.Lock()
defer p.Unlock()
attachments := make([]interface{}, 0) attachments := make([]interface{}, 0)
mailMgrPtr := global.GetMailMgr().(*mail.MailMgr) mailMgrPtr := global.GetMailMgr().(*mail.MailMgr)
mailIdStrings := strings.Split(mailIds, ",") mailIdStrings := strings.Split(mailIds, ",")
@ -128,9 +133,11 @@ func (p *Player) GetAttachment(mailIds string) []interface{} {
} }
func (p *Player) Deserialize(accountPB *ss.MFAccountData) { func (p *Player) Deserialize(accountPB *ss.MFAccountData) {
p.Lock()
defer p.Unlock()
var nextDaySec int32 = 3600 * 24 var nextDaySec int32 = 3600 * 24
nowUnixSec := int32(time.Now().Unix()) nowUnixSec := int32(time.Now().Unix())
for _, MFReadMail := range accountPB.GetReadMailList() { for _, MFReadMail := range accountPB.GetReadMailList() {
expireTime := MFReadMail.GetExpireTime() + nextDaySec expireTime := MFReadMail.GetExpireTime() + nextDaySec
if expireTime > nowUnixSec { if expireTime > nowUnixSec {
@ -166,9 +173,11 @@ func (p *Player) Deserialize(accountPB *ss.MFAccountData) {
} }
func (p *Player) Serialize(accountPB *ss.MFAccountData) { func (p *Player) Serialize(accountPB *ss.MFAccountData) {
p.Lock()
defer p.Unlock()
var nextDaySec int32 = 3600 * 24 var nextDaySec int32 = 3600 * 24
nowUnixSec := time.Now().Unix() nowUnixSec := time.Now().Unix()
for _, readMail := range p.ReadMailHash { for _, readMail := range p.ReadMailHash {
if int64(readMail.expireTime+nextDaySec) > nowUnixSec { if int64(readMail.expireTime+nextDaySec) > nowUnixSec {
p2 := &ss.MFReadMail{ p2 := &ss.MFReadMail{
@ -192,7 +201,20 @@ func (p *Player) Serialize(accountPB *ss.MFAccountData) {
} }
} }
func (p *Player) UpdateExpire() {
p.Lock()
defer p.Unlock()
p.CacheExpiration = time.Now().Add(10 * time.Second)
}
func (p *Player) IsCacheExpired() bool {
return time.Now().After(p.CacheExpiration)
}
func (p *Player) SaveToDB() { func (p *Player) SaveToDB() {
p.Lock()
defer p.Unlock()
accountPB := ss.MFAccountData{} accountPB := ss.MFAccountData{}
p.Serialize(&accountPB) p.Serialize(&accountPB)
blobData, err := proto.Marshal(&accountPB) blobData, err := proto.Marshal(&accountPB)
@ -229,3 +251,12 @@ func (p *Player) MarkDirty() {
}, },
) )
} }
func NewPlayer(accountId string) *Player {
return &Player{
AccountId: accountId,
ReadMailHash: make(map[int64]*ReadMail),
DeletedMailHash: make(map[int64]*DeletedMail),
CacheExpiration: time.Now().Add(10 * time.Second),
}
}

View File

@ -24,15 +24,10 @@ func (pm *PlayerMgr) LoadPlayer(accountId string, cb func(err error, p *Player))
aId := q5.ToString(*rows.GetByName("accountid")) aId := q5.ToString(*rows.GetByName("accountid"))
data := q5.ToString(*rows.GetByName("blobdata")) data := q5.ToString(*rows.GetByName("blobdata"))
profile := &Player{ profile := NewPlayer(aId)
AccountId: aId,
}
profile.Init()
accountPB := &ss.MFAccountData{} accountPB := &ss.MFAccountData{}
err = proto.Unmarshal([]byte(data), accountPB) err = proto.Unmarshal([]byte(data), accountPB)
profile.Deserialize(accountPB) profile.Deserialize(accountPB)
cb(nil, profile) cb(nil, profile)
} else { } else {
cb(fmt.Errorf("player nil"), nil) cb(fmt.Errorf("player nil"), nil)

View File

@ -27,6 +27,11 @@ func (pm *PlayerMgr) GetPlayer(accountId string) *Player {
} }
func (pm *PlayerMgr) AsyncGetPlayer(accountId string) *Player { func (pm *PlayerMgr) AsyncGetPlayer(accountId string) *Player {
p := pm.GetPlayer(accountId)
if p != nil && !p.IsCacheExpired() {
return p
}
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
@ -35,6 +40,7 @@ func (pm *PlayerMgr) AsyncGetPlayer(accountId string) *Player {
pm.LoadPlayer(accountId, func(err error, p *Player) { pm.LoadPlayer(accountId, func(err error, p *Player) {
defer wg.Done() defer wg.Done()
player = p player = p
pm.accountIdHash[p.GetAccountId()] = p
}) })
}(accountId) }(accountId)
wg.Wait() wg.Wait()