1
This commit is contained in:
parent
ae7fcc377c
commit
cbba27fab3
132
server/imserver_new/cache/cachedbmgr.go
vendored
132
server/imserver_new/cache/cachedbmgr.go
vendored
@ -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)
|
|
||||||
}
|
|
31
server/imserver_new/cache/cachemgr.go
vendored
31
server/imserver_new/cache/cachemgr.go
vendored
@ -26,19 +26,18 @@ func (s Set) Delete(key string) {
|
|||||||
delete(s, key)
|
delete(s, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *CacheMgr) init() {
|
func (this *CacheMgr) init() {
|
||||||
cm.cachePlayerProfiles = make(map[string]*common.CachePlayerProfile)
|
this.cachePlayerProfiles = make(map[string]*common.CachePlayerProfile)
|
||||||
cm.LoadFromDB()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
var wg sync.WaitGroup
|
||||||
successCount := 0
|
successCount := 0
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
@ -46,17 +45,19 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(accountId string) {
|
go func(accountId string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
cm.cacheMutex.Lock()
|
this.cacheMutex.Lock()
|
||||||
cp, exists := cm.cachePlayerProfiles[accountId]
|
/*
|
||||||
cm.cacheMutex.Unlock()
|
cp, exists := this.cachePlayerProfiles[accountId]
|
||||||
if exists && cp.Version == cm.getCacheVersion() {
|
this.cacheMutex.Unlock()
|
||||||
|
if exists && cp.Version == this.getCacheVersion() {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
successCount++
|
successCount++
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cm.loadUserProfile(accountId)
|
this.loadUserProfile(accountId)
|
||||||
|
*/
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
successCount++
|
successCount++
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
@ -71,15 +72,15 @@ func (cm *CacheMgr) AsyncGetUsers(accountIds []string, cb func(bool)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *CacheMgr) AddPlayerProfile(version int, playerProfile *common.PlayerProfile) {
|
func (this *CacheMgr) AddPlayerProfile(version int, playerProfile *common.PlayerProfile) {
|
||||||
cm.cachePlayerProfiles[playerProfile.AccountId] = &common.CachePlayerProfile{
|
this.cachePlayerProfiles[playerProfile.AccountId] = &common.CachePlayerProfile{
|
||||||
Version: version,
|
Version: version,
|
||||||
Data: playerProfile,
|
Data: playerProfile,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *CacheMgr) GetPlayerProfile(accountId string) *common.PlayerProfile {
|
func (this *CacheMgr) GetPlayerProfile(accountId string) *common.PlayerProfile {
|
||||||
if profile, exists := cm.cachePlayerProfiles[accountId]; exists {
|
if profile, exists := this.cachePlayerProfiles[accountId]; exists {
|
||||||
return profile.Data
|
return profile.Data
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user