pull/232/head
wangchuxiao 3 years ago
parent 8240cb9c0f
commit 0460033281

@ -11,11 +11,11 @@ import (
commonPb "Open_IM/pkg/proto/sdk_ws" commonPb "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils" "Open_IM/pkg/utils"
"context" "context"
"google.golang.org/grpc"
"net" "net"
"strconv" "strconv"
"strings" "strings"
"sync"
"google.golang.org/grpc"
) )
type cacheServer struct { type cacheServer struct {
@ -103,7 +103,11 @@ func SyncDB2Cache() error {
func DelRelationCache() {} func DelRelationCache() {}
func updateAllUserToCache(userList []db.User) error { func updateAllUserToCache(userList []db.User) error {
wg := &sync.WaitGroup{}
wg.Add(len(userList))
for _, userInfo := range userList { for _, userInfo := range userList {
go func() {
defer wg.Done()
userInfoPb := &commonPb.UserInfo{ userInfoPb := &commonPb.UserInfo{
UserID: userInfo.UserID, UserID: userInfo.UserID,
Nickname: userInfo.Nickname, Nickname: userInfo.Nickname,
@ -123,7 +127,9 @@ func updateAllUserToCache(userList []db.User) error {
if err := db.DB.SetUserInfoToCache(userInfo.UserID, m); err != nil { if err := db.DB.SetUserInfoToCache(userInfo.UserID, m); err != nil {
log.NewWarn("0", utils.GetSelfFuncName(), "set userInfo to cache failed", err.Error()) log.NewWarn("0", utils.GetSelfFuncName(), "set userInfo to cache failed", err.Error())
} }
}()
} }
wg.Wait()
log.NewInfo("0", utils.GetSelfFuncName(), "ok") log.NewInfo("0", utils.GetSelfFuncName(), "ok")
return nil return nil
} }
@ -135,11 +141,15 @@ func updateAllGroupMemberListToCache() error {
log.NewWarn("0", utils.GetSelfFuncName(), "getAllGroupIDList failed", err.Error()) log.NewWarn("0", utils.GetSelfFuncName(), "getAllGroupIDList failed", err.Error())
panic(err.Error()) panic(err.Error())
} }
wg := &sync.WaitGroup{}
wg.Add(len(groupIDList))
for _, groupID := range groupIDList { for _, groupID := range groupIDList {
go func() {
defer wg.Done()
groupMemberIDList, err := imdb.GetGroupMemberIDListByGroupID(groupID) groupMemberIDList, err := imdb.GetGroupMemberIDListByGroupID(groupID)
if err != nil { if err != nil {
log.NewWarn("", utils.GetSelfFuncName(), "GetGroupMemberIDListByGroupID", err.Error()) log.NewWarn("", utils.GetSelfFuncName(), "GetGroupMemberIDListByGroupID", err.Error())
continue return
} }
//log.NewDebug("", utils.GetSelfFuncName(), "groupMemberIDList", groupMemberIDList) //log.NewDebug("", utils.GetSelfFuncName(), "groupMemberIDList", groupMemberIDList)
if len(groupMemberIDList) > 0 { if len(groupMemberIDList) > 0 {
@ -147,43 +157,57 @@ func updateAllGroupMemberListToCache() error {
log.NewWarn("", utils.GetSelfFuncName(), "AddGroupMemberToCache", err.Error()) log.NewWarn("", utils.GetSelfFuncName(), "AddGroupMemberToCache", err.Error())
} }
} }
}()
} }
wg.Wait()
log.NewInfo("0", utils.GetSelfFuncName(), "ok") log.NewInfo("0", utils.GetSelfFuncName(), "ok")
return nil return nil
} }
func updateAllFriendToCache(userList []db.User) error { func updateAllFriendToCache(userList []db.User) error {
log.NewInfo("0", utils.GetSelfFuncName()) log.NewInfo("0", utils.GetSelfFuncName())
wg := sync.WaitGroup{}
wg.Add(len(userList))
for _, user := range userList { for _, user := range userList {
go func() {
defer wg.Done()
friendIDList, err := imdb.GetFriendIDListByUserID(user.UserID) friendIDList, err := imdb.GetFriendIDListByUserID(user.UserID)
if err != nil { if err != nil {
log.NewWarn("0", utils.GetSelfFuncName(), err.Error()) log.NewWarn("0", utils.GetSelfFuncName(), err.Error())
continue return
} }
if len(friendIDList) > 0 { if len(friendIDList) > 0 {
if err := db.DB.AddFriendToCache(user.UserID, friendIDList...); err != nil { if err := db.DB.AddFriendToCache(user.UserID, friendIDList...); err != nil {
log.NewWarn("0", utils.GetSelfFuncName(), err.Error(), friendIDList, user.UserID) log.NewWarn("0", utils.GetSelfFuncName(), err.Error(), friendIDList, user.UserID)
} }
} }
}()
} }
wg.Wait()
log.NewInfo("0", utils.GetSelfFuncName(), "ok") log.NewInfo("0", utils.GetSelfFuncName(), "ok")
return nil return nil
} }
func updateAllBlackListToCache(userList []db.User) error { func updateAllBlackListToCache(userList []db.User) error {
log.NewInfo("0", utils.GetSelfFuncName()) log.NewInfo("0", utils.GetSelfFuncName())
wg := &sync.WaitGroup{}
wg.Add(len(userList))
for _, user := range userList { for _, user := range userList {
go func() {
defer wg.Done()
blackIDList, err := imdb.GetBlackIDListByUserID(user.UserID) blackIDList, err := imdb.GetBlackIDListByUserID(user.UserID)
if err != nil { if err != nil {
log.NewWarn("", utils.GetSelfFuncName(), err.Error()) log.NewWarn("", utils.GetSelfFuncName(), err.Error())
continue return
} }
if len(blackIDList) > 0 { if len(blackIDList) > 0 {
if err := db.DB.AddBlackUserToCache(user.UserID, blackIDList...); err != nil { if err := db.DB.AddBlackUserToCache(user.UserID, blackIDList...); err != nil {
log.NewWarn("0", utils.GetSelfFuncName(), err.Error()) log.NewWarn("0", utils.GetSelfFuncName(), err.Error())
} }
} }
}()
} }
wg.Wait()
log.NewInfo("0", utils.GetSelfFuncName(), "ok") log.NewInfo("0", utils.GetSelfFuncName(), "ok")
return nil return nil
} }

Loading…
Cancel
Save