添加消息部分用户关系及群用户缓存-修复
This commit is contained in:
parent
e3bc77697b
commit
f1534502a2
|
|
@ -236,6 +236,12 @@ type FriendRet struct {
|
||||||
Email string `json:"email"` //邮箱
|
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 {
|
func FindFriends(user_id int) []FriendRet {
|
||||||
var friends []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)
|
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)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
"videoplayer/dao"
|
"videoplayer/dao"
|
||||||
|
|
@ -16,8 +17,9 @@ func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content
|
||||||
switch msg_type {
|
switch msg_type {
|
||||||
case proto.MSG_TYPE_SIMPLE:
|
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)
|
isSuccess := SetFriendCache(from_id)
|
||||||
//设置失败,直接查询数据库
|
//设置失败,直接查询数据库
|
||||||
if !isSuccess {
|
if !isSuccess {
|
||||||
|
|
@ -27,14 +29,14 @@ func CreateGeneralMessageService(from_id, to_id, msg_type, group_id int, content
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//判断是否是好友-redis方式
|
//判断是否是好友-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 {
|
if !is_f {
|
||||||
return errors.New("未添加好友"), 0
|
return errors.New("未添加好友"), 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//判断是否是好友-redis方式
|
//判断是否是好友-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 {
|
if !is_f {
|
||||||
return errors.New("未添加好友"), 0
|
return errors.New("未添加好友"), 0
|
||||||
}
|
}
|
||||||
|
|
@ -301,10 +303,10 @@ func GetGroupRequestUsers(user_id int) []dao.FriendRequest {
|
||||||
// 设置用户朋友关系缓存
|
// 设置用户朋友关系缓存
|
||||||
func SetFriendCache(user_id int) bool {
|
func SetFriendCache(user_id int) bool {
|
||||||
//获取好友id
|
//获取好友id
|
||||||
friends := dao.FindFriends(user_id)
|
friends := dao.FindFriendsIDs(user_id)
|
||||||
var ids []string
|
var ids []string
|
||||||
for _, friend := range friends {
|
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)
|
res := worker.SetRedisSet("user_"+strconv.Itoa(user_id)+"_friends", ids, time.Hour*12)
|
||||||
return res
|
return res
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue