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.Init()
return m

View File

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

View File

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

View File

@ -33,16 +33,13 @@ type Player struct {
RegisterTime int32
ReadMailHash map[int64]*ReadMail
DeletedMailHash map[int64]*DeletedMail
}
func (p *Player) Init() {
p.ReadMailHash = make(map[int64]*ReadMail)
p.DeletedMailHash = make(map[int64]*DeletedMail)
CacheExpiration time.Time
}
func (p *Player) GetAccountId() string {
return p.AccountId
}
func (p *Player) GetRegisterTime() int32 {
return p.RegisterTime
}
@ -58,6 +55,9 @@ func (p *Player) IsDeletedMail(mailId int64) bool {
}
func (p *Player) MarkMail(mailIds string) {
p.Lock()
defer p.Unlock()
nowUnixSec := time.Now().Unix()
mailMgrPtr := global.GetMailMgr().(*mail.MailMgr)
mailIdStrings := strings.Split(mailIds, ",")
@ -80,9 +80,11 @@ func (p *Player) MarkMail(mailIds string) {
}
func (p *Player) DeleteMails(mailIds string) {
p.Lock()
defer p.Unlock()
nowUnixSec := time.Now().Unix()
mailMgrPtr := global.GetMailMgr().(*mail.MailMgr)
mailIdStrings := strings.Split(mailIds, ",")
for _, mailId := range mailIdStrings {
if len(mailId) <= 0 {
@ -105,6 +107,9 @@ func (p *Player) DeleteMails(mailIds string) {
func (p *Player) AddToReadList(mailIds string) {}
func (p *Player) GetAttachment(mailIds string) []interface{} {
p.Lock()
defer p.Unlock()
attachments := make([]interface{}, 0)
mailMgrPtr := global.GetMailMgr().(*mail.MailMgr)
mailIdStrings := strings.Split(mailIds, ",")
@ -128,9 +133,11 @@ func (p *Player) GetAttachment(mailIds string) []interface{} {
}
func (p *Player) Deserialize(accountPB *ss.MFAccountData) {
p.Lock()
defer p.Unlock()
var nextDaySec int32 = 3600 * 24
nowUnixSec := int32(time.Now().Unix())
for _, MFReadMail := range accountPB.GetReadMailList() {
expireTime := MFReadMail.GetExpireTime() + nextDaySec
if expireTime > nowUnixSec {
@ -166,9 +173,11 @@ func (p *Player) Deserialize(accountPB *ss.MFAccountData) {
}
func (p *Player) Serialize(accountPB *ss.MFAccountData) {
p.Lock()
defer p.Unlock()
var nextDaySec int32 = 3600 * 24
nowUnixSec := time.Now().Unix()
for _, readMail := range p.ReadMailHash {
if int64(readMail.expireTime+nextDaySec) > nowUnixSec {
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() {
p.Lock()
defer p.Unlock()
accountPB := ss.MFAccountData{}
p.Serialize(&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"))
data := q5.ToString(*rows.GetByName("blobdata"))
profile := &Player{
AccountId: aId,
}
profile.Init()
profile := NewPlayer(aId)
accountPB := &ss.MFAccountData{}
err = proto.Unmarshal([]byte(data), accountPB)
profile.Deserialize(accountPB)
cb(nil, profile)
} else {
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 {
p := pm.GetPlayer(accountId)
if p != nil && !p.IsCacheExpired() {
return p
}
var wg sync.WaitGroup
wg.Add(1)
@ -35,6 +40,7 @@ func (pm *PlayerMgr) AsyncGetPlayer(accountId string) *Player {
pm.LoadPlayer(accountId, func(err error, p *Player) {
defer wg.Done()
player = p
pm.accountIdHash[p.GetAccountId()] = p
})
}(accountId)
wg.Wait()