aozhiwei da2f078f18 1
2024-04-07 15:05:04 +08:00

103 lines
2.3 KiB
Go

package cache
import (
"f5"
"q5"
"fmt"
"main/common"
"main/constant"
)
type pendingUser struct {
accountId string
reqTime int64
}
type cacheMgr struct {
userHash map[string]*userProfile
pendingHash map[string]*pendingUser
}
func (this *cacheMgr) Init() {
this.userHash = make(map[string]*userProfile)
this.pendingHash = make(map[string]*pendingUser)
}
func (this *cacheMgr) UnInit() {
}
func (this *cacheMgr) AsyncGetUsers(accountIds []string, cb func(int32, string)) {
this.internalGetUsers(accountIds, cb)
}
func (this *cacheMgr) GetUserProfile(accountId string) common.UserProfile {
if user, ok := this.userHash[accountId]; ok {
return user
}
return nil
}
func (this *cacheMgr) PreLoadUsers(accountIds []string) {
this.internalGetUsers(accountIds,
func (int32, string) {
})
}
func (this *cacheMgr) AsyncSearch(sinceId int64, q string,
cb func(int32, string, int64, []string)) {
f5.GetJsStyleDb().PageQuery(
constant.GAME_DB,
50,
0,
"SELECT * FROM t_user WHERE 1=1",
[]string{},
f5.GetDbFilter().Comp(
f5.GetDbFilter().GT("idx", q5.ToString(sinceId)).And(),
f5.GetDbFilter().Like("name", q5.ToString(q)).And(),
),
"",
func (err error, pg *f5.Pagination) {
var lastSinceId int64 = sinceId
if err != nil {
cb(500, "", lastSinceId, []string{})
}
users := []string{}
for pg.Rows.Next() {
idx := q5.ToInt64(pg.Rows.GetByName("idx"))
accountId := pg.Rows.GetByName("account_id")
if idx > lastSinceId {
lastSinceId = idx
} else {
panic(fmt.Sprintf("AsyncGetApply idx error:%s %s", idx, lastSinceId))
}
u := this.getUser(accountId)
if u == nil {
u = newUserProfile()
}
u.accountId = pg.Rows.GetByName("account_id")
u.name = pg.Rows.GetByName("name")
u.avatarUrl = pg.Rows.GetByName("avatar")
u.head = pg.Rows.GetByName("head")
u.lastLoginTime = q5.ToInt32(pg.Rows.GetByName("last_login_time"))
u.lastSyncTime = f5.GetApp().GetNowMillis()
this.userHash[u.accountId] = u
*q5.NewSliceElement(&users) = u.accountId
}
cb(0, "", lastSinceId, users)
})
}
func (this *cacheMgr) internalGetUsers(accountIds []string, cb func(int32, string)) {
}
func (this *cacheMgr) getUser(accountId string) *userProfile {
if val, ok := this.userHash[accountId]; ok {
return val
} else {
return nil
}
}