From b273f36ed28ef3f47c38d237b6d3eca23d4a6538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Mon, 25 Sep 2023 16:26:04 +0800 Subject: [PATCH] save --- server/imserver/cachedbmgr.go | 70 ++++++++++++++++++----------------- server/imserver/cachemgr.go | 15 ++++++++ 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/server/imserver/cachedbmgr.go b/server/imserver/cachedbmgr.go index 0c9ccc5e..c63819df 100644 --- a/server/imserver/cachedbmgr.go +++ b/server/imserver/cachedbmgr.go @@ -3,10 +3,16 @@ package main import ( "f5" "fmt" + "mt" "q5" ) +var tableNames = make(map[string]string) + func (cm *CacheMgr) LoadFromDB() { + tableNames["user"] = fmt.Sprintf("%s.t_user", mt.Table.GameDb.GetById(0).GetDatabase()) + tableNames["friendships"] = fmt.Sprintf("%s.t_friend_ships", mt.Table.FriendDb.GetById(0).GetDatabase()) + tableNames["guild_member"] = fmt.Sprintf("%s.t_guild_members", mt.Table.FriendDb.GetById(0).GetDatabase()) // 加载所有好友信息 cm.loadAllFriendUserProfile() // 加载所有工会成员信息 @@ -14,21 +20,45 @@ func (cm *CacheMgr) LoadFromDB() { } // TODO 重加载数据,1000 迭代 -var userTable = "gamedb2006_dev_1.t_user" -var friendShipsTable = "frienddb_dev_1.t_friend_ships" -var guildMembersTable = "frienddb_dev_1.t_guild_members" - func (cm *CacheMgr) loadAllFriendUserProfile() { - sql := fmt.Sprintf("SELECT account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM ( SELECT account1_id AS account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM %s AS fs JOIN %s AS u ON fs.account1_id = u.account_id UNION SELECT account2_id AS account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM %s AS fs JOIN %s AS u ON fs.account2_id = u.account_id) AS friend_info_table;", friendShipsTable, userTable, friendShipsTable, userTable) + sql := fmt.Sprintf("SELECT account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM ( SELECT account1_id AS account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM %s AS fs JOIN %s AS u ON fs.account1_id = u.account_id UNION SELECT account2_id AS account_id, name, head_id, head_frame, star_num, rank, last_login_time FROM %s AS fs JOIN %s AS u ON fs.account2_id = u.account_id) AS friend_info_table;", tableNames["friendships"], tableNames["user"], tableNames["friendships"], tableNames["user"]) cm.loadUsersProfile(sql) } // loadGuildFromDB 加载公会成员信息 func (cm *CacheMgr) loadAllGuildUserProfile() { - sql := fmt.Sprintf("select a.account_id, a.name, a.head_id, a.head_frame, a.star_num, a.`rank`, a.last_login_time from %s a,%s b where a.account_id = b.account_id", userTable, guildMembersTable) + sql := fmt.Sprintf("select a.account_id, a.name, a.head_id, a.head_frame, a.star_num, a.`rank`, a.last_login_time from %s a,%s b where a.account_id = b.account_id", tableNames["user"], tableNames["guild_member"]) cm.loadUsersProfile(sql) } +func (cm *CacheMgr) loadUsersProfile(sql string) { + f5.GetJsStyleDb().SelectCustomQuery( + FRIEND_DB, + sql, + func(err error, rows *f5.DataSet) { + if err != nil { + f5.GetSysLog().Info("loadUsersProfile err:%v \n", err) + return + } + for rows.Next() { + accountId := q5.ToString(*rows.GetByIndex(0)) + onlineStatus := playerMgr.GetOnlineStatus(accountId) + profile := &PlayerProfile{ + AccountId: accountId, + Username: q5.ToString(*rows.GetByIndex(1)), + Avatar: q5.ToInt32(*rows.GetByIndex(2)), + AvatarHead: q5.ToInt32(*rows.GetByIndex(3)), + Star: q5.ToInt32(*rows.GetByIndex(4)), + Rank: q5.ToInt32(*rows.GetByIndex(5)), + LastLoginTime: q5.ToInt32(*rows.GetByIndex(6)), + OnlineStatus: onlineStatus, + } + cm.AddCacheProfile(1, profile) + } + }, + ) +} + func (cm *CacheMgr) GetProfileByAccountId(accountId string, cb func(err error, profile *PlayerProfile)) { fields := []string{"account_id", "name", "head_id", "head_frame", "star_num", "rank", "last_login_time"} where := [][]string{ @@ -63,31 +93,3 @@ func (cm *CacheMgr) GetProfileByAccountId(accountId string, cb func(err error, p }, ) } - -func (cm *CacheMgr) loadUsersProfile(sql string) { - f5.GetJsStyleDb().SelectCustomQuery( - FRIEND_DB, - sql, - func(err error, rows *f5.DataSet) { - if err != nil { - f5.GetSysLog().Info("loadUsersProfile err:%v \n", err) - return - } - for rows.Next() { - accountId := q5.ToString(*rows.GetByIndex(0)) - onlineStatus := playerMgr.GetOnlineStatus(accountId) - profile := &PlayerProfile{ - AccountId: accountId, - Username: q5.ToString(*rows.GetByIndex(1)), - Avatar: q5.ToInt32(*rows.GetByIndex(2)), - AvatarHead: q5.ToInt32(*rows.GetByIndex(3)), - Star: q5.ToInt32(*rows.GetByIndex(4)), - Rank: q5.ToInt32(*rows.GetByIndex(5)), - LastLoginTime: q5.ToInt32(*rows.GetByIndex(6)), - OnlineStatus: onlineStatus, - } - cm.AddCacheProfile(1, profile) - } - }, - ) -} diff --git a/server/imserver/cachemgr.go b/server/imserver/cachemgr.go index 4d7db40d..40679daf 100644 --- a/server/imserver/cachemgr.go +++ b/server/imserver/cachemgr.go @@ -28,6 +28,21 @@ type CacheMgr struct { cachePlayerProfiles map[string]*CachePlayerProfile } +type Set map[string]struct{} + +func (s Set) Has(key string) bool { + _, ok := s[key] + return ok +} + +func (s Set) Add(key string) { + s[key] = struct{}{} +} + +func (s Set) Delete(key string) { + delete(s, key) +} + func (cm *CacheMgr) init() { cm.cachePlayerProfiles = make(map[string]*CachePlayerProfile) cm.LoadFromDB()