189 lines
4.1 KiB
Go
189 lines
4.1 KiB
Go
package cache
|
|
|
|
import (
|
|
"f5"
|
|
"q5"
|
|
"fmt"
|
|
"cs"
|
|
"main/constant"
|
|
)
|
|
|
|
type cacheMgr struct {
|
|
userHash map[string]*userProfile
|
|
}
|
|
|
|
func (this *cacheMgr) Init() {
|
|
this.userHash = make(map[string]*userProfile)
|
|
}
|
|
|
|
func (this *cacheMgr) UnInit() {
|
|
}
|
|
|
|
func (this *cacheMgr) PreLoadUsers(accountIds []string) {
|
|
this.internalGetUsers(accountIds,
|
|
func (int32, string) {
|
|
|
|
})
|
|
}
|
|
|
|
func (this *cacheMgr) internalGetUsers(accountIds []string, cb func(int32, string)) {
|
|
keys := [][]string{}
|
|
for _, accountId := range(accountIds) {
|
|
if this.getUser(accountId) == nil {
|
|
q5.AppendSlice(&keys, []string{constant.CACHE_LOCK_KEY, accountId})
|
|
}
|
|
}
|
|
if len(keys) <= 0{
|
|
cb(0, "")
|
|
return
|
|
}
|
|
f5.NewLockAsyncTask(
|
|
keys,
|
|
func (task *f5.LockAsyncTask) {
|
|
i := 0
|
|
f5.NewAsyncTask(
|
|
func (subTask* f5.AsyncTask) {
|
|
f5.GetJsStyleDb().OrmSelect(
|
|
constant.GAME_DB,
|
|
"t_user",
|
|
[][]string{
|
|
{"account_id", keys[i][1]},
|
|
},
|
|
func (err error, ds *f5.DataSet) {
|
|
if err != nil {
|
|
subTask.SetFail()
|
|
return
|
|
}
|
|
for ds.Next() {
|
|
accountId := ds.GetByName("account_id")
|
|
u := this.getUser(accountId)
|
|
if u == nil {
|
|
u = newUserProfile()
|
|
}
|
|
u.loadFromDb(ds)
|
|
this.userHash[u.accountId] = u
|
|
}
|
|
if q5.IsDebug() && ds.NumOfReaded() <= 0 {
|
|
f5.GetSysLog().Warning("cacheMgr.internalGetUsers %s not found", keys[i][1])
|
|
}
|
|
if i + 1 < len(keys) {
|
|
i++
|
|
subTask.Continue()
|
|
} else {
|
|
subTask.SetSucc()
|
|
}
|
|
})
|
|
}).OnSucc(
|
|
func (subTask* f5.AsyncTask) {
|
|
task.SetSucc()
|
|
}).OnFail(
|
|
func (subTask* f5.AsyncTask) {
|
|
task.SetFail()
|
|
})
|
|
}).OnSucc(
|
|
func (task* f5.LockAsyncTask) {
|
|
cb(0, "")
|
|
}).OnFail(
|
|
func (task* f5.LockAsyncTask) {
|
|
cb(1, "")
|
|
})
|
|
}
|
|
|
|
func (this* cacheMgr) AsyncGetUsersAndFillMFUser(accountIds []string, pbUsers *[]*cs.MFUser,
|
|
cb func(int32, string)) {
|
|
this.internalGetUsers(
|
|
accountIds,
|
|
func (errCode int32, errMsg string) {
|
|
if errCode != 0 {
|
|
cb(errCode, errMsg)
|
|
return
|
|
}
|
|
for _, accountId := range(accountIds) {
|
|
u := this.getUser(accountId)
|
|
if u != nil {
|
|
pbUser := new(cs.MFUser)
|
|
u.fillMFUser(pbUser)
|
|
q5.AppendSlice(pbUsers, pbUser)
|
|
}
|
|
}
|
|
cb(0, "")
|
|
})
|
|
}
|
|
|
|
func (this* cacheMgr) AsyncGetUsersAndFillMFGuildMember(accountIds []string, pbMembers *[]*cs.MFGuildMember,
|
|
cb func(int32, string)) {
|
|
this.internalGetUsers(
|
|
accountIds,
|
|
func (errCode int32, errMsg string) {
|
|
if errCode != 0 {
|
|
cb(errCode, errMsg)
|
|
return
|
|
}
|
|
for _, accountId := range(accountIds) {
|
|
u := this.getUser(accountId)
|
|
if u != nil {
|
|
pbMember := new(cs.MFGuildMember)
|
|
u.fillMFGuildMember(pbMember)
|
|
q5.AppendSlice(pbMembers, pbMember)
|
|
}
|
|
}
|
|
cb(0, "")
|
|
})
|
|
}
|
|
|
|
func (this *cacheMgr) AsyncSearch(sinceId int64, q string, pbUsers *[]*cs.MFUser,
|
|
cb func(int32, string, int64)) {
|
|
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)
|
|
return
|
|
}
|
|
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("cacheMgr.AsyncSearch idx error:%s %s", idx, lastSinceId))
|
|
}
|
|
u := this.getUser(accountId)
|
|
if u == nil {
|
|
u = newUserProfile()
|
|
}
|
|
u.loadFromDb(pg.Rows)
|
|
this.userHash[u.accountId] = u
|
|
*q5.NewSliceElement(&users) = u.accountId
|
|
}
|
|
this.AsyncGetUsersAndFillMFUser(
|
|
users, pbUsers,
|
|
func (errCode int32, errMsg string) {
|
|
if errCode != 0 {
|
|
cb(500, "", lastSinceId)
|
|
return
|
|
}
|
|
cb(0, "", lastSinceId)
|
|
})
|
|
})
|
|
}
|
|
|
|
func (this *cacheMgr) getUser(accountId string) *userProfile {
|
|
if val, ok := this.userHash[accountId]; ok {
|
|
return val
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|