From f1534502a22cc2d64fabfbdd8d63bb5cd810332a Mon Sep 17 00:00:00 2001 From: junleea <354425203@qq.com> Date: Sat, 21 Sep 2024 13:27:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B6=88=E6=81=AF=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=94=A8=E6=88=B7=E5=85=B3=E7=B3=BB=E5=8F=8A=E7=BE=A4?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=BC=93=E5=AD=98-=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/im.go | 6 ++++++ service/imService.go | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/dao/im.go b/dao/im.go index 81ff6c0..d06b52d 100644 --- a/dao/im.go +++ b/dao/im.go @@ -236,6 +236,12 @@ type FriendRet struct { Email string `json:"email"` //邮箱 } +func FindFriendsIDs(user_id int) []Friend { + var friends []Friend + DB.Debug().Where("user_id = ?", user_id).Find(&friends) + return friends +} + func FindFriends(user_id int) []FriendRet { var friends []FriendRet DB.Debug().Raw("select users.id, users.name, users.email from users join friends on users.id = friends.friend_id where friends.user_id = ? and friends.deleted_at is null", user_id).Scan(&friends) diff --git a/service/imService.go b/service/imService.go index 5f0e93e..71c21d0 100644 --- a/service/imService.go +++ b/service/imService.go @@ -2,6 +2,7 @@ package service import ( "errors" + "fmt" "strconv" "time" "videoplayer/dao" @@ -16,8 +17,9 @@ func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content switch msg_type { case proto.MSG_TYPE_SIMPLE: //判断是否是好友,判断是否存在缓存,不存在则设置缓存,存在则判断是否是好友 - if worker.IsContainKey("user_"+strconv.Itoa(to_id)+"_friends") == false { + if worker.IsContainKey("user_"+strconv.Itoa(from_id)+"_friends") == false { //设置好友缓存 + fmt.Println("设置好友缓存:", from_id, to_id) isSuccess := SetFriendCache(from_id) //设置失败,直接查询数据库 if !isSuccess { @@ -27,14 +29,14 @@ func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content } } else { //判断是否是好友-redis方式 - is_f := worker.IsContainSet("user_"+strconv.Itoa(to_id)+"_friends", strconv.Itoa(from_id)) + is_f := worker.IsContainSet("user_"+strconv.Itoa(from_id)+"_friends", strconv.Itoa(to_id)) if !is_f { return errors.New("未添加好友"), 0 } } } else { //判断是否是好友-redis方式 - is_f := worker.IsContainSet("user_"+strconv.Itoa(to_id)+"_friends", strconv.Itoa(from_id)) + is_f := worker.IsContainSet("user_"+strconv.Itoa(from_id)+"_friends", strconv.Itoa(to_id)) if !is_f { return errors.New("未添加好友"), 0 } @@ -301,10 +303,10 @@ func GetGroupRequestUsers(user_id int) []dao.FriendRequest { // 设置用户朋友关系缓存 func SetFriendCache(user_id int) bool { //获取好友id - friends := dao.FindFriends(user_id) + friends := dao.FindFriendsIDs(user_id) var ids []string for _, friend := range friends { - ids = append(ids, strconv.Itoa(friend.ID)) + ids = append(ids, strconv.Itoa(friend.FriendID)) } res := worker.SetRedisSet("user_"+strconv.Itoa(user_id)+"_friends", ids, time.Hour*12) return res