This commit is contained in:
aozhiwei 2024-03-22 16:29:30 +08:00
parent ae7fcc377c
commit cbba27fab3
2 changed files with 16 additions and 147 deletions

View File

@ -1,132 +0,0 @@
package cache
import (
"f5"
"fmt"
"q5"
"time"
"main/constant"
"main/common"
. "main/global"
)
func (cm *CacheMgr) LoadFromDB() {
uniAccountIds := Set{}
uniAccountIds = make(Set)
// Load friendships
{
lastIdx := int64(0)
done := false
for !done {
f5.GetGoStyleDb().SyncSelectCustomQuery(
constant.FRIEND_DB,
fmt.Sprintf("SELECT idx, account1_id, account2_id FROM t_friend_ships WHERE idx > %d limit 1000", lastIdx),
func(err error, rows *f5.DataSet) {
if err != nil {
f5.GetSysLog().Info("LoadFromDB FRIENDSHIP err:%v \n", err)
panic(err)
}
empty := true
for rows.Next() {
empty = false
account1Id := q5.ToString(rows.GetByName("account1_id"))
account2Id := q5.ToString(rows.GetByName("account2_id"))
if !uniAccountIds.Has(account1Id) {
cm.loadUserProfile(account1Id)
}
uniAccountIds.Add(account1Id)
if !uniAccountIds.Has(account2Id) {
cm.loadUserProfile(account2Id)
}
uniAccountIds.Add(account2Id)
lastIdx = q5.ToInt64(rows.GetByName("idx"))
}
if empty {
done = true
}
},
)
}
}
// Load guild members
{
lastIdx := int64(0)
done := false
for !done {
f5.GetGoStyleDb().SyncSelectCustomQuery(
constant.FRIEND_DB,
fmt.Sprintf("SELECT idx, account_id FROM t_guild_members WHERE idx > %d", lastIdx),
func(err error, rows *f5.DataSet) {
if err != nil {
f5.GetSysLog().Info("LoadFromDB GUILD err:%v \n", err)
panic(err)
}
empty := true
for rows.Next() {
empty = false
accountId := q5.ToString(rows.GetByName("account_id"))
if !uniAccountIds.Has(accountId) {
cm.loadUserProfile(accountId)
}
uniAccountIds.Add(accountId)
lastIdx = q5.ToInt64(rows.GetByName("idx"))
}
if empty {
done = true
}
},
)
}
}
uniAccountIds = nil
}
func (cm *CacheMgr) loadUserProfile(accountId string) {
// "account_id", "name", "head_id", "head_frame", "star_num", "rank", "last_login_time"
sql := fmt.Sprintf("select * from t_user where account_id='%s'", accountId)
f5.GetGoStyleDb().SyncSelectCustomQuery(
constant.GAME_DB,
sql,
func(err error, rows *f5.DataSet) {
if err != nil {
f5.GetSysLog().Info("loadUsersProfile err:%v \n", err)
panic(err)
}
for rows.Next() {
aId := q5.ToString(rows.GetByName("account_id"))
onlineStatus := GetPlayerMgr().GetOnlineStatus(accountId)
profile := &common.PlayerProfile{
AccountId: aId,
Username: q5.ToString(rows.GetByName("name")),
Avatar: q5.ToInt32(rows.GetByName("head_id")),
AvatarHead: q5.ToInt32(rows.GetByName("head_frame")),
Star: q5.ToInt32(rows.GetByName("star_num")),
Rank: q5.ToInt32(rows.GetByName("rank")),
LastLoginTime: q5.ToInt32(rows.GetByName("last_login_time")),
OnlineStatus: onlineStatus,
}
cm.AddPlayerProfile(cm.getCacheVersion(), profile)
}
},
)
}
func (cm *CacheMgr) getCacheVersion() int {
today := time.Now()
_, week := today.ISOWeek()
return week
}
func (cm *CacheMgr) GetProfileByAccountId(accountId string, cb func(err error, profile *common.PlayerProfile)) {
cm.loadUserProfile(accountId)
profile := cm.GetPlayerProfile(accountId)
cb(nil, profile)
}

View File

@ -26,19 +26,18 @@ func (s Set) Delete(key string) {
delete(s, key)
}
func (cm *CacheMgr) init() {
cm.cachePlayerProfiles = make(map[string]*common.CachePlayerProfile)
cm.LoadFromDB()
func (this *CacheMgr) init() {
this.cachePlayerProfiles = make(map[string]*common.CachePlayerProfile)
}
func (cm *CacheMgr) UnInit() {
func (this *CacheMgr) UnInit() {
}
func (cm *CacheMgr) SyncGetUsers(accountIds []string, cb func(bool)) {
func (this *CacheMgr) SyncGetUsers(accountIds []string, cb func(bool)) {
}
func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
func (this *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
var wg sync.WaitGroup
successCount := 0
var mu sync.Mutex
@ -46,17 +45,19 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
wg.Add(1)
go func(accountId string) {
defer wg.Done()
cm.cacheMutex.Lock()
cp, exists := cm.cachePlayerProfiles[accountId]
cm.cacheMutex.Unlock()
if exists && cp.Version == cm.getCacheVersion() {
this.cacheMutex.Lock()
/*
cp, exists := this.cachePlayerProfiles[accountId]
this.cacheMutex.Unlock()
if exists && cp.Version == this.getCacheVersion() {
mu.Lock()
successCount++
mu.Unlock()
return
}
cm.loadUserProfile(accountId)
this.loadUserProfile(accountId)
*/
mu.Lock()
successCount++
mu.Unlock()
@ -71,15 +72,15 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
}
}
func (cm *CacheMgr) AddPlayerProfile(version int, playerProfile *common.PlayerProfile) {
cm.cachePlayerProfiles[playerProfile.AccountId] = &common.CachePlayerProfile{
func (this *CacheMgr) AddPlayerProfile(version int, playerProfile *common.PlayerProfile) {
this.cachePlayerProfiles[playerProfile.AccountId] = &common.CachePlayerProfile{
Version: version,
Data: playerProfile,
}
}
func (cm *CacheMgr) GetPlayerProfile(accountId string) *common.PlayerProfile {
if profile, exists := cm.cachePlayerProfiles[accountId]; exists {
func (this *CacheMgr) GetPlayerProfile(accountId string) *common.PlayerProfile {
if profile, exists := this.cachePlayerProfiles[accountId]; exists {
return profile.Data
}
return nil