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