Files
open-im-server/pkg/common/db/rocks_cache/rocks_cache.go
T

690 lines
23 KiB
Go
Raw Normal View History

2022-07-14 12:08:28 +08:00
package rocksCache
import (
2022-07-29 14:36:07 +08:00
"Open_IM/pkg/common/constant"
2022-07-14 12:08:28 +08:00
"Open_IM/pkg/common/db"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
2022-07-26 15:16:46 +08:00
"Open_IM/pkg/common/log"
2023-01-09 14:27:08 +08:00
"Open_IM/pkg/common/trace_log"
2022-07-20 15:49:21 +08:00
"Open_IM/pkg/utils"
2022-07-20 11:35:19 +08:00
"context"
2022-07-14 12:08:28 +08:00
"encoding/json"
2022-07-20 11:35:19 +08:00
"fmt"
2022-08-08 11:30:10 +08:00
"math/big"
"sort"
"strconv"
2022-07-14 12:08:28 +08:00
"time"
)
const (
2022-07-29 14:36:07 +08:00
userInfoCache = "USER_INFO_CACHE:"
friendRelationCache = "FRIEND_RELATION_CACHE:"
blackListCache = "BLACK_LIST_CACHE:"
groupCache = "GROUP_CACHE:"
groupInfoCache = "GROUP_INFO_CACHE:"
groupOwnerIDCache = "GROUP_OWNER_ID:"
joinedGroupListCache = "JOINED_GROUP_LIST_CACHE:"
groupMemberInfoCache = "GROUP_MEMBER_INFO_CACHE:"
groupAllMemberInfoCache = "GROUP_ALL_MEMBER_INFO_CACHE:"
allFriendInfoCache = "ALL_FRIEND_INFO_CACHE:"
allDepartmentCache = "ALL_DEPARTMENT_CACHE:"
allDepartmentMemberCache = "ALL_DEPARTMENT_MEMBER_CACHE:"
joinedSuperGroupListCache = "JOINED_SUPER_GROUP_LIST_CACHE:"
2022-08-08 11:30:10 +08:00
groupMemberListHashCache = "GROUP_MEMBER_LIST_HASH_CACHE:"
groupMemberNumCache = "GROUP_MEMBER_NUM_CACHE:"
2022-08-19 12:09:05 +08:00
conversationCache = "CONVERSATION_CACHE:"
conversationIDListCache = "CONVERSATION_ID_LIST_CACHE:"
2022-12-01 19:34:54 +08:00
extendMsgSetCache = "EXTEND_MSG_SET_CACHE:"
extendMsgCache = "EXTEND_MSG_CACHE:"
2022-07-14 12:08:28 +08:00
)
2022-09-01 21:18:33 +08:00
func DelKeys() {
2022-07-20 11:35:19 +08:00
fmt.Println("init to del old keys")
for _, key := range []string{groupCache, friendRelationCache, blackListCache, userInfoCache, groupInfoCache, groupOwnerIDCache, joinedGroupListCache,
2022-07-21 11:03:02 +08:00
groupMemberInfoCache, groupAllMemberInfoCache, allFriendInfoCache} {
2022-07-26 15:16:46 +08:00
fName := utils.GetSelfFuncName()
2022-07-20 11:35:19 +08:00
var cursor uint64
var n int
for {
var keys []string
var err error
keys, cursor, err = db.DB.RDB.Scan(context.Background(), cursor, key+"*", 3000).Result()
if err != nil {
panic(err.Error())
}
n += len(keys)
2022-07-29 14:36:07 +08:00
// for each for redis cluster
2022-07-26 15:16:46 +08:00
for _, key := range keys {
if err = db.DB.RDB.Del(context.Background(), key).Err(); err != nil {
log.NewError("", fName, key, err.Error())
err = db.DB.RDB.Del(context.Background(), key).Err()
if err != nil {
panic(err.Error())
}
2022-07-20 12:08:58 +08:00
}
2022-07-20 12:05:57 +08:00
}
2022-07-20 11:35:19 +08:00
if cursor == 0 {
break
}
}
}
}
2023-01-09 14:27:08 +08:00
func GetFriendIDListFromCache(ctx context.Context, userID string) (friendIDList []string, err error) {
2022-07-14 12:08:28 +08:00
getFriendIDList := func() (string, error) {
friendIDList, err := imdb.GetFriendIDListByUserID(userID)
2022-07-20 15:49:21 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(friendIDList)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID, "friendIDList", friendIDList)
}()
2022-07-20 11:35:19 +08:00
friendIDListStr, err := db.DB.Rc.Fetch(friendRelationCache+userID, time.Second*30*60, getFriendIDList)
2022-07-20 15:49:21 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
2022-07-20 16:07:30 +08:00
err = json.Unmarshal([]byte(friendIDListStr), &friendIDList)
2022-07-20 15:49:21 +08:00
return friendIDList, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func DelFriendIDListFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
return db.DB.Rc.TagAsDeleted(friendRelationCache + userID)
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func GetBlackListFromCache(ctx context.Context, userID string) (blackIDList []string, err error) {
2022-07-14 12:08:28 +08:00
getBlackIDList := func() (string, error) {
blackIDList, err := imdb.GetBlackIDListByUserID(userID)
2022-07-20 15:49:21 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(blackIDList)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID, "blackIDList", blackIDList)
}()
2022-07-20 11:35:19 +08:00
blackIDListStr, err := db.DB.Rc.Fetch(blackListCache+userID, time.Second*30*60, getBlackIDList)
2022-07-20 15:49:21 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
2022-07-20 16:07:30 +08:00
err = json.Unmarshal([]byte(blackIDListStr), &blackIDList)
2022-07-20 15:49:21 +08:00
return blackIDList, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 16:37:33 +08:00
func DelBlackIDListFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "ctx", ctx)
}()
2022-07-14 12:08:28 +08:00
return db.DB.Rc.TagAsDeleted(blackListCache + userID)
}
2023-01-09 14:27:08 +08:00
func GetJoinedGroupIDListFromCache(ctx context.Context, userID string) (joinedGroupList []string, err error) {
2022-07-14 12:08:28 +08:00
getJoinedGroupIDList := func() (string, error) {
joinedGroupList, err := imdb.GetJoinedGroupIDListByUserID(userID)
2022-07-20 15:49:21 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(joinedGroupList)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID, "joinedGroupList", joinedGroupList)
}()
2022-07-20 11:35:19 +08:00
joinedGroupIDListStr, err := db.DB.Rc.Fetch(joinedGroupListCache+userID, time.Second*30*60, getJoinedGroupIDList)
2022-07-20 15:49:21 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
2022-07-20 16:07:30 +08:00
err = json.Unmarshal([]byte(joinedGroupIDListStr), &joinedGroupList)
2022-07-20 15:49:21 +08:00
return joinedGroupList, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 16:37:33 +08:00
func DelJoinedGroupIDListFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
2022-07-14 12:08:28 +08:00
return db.DB.Rc.TagAsDeleted(joinedGroupListCache + userID)
}
2023-01-09 14:27:08 +08:00
func GetGroupMemberIDListFromCache(ctx context.Context, groupID string) (groupMemberIDList []string, err error) {
2022-07-29 14:36:07 +08:00
f := func() (string, error) {
2023-01-09 14:27:08 +08:00
groupInfo, err := GetGroupInfoFromCache(ctx, groupID)
2022-07-20 15:49:21 +08:00
if err != nil {
2022-07-29 14:36:07 +08:00
return "", utils.Wrap(err, "GetGroupInfoFromCache failed")
}
var groupMemberIDList []string
if groupInfo.GroupType == constant.SuperGroup {
superGroup, err := db.DB.GetSuperGroup(groupID)
if err != nil {
return "", utils.Wrap(err, "")
}
groupMemberIDList = superGroup.MemberIDList
} else {
groupMemberIDList, err = imdb.GetGroupMemberIDListByGroupID(groupID)
if err != nil {
return "", utils.Wrap(err, "")
}
2022-07-20 15:49:21 +08:00
}
bytes, err := json.Marshal(groupMemberIDList)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMemberIDList", groupMemberIDList)
}()
2022-07-29 14:36:07 +08:00
groupIDListStr, err := db.DB.Rc.Fetch(groupCache+groupID, time.Second*30*60, f)
2022-07-20 15:49:21 +08:00
if err != nil {
return nil, utils.Wrap(err, "")
}
2022-07-20 16:07:30 +08:00
err = json.Unmarshal([]byte(groupIDListStr), &groupMemberIDList)
2022-07-20 15:49:21 +08:00
return groupMemberIDList, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func DelGroupMemberIDListFromCache(ctx context.Context, groupID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID)
}()
return db.DB.Rc.TagAsDeleted(groupCache + groupID)
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func GetUserInfoFromCache(ctx context.Context, userID string) (userInfo *imdb.User, err error) {
2022-07-14 12:08:28 +08:00
getUserInfo := func() (string, error) {
userInfo, err := imdb.GetUserByUserID(userID)
if err != nil {
2022-07-20 15:49:21 +08:00
return "", utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
bytes, err := json.Marshal(userInfo)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID, "userInfo", *userInfo)
}()
2022-07-20 11:35:19 +08:00
userInfoStr, err := db.DB.Rc.Fetch(userInfoCache+userID, time.Second*30*60, getUserInfo)
2022-07-14 12:08:28 +08:00
if err != nil {
2022-07-20 15:49:21 +08:00
return nil, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
userInfo = &imdb.User{}
2022-07-14 12:08:28 +08:00
err = json.Unmarshal([]byte(userInfoStr), userInfo)
2022-07-20 15:49:21 +08:00
return userInfo, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func GetUserInfoFromCacheBatch(ctx context.Context, userIDs []string) ([]*imdb.User, error) {
2023-01-06 16:19:57 +08:00
var users []*imdb.User
for _, userID := range userIDs {
2023-01-09 14:27:08 +08:00
user, err := GetUserInfoFromCache(ctx, userID)
2023-01-06 16:19:57 +08:00
if err != nil {
return nil, err
}
users = append(users, user)
}
return users, nil
}
2023-01-09 14:27:08 +08:00
func DelUserInfoFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
2022-07-14 12:08:28 +08:00
return db.DB.Rc.TagAsDeleted(userInfoCache + userID)
}
2023-01-09 14:27:08 +08:00
func GetGroupMemberInfoFromCache(ctx context.Context, groupID, userID string) (groupMember *imdb.GroupMember, err error) {
2022-07-14 12:08:28 +08:00
getGroupMemberInfo := func() (string, error) {
groupMemberInfo, err := imdb.GetGroupMemberInfoByGroupIDAndUserID(groupID, userID)
if err != nil {
2022-07-20 15:49:21 +08:00
return "", utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
bytes, err := json.Marshal(groupMemberInfo)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupMember", *groupMember)
}()
2022-08-04 18:24:17 +08:00
groupMemberInfoStr, err := db.DB.Rc.Fetch(groupMemberInfoCache+groupID+"-"+userID, time.Second*30*60, getGroupMemberInfo)
2022-07-14 12:08:28 +08:00
if err != nil {
2022-07-20 15:49:21 +08:00
return nil, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
groupMember = &imdb.GroupMember{}
2022-07-14 12:08:28 +08:00
err = json.Unmarshal([]byte(groupMemberInfoStr), groupMember)
2022-07-20 15:49:21 +08:00
return groupMember, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func DelGroupMemberInfoFromCache(ctx context.Context, groupID, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID)
}()
2022-07-14 12:08:28 +08:00
return db.DB.Rc.TagAsDeleted(groupMemberInfoCache + groupID + "-" + userID)
}
2023-01-09 14:27:08 +08:00
func GetGroupMembersInfoFromCache(ctx context.Context, count, offset int32, groupID string) (groupMembers []*imdb.GroupMember, err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "count", count, "offset", offset, "groupID", groupID, "groupMember", groupMembers)
}()
groupMemberIDList, err := GetGroupMemberIDListFromCache(ctx, groupID)
2022-08-04 18:24:17 +08:00
if err != nil {
return nil, err
}
if count < 0 || offset < 0 {
return nil, nil
}
2023-01-04 17:22:55 +08:00
var groupMemberList []*imdb.GroupMember
2022-08-04 18:55:38 +08:00
var start, stop int32
start = offset
stop = offset + count
l := int32(len(groupMemberIDList))
if start > stop {
return nil, nil
}
if start >= l {
return nil, nil
}
2022-08-04 17:20:33 +08:00
if count != 0 {
2022-08-04 18:24:17 +08:00
if stop >= l {
stop = l
}
groupMemberIDList = groupMemberIDList[start:stop]
2022-08-04 18:55:38 +08:00
} else {
if l < 1000 {
stop = l
} else {
stop = 1000
}
groupMemberIDList = groupMemberIDList[start:stop]
2022-08-04 17:20:33 +08:00
}
2022-08-04 18:55:38 +08:00
//log.NewDebug("", utils.GetSelfFuncName(), "ID list: ", groupMemberIDList)
2022-08-04 18:24:17 +08:00
for _, userID := range groupMemberIDList {
2023-01-09 14:27:08 +08:00
groupMember, err := GetGroupMemberInfoFromCache(ctx, groupID, userID)
2022-08-04 17:20:33 +08:00
if err != nil {
2022-08-04 18:24:17 +08:00
log.NewError("", utils.GetSelfFuncName(), err.Error(), groupID, userID)
2022-08-05 18:38:25 +08:00
continue
2022-08-04 17:20:33 +08:00
}
2023-01-09 14:27:08 +08:00
groupMembers = append(groupMembers, groupMember)
2022-08-04 17:20:33 +08:00
}
return groupMemberList, nil
}
2023-01-09 14:27:08 +08:00
func GetAllGroupMembersInfoFromCache(ctx context.Context, groupID string) (groupMembers []*imdb.GroupMember, err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMembers", groupMembers)
}()
2022-07-14 12:08:28 +08:00
getGroupMemberInfo := func() (string, error) {
groupMembers, err := imdb.GetGroupMemberListByGroupID(groupID)
if err != nil {
2022-07-20 15:49:21 +08:00
return "", utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
bytes, err := json.Marshal(groupMembers)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2022-07-20 11:35:19 +08:00
groupMembersStr, err := db.DB.Rc.Fetch(groupAllMemberInfoCache+groupID, time.Second*30*60, getGroupMemberInfo)
2022-07-14 12:08:28 +08:00
if err != nil {
2022-07-20 15:49:21 +08:00
return nil, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
err = json.Unmarshal([]byte(groupMembersStr), &groupMembers)
2022-07-20 15:49:21 +08:00
return groupMembers, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func DelAllGroupMembersInfoFromCache(ctx context.Context, groupID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID)
}()
2022-07-14 12:08:28 +08:00
return db.DB.Rc.TagAsDeleted(groupAllMemberInfoCache + groupID)
}
2023-01-09 14:27:08 +08:00
func GetGroupInfoFromCache(ctx context.Context, groupID string) (groupInfo *imdb.Group, err error) {
2022-07-14 12:08:28 +08:00
getGroupInfo := func() (string, error) {
groupInfo, err := imdb.GetGroupInfoByGroupID(groupID)
if err != nil {
2022-07-20 15:49:21 +08:00
return "", utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
bytes, err := json.Marshal(groupInfo)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2023-01-11 19:03:26 +08:00
groupInfo = &imdb.Group{}
2023-01-09 14:27:08 +08:00
defer func() {
2023-01-11 19:03:26 +08:00
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupInfo", groupInfo)
2023-01-09 14:27:08 +08:00
}()
2022-07-20 11:35:19 +08:00
groupInfoStr, err := db.DB.Rc.Fetch(groupInfoCache+groupID, time.Second*30*60, getGroupInfo)
2022-07-14 12:08:28 +08:00
if err != nil {
2022-07-20 15:49:21 +08:00
return nil, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
err = json.Unmarshal([]byte(groupInfoStr), groupInfo)
2022-07-20 15:49:21 +08:00
return groupInfo, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func DelGroupInfoFromCache(ctx context.Context, groupID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID)
}()
2022-07-14 12:08:28 +08:00
return db.DB.Rc.TagAsDeleted(groupInfoCache + groupID)
}
2023-01-09 14:27:08 +08:00
func GetAllFriendsInfoFromCache(ctx context.Context, userID string) (friends []*imdb.Friend, err error) {
2022-07-14 12:08:28 +08:00
getAllFriendInfo := func() (string, error) {
friendInfoList, err := imdb.GetFriendListByUserID(userID)
if err != nil {
2022-07-20 15:49:21 +08:00
return "", utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
bytes, err := json.Marshal(friendInfoList)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID, "friends", friends)
}()
2022-07-20 11:35:19 +08:00
allFriendInfoStr, err := db.DB.Rc.Fetch(allFriendInfoCache+userID, time.Second*30*60, getAllFriendInfo)
2022-07-14 12:08:28 +08:00
if err != nil {
2022-07-20 15:49:21 +08:00
return nil, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
err = json.Unmarshal([]byte(allFriendInfoStr), &friends)
return friends, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-09 14:27:08 +08:00
func DelAllFriendsInfoFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
2022-07-14 12:08:28 +08:00
return db.DB.Rc.TagAsDeleted(allFriendInfoCache + userID)
}
2023-01-04 17:22:55 +08:00
func GetAllDepartmentsFromCache() ([]imdb.Department, error) {
2022-07-14 12:08:28 +08:00
getAllDepartments := func() (string, error) {
departmentList, err := imdb.GetSubDepartmentList("-1")
if err != nil {
2022-07-20 15:49:21 +08:00
return "", utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
bytes, err := json.Marshal(departmentList)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2022-07-20 11:35:19 +08:00
allDepartmentsStr, err := db.DB.Rc.Fetch(allDepartmentCache, time.Second*30*60, getAllDepartments)
2022-07-14 12:08:28 +08:00
if err != nil {
2022-07-20 15:49:21 +08:00
return nil, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-04 17:22:55 +08:00
var allDepartments []imdb.Department
2022-07-14 12:08:28 +08:00
err = json.Unmarshal([]byte(allDepartmentsStr), &allDepartments)
2022-07-20 15:49:21 +08:00
return allDepartments, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
func DelAllDepartmentsFromCache() error {
return db.DB.Rc.TagAsDeleted(allDepartmentCache)
}
2023-01-04 17:22:55 +08:00
func GetAllDepartmentMembersFromCache() ([]imdb.DepartmentMember, error) {
2022-07-14 12:08:28 +08:00
getAllDepartmentMembers := func() (string, error) {
departmentMembers, err := imdb.GetDepartmentMemberList("-1")
if err != nil {
2022-07-20 15:49:21 +08:00
return "", utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
bytes, err := json.Marshal(departmentMembers)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-14 12:08:28 +08:00
}
2022-07-20 11:35:19 +08:00
allDepartmentMembersStr, err := db.DB.Rc.Fetch(allDepartmentMemberCache, time.Second*30*60, getAllDepartmentMembers)
2022-07-14 12:08:28 +08:00
if err != nil {
2022-07-20 15:49:21 +08:00
return nil, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
2023-01-04 17:22:55 +08:00
var allDepartmentMembers []imdb.DepartmentMember
2022-07-14 12:08:28 +08:00
err = json.Unmarshal([]byte(allDepartmentMembersStr), &allDepartmentMembers)
2022-07-20 15:49:21 +08:00
return allDepartmentMembers, utils.Wrap(err, "")
2022-07-14 12:08:28 +08:00
}
func DelAllDepartmentMembersFromCache() error {
return db.DB.Rc.TagAsDeleted(allDepartmentMemberCache)
}
2022-07-29 14:36:07 +08:00
2023-01-09 14:27:08 +08:00
func GetJoinedSuperGroupListFromCache(ctx context.Context, userID string) (joinedSuperGroupIDs []string, err error) {
2022-07-29 14:36:07 +08:00
getJoinedSuperGroupIDList := func() (string, error) {
userToSuperGroup, err := db.DB.GetSuperGroupByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(userToSuperGroup.GroupIDList)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-07-29 14:36:07 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID, "joinedSuperGroupIDs", joinedSuperGroupIDs)
}()
2022-08-08 11:30:10 +08:00
joinedSuperGroupListStr, err := db.DB.Rc.Fetch(joinedSuperGroupListCache+userID, time.Second*30*60, getJoinedSuperGroupIDList)
2022-08-24 20:33:41 +08:00
if err != nil {
return nil, err
}
2023-01-09 14:27:08 +08:00
err = json.Unmarshal([]byte(joinedSuperGroupListStr), &joinedSuperGroupIDs)
return joinedSuperGroupIDs, utils.Wrap(err, "")
2022-07-29 14:36:07 +08:00
}
2023-01-09 14:27:08 +08:00
func DelJoinedSuperGroupIDListFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
return db.DB.Rc.TagAsDeleted(joinedSuperGroupListCache + userID)
2022-07-29 14:36:07 +08:00
}
2023-01-09 14:27:08 +08:00
func GetGroupMemberListHashFromCache(ctx context.Context, groupID string) (hashCodeUint64 uint64, err error) {
2022-08-08 11:30:10 +08:00
generateHash := func() (string, error) {
2023-01-09 14:27:08 +08:00
groupInfo, err := GetGroupInfoFromCache(ctx, groupID)
2022-09-13 18:31:47 +08:00
if err != nil {
return "0", utils.Wrap(err, "GetGroupInfoFromCache failed")
}
if groupInfo.Status == constant.GroupStatusDismissed {
return "0", nil
}
2023-01-09 14:27:08 +08:00
groupMemberIDList, err := GetGroupMemberIDListFromCache(ctx, groupID)
2022-08-08 11:30:10 +08:00
if err != nil {
2022-08-08 13:44:43 +08:00
return "", utils.Wrap(err, "GetGroupMemberIDListFromCache failed")
2022-08-08 11:30:10 +08:00
}
sort.Strings(groupMemberIDList)
var all string
for _, v := range groupMemberIDList {
all += v
}
bi := big.NewInt(0)
2022-08-08 13:43:57 +08:00
bi.SetString(utils.Md5(all)[0:8], 16)
2022-08-08 11:30:10 +08:00
return strconv.Itoa(int(bi.Uint64())), nil
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID, "hashCodeUint64", hashCodeUint64)
}()
hashCodeStr, err := db.DB.Rc.Fetch(groupMemberListHashCache+groupID, time.Second*30*60, generateHash)
2022-09-13 18:31:47 +08:00
if err != nil {
return 0, utils.Wrap(err, "fetch failed")
}
2023-01-09 14:27:08 +08:00
hashCode, err := strconv.Atoi(hashCodeStr)
return uint64(hashCode), err
2022-08-08 11:30:10 +08:00
}
2023-01-09 14:27:08 +08:00
func DelGroupMemberListHashFromCache(ctx context.Context, groupID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID)
}()
return db.DB.Rc.TagAsDeleted(groupMemberListHashCache + groupID)
2022-08-08 11:30:10 +08:00
}
2023-01-09 14:27:08 +08:00
func GetGroupMemberNumFromCache(ctx context.Context, groupID string) (num int, err error) {
2022-08-08 11:30:10 +08:00
getGroupMemberNum := func() (string, error) {
num, err := imdb.GetGroupMemberNumByGroupID(groupID)
if err != nil {
return "", utils.Wrap(err, "")
}
return strconv.Itoa(int(num)), nil
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID, "num", num)
}()
2022-08-08 11:30:10 +08:00
groupMember, err := db.DB.Rc.Fetch(groupMemberNumCache+groupID, time.Second*30*60, getGroupMemberNum)
2022-09-20 19:18:10 +08:00
if err != nil {
return 0, utils.Wrap(err, "")
}
2023-01-09 14:27:08 +08:00
return strconv.Atoi(groupMember)
2022-07-29 14:36:07 +08:00
}
2023-01-09 14:27:08 +08:00
func DelGroupMemberNumFromCache(ctx context.Context, groupID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "groupID", groupID)
}()
2022-08-08 11:30:10 +08:00
return db.DB.Rc.TagAsDeleted(groupMemberNumCache + groupID)
2022-07-29 14:36:07 +08:00
}
2022-08-19 12:09:05 +08:00
2023-01-09 14:27:08 +08:00
func GetUserConversationIDListFromCache(ctx context.Context, userID string) (conversationIDs []string, err error) {
2022-08-19 12:09:05 +08:00
getConversationIDList := func() (string, error) {
conversationIDList, err := imdb.GetConversationIDListByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "getConversationIDList failed")
}
2022-08-22 16:14:31 +08:00
log.NewDebug("", utils.GetSelfFuncName(), conversationIDList)
2022-08-19 12:09:05 +08:00
bytes, err := json.Marshal(conversationIDList)
2022-08-26 17:51:01 +08:00
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
2022-08-19 12:09:05 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID, "conversationIDs", conversationIDs)
}()
2022-08-19 12:09:05 +08:00
conversationIDListStr, err := db.DB.Rc.Fetch(conversationIDListCache+userID, time.Second*30*60, getConversationIDList)
2023-01-09 14:27:08 +08:00
err = json.Unmarshal([]byte(conversationIDListStr), &conversationIDs)
2022-08-19 12:09:05 +08:00
if err != nil {
2022-08-22 16:14:31 +08:00
return nil, utils.Wrap(err, "")
2022-08-19 12:09:05 +08:00
}
2023-01-09 14:27:08 +08:00
return conversationIDs, nil
2022-08-19 12:09:05 +08:00
}
2023-01-09 14:27:08 +08:00
func DelUserConversationIDListFromCache(ctx context.Context, userID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "userID", userID)
}()
2022-08-23 12:06:44 +08:00
return utils.Wrap(db.DB.Rc.TagAsDeleted(conversationIDListCache+userID), "DelUserConversationIDListFromCache err")
2022-08-19 12:09:05 +08:00
}
2023-01-09 14:27:08 +08:00
func GetConversationFromCache(ctx context.Context, ownerUserID, conversationID string) (conversation *imdb.Conversation, err error) {
2022-08-19 12:09:05 +08:00
getConversation := func() (string, error) {
conversation, err := imdb.GetConversation(ownerUserID, conversationID)
if err != nil {
2022-08-22 15:47:50 +08:00
return "", utils.Wrap(err, "get failed")
2022-08-19 12:09:05 +08:00
}
bytes, err := json.Marshal(conversation)
2022-08-22 16:03:59 +08:00
if err != nil {
return "", utils.Wrap(err, "Marshal failed")
}
return string(bytes), nil
2022-08-19 12:09:05 +08:00
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationID", conversationID, "conversation", *conversation)
}()
2022-08-21 17:33:40 +08:00
conversationStr, err := db.DB.Rc.Fetch(conversationCache+ownerUserID+":"+conversationID, time.Second*30*60, getConversation)
2022-08-22 16:03:59 +08:00
if err != nil {
return nil, utils.Wrap(err, "Fetch failed")
}
2023-01-09 14:27:08 +08:00
conversation = &imdb.Conversation{}
2022-08-19 12:09:05 +08:00
err = json.Unmarshal([]byte(conversationStr), &conversation)
2023-01-09 14:27:08 +08:00
return conversation, utils.Wrap(err, "Unmarshal failed")
2022-08-19 12:09:05 +08:00
}
2023-01-09 14:27:08 +08:00
func GetConversationsFromCache(ctx context.Context, ownerUserID string, conversationIDs []string) (conversations []imdb.Conversation, err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationIDs", conversationIDs, "conversations", conversations)
}()
for _, conversationID := range conversationIDs {
conversation, err := GetConversationFromCache(ctx, ownerUserID, conversationID)
2022-08-21 17:33:40 +08:00
if err != nil {
return nil, utils.Wrap(err, "GetConversationFromCache failed")
}
2023-01-09 14:27:08 +08:00
conversations = append(conversations, *conversation)
2022-08-21 17:33:40 +08:00
}
2023-01-09 14:27:08 +08:00
return conversations, nil
2022-08-21 17:33:40 +08:00
}
2023-01-09 14:27:08 +08:00
func GetUserAllConversationList(ctx context.Context, ownerUserID string) (conversations []imdb.Conversation, err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversations", conversations)
}()
IDList, err := GetUserConversationIDListFromCache(ctx, ownerUserID)
2022-08-19 12:09:05 +08:00
if err != nil {
return nil, err
}
2023-01-04 17:22:55 +08:00
var conversationList []imdb.Conversation
2022-08-22 15:59:45 +08:00
log.NewDebug("", utils.GetSelfFuncName(), IDList)
2022-08-19 12:09:05 +08:00
for _, conversationID := range IDList {
2023-01-09 14:27:08 +08:00
conversation, err := GetConversationFromCache(ctx, ownerUserID, conversationID)
2022-08-19 12:09:05 +08:00
if err != nil {
return nil, utils.Wrap(err, "GetConversationFromCache failed")
}
conversationList = append(conversationList, *conversation)
}
return conversationList, nil
}
2023-01-09 14:27:08 +08:00
func DelConversationFromCache(ctx context.Context, ownerUserID, conversationID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "conversationID", conversationID)
}()
2022-08-23 12:06:44 +08:00
return utils.Wrap(db.DB.Rc.TagAsDeleted(conversationCache+ownerUserID+":"+conversationID), "DelConversationFromCache err")
2022-08-19 12:09:05 +08:00
}
2022-12-01 19:34:54 +08:00
2023-01-09 14:27:08 +08:00
func GetExtendMsg(ctx context.Context, sourceID string, sessionType int32, clientMsgID string, firstModifyTime int64) (extendMsg *db.ExtendMsg, err error) {
2022-12-01 19:34:54 +08:00
getExtendMsg := func() (string, error) {
2022-12-12 19:22:50 +08:00
extendMsg, err := db.DB.GetExtendMsg(sourceID, sessionType, clientMsgID, firstModifyTime)
2022-12-01 19:34:54 +08:00
if err != nil {
return "", utils.Wrap(err, "GetExtendMsgList failed")
}
2022-12-08 12:54:39 +08:00
bytes, err := json.Marshal(extendMsg)
2022-12-01 19:34:54 +08:00
if err != nil {
return "", utils.Wrap(err, "Marshal failed")
}
return string(bytes), nil
}
2023-01-09 14:27:08 +08:00
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "sourceID", sourceID, "sessionType",
sessionType, "clientMsgID", clientMsgID, "firstModifyTime", firstModifyTime, "extendMsg", extendMsg)
}()
2022-12-09 19:54:49 +08:00
extendMsgStr, err := db.DB.Rc.Fetch(extendMsgCache+clientMsgID, time.Second*30*60, getExtendMsg)
2022-12-01 19:34:54 +08:00
if err != nil {
return nil, utils.Wrap(err, "Fetch failed")
}
2023-01-09 14:27:08 +08:00
extendMsg = &db.ExtendMsg{}
2022-12-01 19:34:54 +08:00
err = json.Unmarshal([]byte(extendMsgStr), extendMsg)
2023-01-09 14:27:08 +08:00
return extendMsg, utils.Wrap(err, "Unmarshal failed")
2022-12-01 19:34:54 +08:00
}
2023-01-09 14:27:08 +08:00
func DelExtendMsg(ctx context.Context, clientMsgID string) (err error) {
defer func() {
trace_log.SetContextInfo(ctx, utils.GetFuncName(1), err, "clientMsgID", clientMsgID)
}()
2022-12-09 19:54:49 +08:00
return utils.Wrap(db.DB.Rc.TagAsDeleted(extendMsgCache+clientMsgID), "DelExtendMsg err")
2022-12-01 19:34:54 +08:00
}