Skip to content

Commit

Permalink
Fix local cache: user cache and group member cache (#765)
Browse files Browse the repository at this point in the history
* fix: group member local cache

* fix: user local cache
  • Loading branch information
icey-yu authored and OpenIM-Robot committed Nov 1, 2024
1 parent 646090c commit b7269fc
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ require (
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions internal/conversation_msg/message_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ func (c *Conversation) groupHandle(ctx context.Context, self, others []*model_st
}
for _, chatLog := range allMessage {
if g, ok := groupMap[chatLog.SendID]; ok { // If group member info is successfully retrieved
log.ZDebug(ctx, "find in GetGroupMemberNameAndFaceURL", "sendID", chatLog.SendID, "faceURL", g.FaceURL, "nickName", g.Nickname)
if g.FaceURL != "" && g.Nickname != "" {
chatLog.SenderFaceURL = g.FaceURL
chatLog.SenderNickname = g.Nickname
Expand All @@ -386,6 +387,7 @@ func (c *Conversation) groupHandle(ctx context.Context, self, others []*model_st
if err != nil {
log.ZWarn(ctx, "getUserNameAndFaceURL error", err, "senderID", chatLog.SendID)
} else if faceURL != "" && name != "" {
log.ZDebug(ctx, "find in getUserNameAndFaceURL", "sendID", chatLog.SendID, "faceURL", faceURL, "nickName", name)
chatLog.SenderFaceURL = faceURL
chatLog.SenderNickname = name
}
Expand Down
4 changes: 3 additions & 1 deletion internal/group/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package group
import (
"context"
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
"github.com/openimsdk/tools/log"
"github.com/openimsdk/tools/utils/datautil"
)

Expand All @@ -20,13 +21,14 @@ func (g *Group) GetGroupMembersInfoFunc(ctx context.Context, groupID string, use

for _, userID := range userIDs {
key := g.buildGroupMemberKey(groupID, userID)
if member, ok := g.groupMemberCache.Load(groupID); ok {
if member, ok := g.groupMemberCache.Load(key); ok {
res[key] = member
} else {
missingKeys = append(missingKeys, userIDs...)
}
}

log.ZDebug(ctx, "GetGroupMembersInfoFunc fetch", "missingKeys", missingKeys)
fetchData, err := fetchFunc(ctx, missingKeys)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions internal/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (g *Group) initSyncer() {
return g.db.DeleteGroupMember(ctx, value.GroupID, value.UserID)
}),
syncer.WithUpdate[*model_struct.LocalGroupMember, group.GetGroupMemberListResp, [2]string](func(ctx context.Context, server, local *model_struct.LocalGroupMember) error {
g.groupMemberCache.Delete(g.buildGroupMemberKey(server.GroupID, server.UserID))
return g.db.UpdateGroupMember(ctx, server)
}),
syncer.WithUUID[*model_struct.LocalGroupMember, group.GetGroupMemberListResp, [2]string](func(value *model_struct.LocalGroupMember) [2]string {
Expand Down
5 changes: 3 additions & 2 deletions internal/relation/relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Relation struct {
db db_interface.DataBase
user *user.User
friendSyncer *syncer.Syncer[*model_struct.LocalFriend, relation.GetPaginationFriendsResp, [2]string]
blockSyncer *syncer.Syncer[*model_struct.LocalBlack, syncer.NoResp, [2]string]
blackSyncer *syncer.Syncer[*model_struct.LocalBlack, syncer.NoResp, [2]string]
requestRecvSyncer *syncer.Syncer[*model_struct.LocalFriendRequest, syncer.NoResp, [2]string]
requestSendSyncer *syncer.Syncer[*model_struct.LocalFriendRequest, syncer.NoResp, [2]string]
conversationCh chan common.Cmd2Value
Expand All @@ -53,6 +53,7 @@ func (r *Relation) initSyncer() {
return r.db.DeleteFriendDB(ctx, value.FriendUserID)
}),
syncer.WithUpdate[*model_struct.LocalFriend, relation.GetPaginationFriendsResp, [2]string](func(ctx context.Context, server, local *model_struct.LocalFriend) error {
r.user.UserCache.Delete(server.FriendUserID)
return r.db.UpdateFriend(ctx, server)
}),
syncer.WithUUID[*model_struct.LocalFriend, relation.GetPaginationFriendsResp, [2]string](func(value *model_struct.LocalFriend) [2]string {
Expand Down Expand Up @@ -132,7 +133,7 @@ func (r *Relation) initSyncer() {
syncer.WithFullSyncLimit[*model_struct.LocalFriend, relation.GetPaginationFriendsResp, [2]string](friendSyncLimit),
)

r.blockSyncer = syncer.New[*model_struct.LocalBlack, syncer.NoResp, [2]string](func(ctx context.Context, value *model_struct.LocalBlack) error {
r.blackSyncer = syncer.New[*model_struct.LocalBlack, syncer.NoResp, [2]string](func(ctx context.Context, value *model_struct.LocalBlack) error {
return r.db.InsertBlack(ctx, value)
}, func(ctx context.Context, value *model_struct.LocalBlack) error {
return r.db.DeleteBlack(ctx, value.BlockUserID)
Expand Down
4 changes: 2 additions & 2 deletions internal/relation/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (r *Relation) SyncAllBlackList(ctx context.Context) error {
return err
}
log.ZDebug(ctx, "black from local", "data", localData)
return r.blockSyncer.Sync(ctx, datautil.Batch(ServerBlackToLocalBlack, serverData), localData, nil)
return r.blackSyncer.Sync(ctx, datautil.Batch(ServerBlackToLocalBlack, serverData), localData, nil)
}

func (r *Relation) SyncAllBlackListWithoutNotice(ctx context.Context) error {
Expand All @@ -118,7 +118,7 @@ func (r *Relation) SyncAllBlackListWithoutNotice(ctx context.Context) error {
return err
}
log.ZDebug(ctx, "black from local", "data", localData)
return r.blockSyncer.Sync(ctx, datautil.Batch(ServerBlackToLocalBlack, serverData), localData, nil, false, true)
return r.blackSyncer.Sync(ctx, datautil.Batch(ServerBlackToLocalBlack, serverData), localData, nil, false, true)
}

func (r *Relation) GetDesignatedFriends(ctx context.Context, friendIDs []string) ([]*sdkws.FriendInfo, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (u *User) initSyncer() {
return fmt.Errorf("not support delete user %s", value.UserID)
},
func(ctx context.Context, serverUser, localUser *model_struct.LocalUser) error {
u.UserCache.Delete(localUser.UserID)
return u.DataBase.UpdateLoginUser(context.Background(), serverUser)
},
func(user *model_struct.LocalUser) string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/group_member_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (d *DataBase) GetGroupSomeMemberInfo(ctx context.Context, groupID string, u
d.mRWMutex.RLock()
defer d.mRWMutex.RUnlock()
var groupMemberList []*model_struct.LocalGroupMember
err := d.conn.WithContext(ctx).Where("group_id = ? And user_id IN ? ", groupID, userIDList).Find(&groupMemberList).Error
err := d.conn.WithContext(ctx).Where("group_id = ? AND user_id IN ? ", groupID, userIDList).Find(&groupMemberList).Error
return groupMemberList, errs.WrapMsg(err, "GetGroupMemberListByGroupID failed ")
}

Expand Down
20 changes: 20 additions & 0 deletions test/long_conn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test

import (
"github.com/openimsdk/openim-sdk-core/v3/open_im_sdk"
"testing"
"time"
)

func Test_SubscribeUsersStatus(t *testing.T) {
time.Sleep(time.Second)
message, err := open_im_sdk.UserForSDK.LongConnMgr().SubscribeUsersStatus(ctx, []string{"5975996883"})
if err != nil {
t.Error(err)
}
t.Log(message)
ch := make(chan struct{})
select {
case <-ch:
}
}

0 comments on commit b7269fc

Please sign in to comment.