save
This commit is contained in:
parent
c834b49870
commit
6809de4a6d
@ -34,6 +34,19 @@ type Player struct {
|
||||
ReadMailHash map[int64]*ReadMail
|
||||
DeletedMailHash map[int64]*DeletedMail
|
||||
CacheExpiration time.Time
|
||||
// 定时器
|
||||
dirtyTimer *f5.TimerWp
|
||||
attacher *f5.TimerAttacher
|
||||
dirty bool // 标记数据已修改
|
||||
}
|
||||
|
||||
func NewPlayer(accountId string) *Player {
|
||||
return &Player{
|
||||
AccountId: accountId,
|
||||
ReadMailHash: make(map[int64]*ReadMail),
|
||||
DeletedMailHash: make(map[int64]*DeletedMail),
|
||||
CacheExpiration: time.Now().Add(20 * time.Second),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Player) GetAccountId() string {
|
||||
@ -96,7 +109,7 @@ func (p *Player) DeleteMails(mailIds string) {
|
||||
p.MarkDirty()
|
||||
}
|
||||
|
||||
func (p *Player) AddToReadList(mailIds string) {}
|
||||
func (p *Player) AddToReadList() {}
|
||||
|
||||
func (p *Player) GetAttachment(mailIds string) []interface{} {
|
||||
attachments := make([]interface{}, 0)
|
||||
@ -160,9 +173,9 @@ func (p *Player) Deserialize(accountPB *ss.MFAccountData) {
|
||||
|
||||
func (p *Player) Serialize(accountPB *ss.MFAccountData) {
|
||||
var nextDaySec int32 = 3600 * 24
|
||||
nowUnixSec := time.Now().Unix()
|
||||
nowUnixSec := int32(time.Now().Unix())
|
||||
for _, readMail := range p.ReadMailHash {
|
||||
if int64(readMail.expireTime+nextDaySec) > nowUnixSec {
|
||||
if readMail.expireTime+nextDaySec > nowUnixSec {
|
||||
p2 := &ss.MFReadMail{
|
||||
MailId: &readMail.mailId,
|
||||
ReadTime: &readMail.readTime,
|
||||
@ -173,7 +186,7 @@ func (p *Player) Serialize(accountPB *ss.MFAccountData) {
|
||||
}
|
||||
|
||||
for _, deletedMail := range p.DeletedMailHash {
|
||||
if int64(deletedMail.expireTime+nextDaySec) > nowUnixSec {
|
||||
if deletedMail.expireTime+nextDaySec > nowUnixSec {
|
||||
p3 := &ss.MFDeletedMail{
|
||||
MailId: &deletedMail.mailId,
|
||||
DeleteTime: &deletedMail.deleteTime,
|
||||
@ -188,19 +201,49 @@ func (p *Player) UpdateExpire() {
|
||||
p.CacheExpiration = time.Now().Add(20 * time.Second)
|
||||
}
|
||||
|
||||
func (p *Player) IsCacheNotExpired() bool {
|
||||
return time.Now().Before(p.CacheExpiration)
|
||||
}
|
||||
|
||||
func (p *Player) IsCacheExpired() bool {
|
||||
return time.Now().After(p.CacheExpiration)
|
||||
}
|
||||
|
||||
func (p *Player) SaveToDB() {
|
||||
func (p *Player) MarkDirty() {
|
||||
// f5.GetApp().RegisterMainThreadCb(func() {})
|
||||
p.Lock()
|
||||
defer p.Unlock()
|
||||
p.dirty = true // 标记数据已修改
|
||||
p.ScheduleSave()
|
||||
}
|
||||
|
||||
func (p *Player) ScheduleSave() {
|
||||
if p.dirtyTimer != nil {
|
||||
return
|
||||
}
|
||||
|
||||
p.attacher = f5.GetTimer().NewTimerAttacher()
|
||||
p.dirtyTimer = f5.GetTimer().SetTimeoutExWp(
|
||||
10000,
|
||||
func(e int32, args *q5.Args) {
|
||||
if e == q5.TIMER_EXEC_EVENT {
|
||||
p.Lock()
|
||||
defer p.Unlock()
|
||||
if p.dirty {
|
||||
p.SaveToDB() // Persistence, Save TO DB
|
||||
p.dirty = false // 重置标志
|
||||
}
|
||||
}
|
||||
},
|
||||
p.attacher)
|
||||
}
|
||||
|
||||
func (p *Player) SaveToDB() {
|
||||
accountPB := ss.MFAccountData{}
|
||||
p.Serialize(&accountPB)
|
||||
blobData, err := proto.Marshal(&accountPB)
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("SaveToDB proto.Marshal Error:%v \n", err)
|
||||
f5.GetSysLog().Info("SaveToDB proto.Marshal Error:%v accountId:%s \n", err, p.GetAccountId())
|
||||
return
|
||||
}
|
||||
blobDataStr := string(blobData)
|
||||
@ -213,34 +256,8 @@ func (p *Player) SaveToDB() {
|
||||
sql,
|
||||
func(err error, rows *f5.DataSet) {
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("SaveToDB Error:%v\n", err)
|
||||
} else {
|
||||
// 标记p 为已过期
|
||||
p.CacheExpiration = time.Now().Add(-10 * time.Second)
|
||||
f5.GetSysLog().Info("SaveToDB OK\n")
|
||||
f5.GetSysLog().Info("SaveToDB Error:%v accountId:%s\n", err, p.GetAccountId())
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (p *Player) MarkDirty() {
|
||||
// f5.GetApp().RegisterMainThreadCb(func() {})
|
||||
timer := f5.GetTimer()
|
||||
timer.SetTimeout(
|
||||
1000*10,
|
||||
func(e int32, args *q5.Args) {
|
||||
if e == q5.TIMER_EXEC_EVENT {
|
||||
p.SaveToDB()
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func NewPlayer(accountId string) *Player {
|
||||
return &Player{
|
||||
AccountId: accountId,
|
||||
ReadMailHash: make(map[int64]*ReadMail),
|
||||
DeletedMailHash: make(map[int64]*DeletedMail),
|
||||
CacheExpiration: time.Now().Add(20 * time.Second),
|
||||
}
|
||||
}
|
||||
|
@ -28,8 +28,7 @@ func (pm *PlayerMgr) GetPlayer(accountId string) *Player {
|
||||
|
||||
func (pm *PlayerMgr) AsyncGetPlayer(accountId string) *Player {
|
||||
p := pm.GetPlayer(accountId)
|
||||
if p != nil && !p.IsCacheExpired() {
|
||||
// f5.GetSysLog().Info("GetPlayer(%s) from cache", accountId)
|
||||
if p != nil && p.IsCacheNotExpired() {
|
||||
return p
|
||||
}
|
||||
|
||||
@ -47,5 +46,6 @@ func (pm *PlayerMgr) AsyncGetPlayer(accountId string) *Player {
|
||||
})
|
||||
}(accountId)
|
||||
wg.Wait()
|
||||
|
||||
return player
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user