You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/rocks_cache/rocks_cache.go

434 lines
14 KiB

2 years ago
package rocksCache
import (
"Open_IM/pkg/common/constant"
2 years ago
"Open_IM/pkg/common/db"
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
2 years ago
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"context"
2 years ago
"encoding/json"
2 years ago
"errors"
"fmt"
2 years ago
"math/big"
"sort"
"strconv"
2 years ago
"time"
)
const (
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:"
2 years ago
groupMemberListHashCache = "GROUP_MEMBER_LIST_HASH_CACHE:"
groupMemberNumCache = "GROUP_MEMBER_NUM_CACHE:"
2 years ago
)
func init() {
fmt.Println("init to del old keys")
for _, key := range []string{groupCache, friendRelationCache, blackListCache, userInfoCache, groupInfoCache, groupOwnerIDCache, joinedGroupListCache,
2 years ago
groupMemberInfoCache, groupAllMemberInfoCache, allFriendInfoCache} {
2 years ago
fName := utils.GetSelfFuncName()
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)
// for each for redis cluster
2 years ago
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())
}
}
}
if cursor == 0 {
break
}
}
}
}
2 years ago
func GetFriendIDListFromCache(userID string) ([]string, error) {
getFriendIDList := func() (string, error) {
friendIDList, err := imdb.GetFriendIDListByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(friendIDList)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
friendIDListStr, err := db.DB.Rc.Fetch(friendRelationCache+userID, time.Second*30*60, getFriendIDList)
if err != nil {
return nil, utils.Wrap(err, "")
}
var friendIDList []string
2 years ago
err = json.Unmarshal([]byte(friendIDListStr), &friendIDList)
return friendIDList, utils.Wrap(err, "")
2 years ago
}
func DelFriendIDListFromCache(userID string) error {
err := db.DB.Rc.TagAsDeleted(friendRelationCache + userID)
return err
}
func GetBlackListFromCache(userID string) ([]string, error) {
getBlackIDList := func() (string, error) {
blackIDList, err := imdb.GetBlackIDListByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(blackIDList)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
blackIDListStr, err := db.DB.Rc.Fetch(blackListCache+userID, time.Second*30*60, getBlackIDList)
if err != nil {
return nil, utils.Wrap(err, "")
}
var blackIDList []string
2 years ago
err = json.Unmarshal([]byte(blackIDListStr), &blackIDList)
return blackIDList, utils.Wrap(err, "")
2 years ago
}
func DelBlackIDListFromCache(userID string) error {
return db.DB.Rc.TagAsDeleted(blackListCache + userID)
}
func GetJoinedGroupIDListFromCache(userID string) ([]string, error) {
getJoinedGroupIDList := func() (string, error) {
joinedGroupList, err := imdb.GetJoinedGroupIDListByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "")
}
bytes, err := json.Marshal(joinedGroupList)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
joinedGroupIDListStr, err := db.DB.Rc.Fetch(joinedGroupListCache+userID, time.Second*30*60, getJoinedGroupIDList)
if err != nil {
return nil, utils.Wrap(err, "")
}
var joinedGroupList []string
2 years ago
err = json.Unmarshal([]byte(joinedGroupIDListStr), &joinedGroupList)
return joinedGroupList, utils.Wrap(err, "")
2 years ago
}
func DelJoinedGroupIDListFromCache(userID string) error {
return db.DB.Rc.TagAsDeleted(joinedGroupListCache + userID)
}
func GetGroupMemberIDListFromCache(groupID string) ([]string, error) {
f := func() (string, error) {
groupInfo, err := GetGroupInfoFromCache(groupID)
if err != nil {
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, "")
}
}
bytes, err := json.Marshal(groupMemberIDList)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
groupIDListStr, err := db.DB.Rc.Fetch(groupCache+groupID, time.Second*30*60, f)
if err != nil {
return nil, utils.Wrap(err, "")
}
var groupMemberIDList []string
2 years ago
err = json.Unmarshal([]byte(groupIDListStr), &groupMemberIDList)
return groupMemberIDList, utils.Wrap(err, "")
2 years ago
}
func DelGroupMemberIDListFromCache(groupID string) error {
err := db.DB.Rc.TagAsDeleted(groupCache + groupID)
2 years ago
return err
}
func GetUserInfoFromCache(userID string) (*db.User, error) {
getUserInfo := func() (string, error) {
userInfo, err := imdb.GetUserByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "")
2 years ago
}
bytes, err := json.Marshal(userInfo)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
userInfoStr, err := db.DB.Rc.Fetch(userInfoCache+userID, time.Second*30*60, getUserInfo)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
2 years ago
}
userInfo := &db.User{}
err = json.Unmarshal([]byte(userInfoStr), userInfo)
return userInfo, utils.Wrap(err, "")
2 years ago
}
func DelUserInfoFromCache(userID string) error {
return db.DB.Rc.TagAsDeleted(userInfoCache + userID)
}
2 years ago
func GetGroupMemberInfoFromCache(groupID, userID string) (*db.GroupMember, error) {
2 years ago
getGroupMemberInfo := func() (string, error) {
groupMemberInfo, err := imdb.GetGroupMemberInfoByGroupIDAndUserID(groupID, userID)
if err != nil {
return "", utils.Wrap(err, "")
2 years ago
}
bytes, err := json.Marshal(groupMemberInfo)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
2 years ago
groupMemberInfoStr, err := db.DB.Rc.Fetch(groupMemberInfoCache+groupID+"-"+userID, time.Second*30*60, getGroupMemberInfo)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
2 years ago
}
groupMember := &db.GroupMember{}
err = json.Unmarshal([]byte(groupMemberInfoStr), groupMember)
return groupMember, utils.Wrap(err, "")
2 years ago
}
func DelGroupMemberInfoFromCache(groupID, userID string) error {
return db.DB.Rc.TagAsDeleted(groupMemberInfoCache + groupID + "-" + userID)
}
2 years ago
func GetGroupMembersInfoFromCache(count, offset int32, groupID string) ([]*db.GroupMember, error) {
2 years ago
groupMemberIDList, err := GetGroupMemberIDListFromCache(groupID)
if err != nil {
return nil, err
}
if count < 0 || offset < 0 {
return nil, nil
}
var groupMemberList []*db.GroupMember
2 years ago
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
}
2 years ago
if count != 0 {
2 years ago
if stop >= l {
stop = l
}
groupMemberIDList = groupMemberIDList[start:stop]
2 years ago
} else {
if l < 1000 {
stop = l
} else {
stop = 1000
}
groupMemberIDList = groupMemberIDList[start:stop]
2 years ago
}
2 years ago
//log.NewDebug("", utils.GetSelfFuncName(), "ID list: ", groupMemberIDList)
2 years ago
for _, userID := range groupMemberIDList {
groupMembers, err := GetGroupMemberInfoFromCache(groupID, userID)
2 years ago
if err != nil {
2 years ago
log.NewError("", utils.GetSelfFuncName(), err.Error(), groupID, userID)
2 years ago
continue
2 years ago
}
2 years ago
groupMemberList = append(groupMemberList, groupMembers)
2 years ago
}
return groupMemberList, nil
}
2 years ago
func GetAllGroupMembersInfoFromCache(groupID string) ([]*db.GroupMember, error) {
getGroupMemberInfo := func() (string, error) {
groupMembers, err := imdb.GetGroupMemberListByGroupID(groupID)
if err != nil {
return "", utils.Wrap(err, "")
2 years ago
}
bytes, err := json.Marshal(groupMembers)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
groupMembersStr, err := db.DB.Rc.Fetch(groupAllMemberInfoCache+groupID, time.Second*30*60, getGroupMemberInfo)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
2 years ago
}
var groupMembers []*db.GroupMember
err = json.Unmarshal([]byte(groupMembersStr), &groupMembers)
return groupMembers, utils.Wrap(err, "")
2 years ago
}
func DelAllGroupMembersInfoFromCache(groupID string) error {
return db.DB.Rc.TagAsDeleted(groupAllMemberInfoCache + groupID)
}
func GetGroupInfoFromCache(groupID string) (*db.Group, error) {
getGroupInfo := func() (string, error) {
groupInfo, err := imdb.GetGroupInfoByGroupID(groupID)
if err != nil {
return "", utils.Wrap(err, "")
2 years ago
}
bytes, err := json.Marshal(groupInfo)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
groupInfoStr, err := db.DB.Rc.Fetch(groupInfoCache+groupID, time.Second*30*60, getGroupInfo)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
2 years ago
}
groupInfo := &db.Group{}
err = json.Unmarshal([]byte(groupInfoStr), groupInfo)
return groupInfo, utils.Wrap(err, "")
2 years ago
}
func DelGroupInfoFromCache(groupID string) error {
return db.DB.Rc.TagAsDeleted(groupInfoCache + groupID)
}
func GetAllFriendsInfoFromCache(userID string) ([]*db.Friend, error) {
getAllFriendInfo := func() (string, error) {
friendInfoList, err := imdb.GetFriendListByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "")
2 years ago
}
bytes, err := json.Marshal(friendInfoList)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
allFriendInfoStr, err := db.DB.Rc.Fetch(allFriendInfoCache+userID, time.Second*30*60, getAllFriendInfo)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
2 years ago
}
var friendInfoList []*db.Friend
err = json.Unmarshal([]byte(allFriendInfoStr), &friendInfoList)
return friendInfoList, utils.Wrap(err, "")
2 years ago
}
func DelAllFriendsInfoFromCache(userID string) error {
return db.DB.Rc.TagAsDeleted(allFriendInfoCache + userID)
}
2 years ago
func GetAllDepartmentsFromCache() ([]db.Department, error) {
2 years ago
getAllDepartments := func() (string, error) {
departmentList, err := imdb.GetSubDepartmentList("-1")
if err != nil {
return "", utils.Wrap(err, "")
2 years ago
}
bytes, err := json.Marshal(departmentList)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
allDepartmentsStr, err := db.DB.Rc.Fetch(allDepartmentCache, time.Second*30*60, getAllDepartments)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
2 years ago
}
2 years ago
var allDepartments []db.Department
2 years ago
err = json.Unmarshal([]byte(allDepartmentsStr), &allDepartments)
return allDepartments, utils.Wrap(err, "")
2 years ago
}
func DelAllDepartmentsFromCache() error {
return db.DB.Rc.TagAsDeleted(allDepartmentCache)
}
2 years ago
func GetAllDepartmentMembersFromCache() ([]db.DepartmentMember, error) {
2 years ago
getAllDepartmentMembers := func() (string, error) {
departmentMembers, err := imdb.GetDepartmentMemberList("-1")
if err != nil {
return "", utils.Wrap(err, "")
2 years ago
}
bytes, err := json.Marshal(departmentMembers)
return string(bytes), utils.Wrap(err, "")
2 years ago
}
allDepartmentMembersStr, err := db.DB.Rc.Fetch(allDepartmentMemberCache, time.Second*30*60, getAllDepartmentMembers)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "")
2 years ago
}
2 years ago
var allDepartmentMembers []db.DepartmentMember
2 years ago
err = json.Unmarshal([]byte(allDepartmentMembersStr), &allDepartmentMembers)
return allDepartmentMembers, utils.Wrap(err, "")
2 years ago
}
func DelAllDepartmentMembersFromCache() error {
return db.DB.Rc.TagAsDeleted(allDepartmentMemberCache)
}
func GetJoinedSuperGroupListFromCache(userID string) ([]string, error) {
getJoinedSuperGroupIDList := func() (string, error) {
userToSuperGroup, err := db.DB.GetSuperGroupByUserID(userID)
if err != nil {
return "", utils.Wrap(err, "")
}
2 years ago
if len(userToSuperGroup.GroupIDList) == 0 {
return "", errors.New("GroupIDList == 0")
}
bytes, err := json.Marshal(userToSuperGroup.GroupIDList)
return string(bytes), utils.Wrap(err, "")
}
2 years ago
joinedSuperGroupListStr, err := db.DB.Rc.Fetch(joinedSuperGroupListCache+userID, time.Second*30*60, getJoinedSuperGroupIDList)
var joinedSuperGroupList []string
err = json.Unmarshal([]byte(joinedSuperGroupListStr), &joinedSuperGroupList)
return joinedSuperGroupList, err
}
func DelJoinedSuperGroupIDListFromCache(userID string) error {
err := db.DB.Rc.TagAsDeleted(joinedSuperGroupListCache + userID)
return err
}
2 years ago
func GetGroupMemberListHashFromCache(groupID string) (uint64, error) {
generateHash := func() (string, error) {
groupMemberIDList, err := GetGroupMemberIDListFromCache(groupID)
if err != nil {
return "", utils.Wrap(err, "")
}
sort.Strings(groupMemberIDList)
var all string
for _, v := range groupMemberIDList {
all += v
}
bi := big.NewInt(0)
2 years ago
bi.SetString(utils.Md5(all)[0:8], 10)
2 years ago
return strconv.Itoa(int(bi.Uint64())), nil
}
hashCode, err := db.DB.Rc.Fetch(groupMemberListHashCache+groupID, time.Second*30*60, generateHash)
hashCodeUint64, err := strconv.Atoi(hashCode)
return uint64(hashCodeUint64), err
}
func DelGroupMemberListHashFromCache(groupID string) error {
err := db.DB.Rc.TagAsDeleted(groupMemberListHashCache + groupID)
return err
}
func GetGroupMemberNumFromCache(groupID string) (int64, error) {
getGroupMemberNum := func() (string, error) {
num, err := imdb.GetGroupMemberNumByGroupID(groupID)
if err != nil {
return "", utils.Wrap(err, "")
}
return strconv.Itoa(int(num)), nil
}
groupMember, err := db.DB.Rc.Fetch(groupMemberNumCache+groupID, time.Second*30*60, getGroupMemberNum)
num, err := strconv.Atoi(groupMember)
return int64(num), err
}
2 years ago
func DelGroupMemberNumFromCache(groupID string) error {
return db.DB.Rc.TagAsDeleted(groupMemberNumCache + groupID)
}