mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-26 19:46:57 +08:00
group cache
This commit is contained in:
parent
29b05499df
commit
5da1195274
14
pkg/common/db/cache/group.go
vendored
14
pkg/common/db/cache/group.go
vendored
@ -37,19 +37,18 @@ type GroupCache interface {
|
|||||||
BatchDelJoinedSuperGroupIDs(ctx context.Context, userIDs []string, fn func(ctx context.Context, userIDs []string) error) (err error)
|
BatchDelJoinedSuperGroupIDs(ctx context.Context, userIDs []string, fn func(ctx context.Context, userIDs []string) error) (err error)
|
||||||
|
|
||||||
GetJoinedSuperGroupIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) (joinedSuperGroupIDs []string, err error)) (joinedSuperGroupIDs []string, err error)
|
GetJoinedSuperGroupIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) (joinedSuperGroupIDs []string, err error)) (joinedSuperGroupIDs []string, err error)
|
||||||
DelJoinedSuperGroupIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) error) (err error)
|
DelJoinedSuperGroupIDs(ctx context.Context, userID string) (err error)
|
||||||
|
|
||||||
GetGroupMembersHash(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (hashCodeUint64 uint64, err error)) (hashCodeUint64 uint64, err error)
|
GetGroupMembersHash(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (hashCodeUint64 uint64, err error)) (hashCodeUint64 uint64, err error)
|
||||||
DelGroupMembersHash(ctx context.Context, groupID string) (err error)
|
DelGroupMembersHash(ctx context.Context, groupID string) (err error)
|
||||||
|
|
||||||
GetGroupMemberIDs(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (groupMemberIDs []string, err error)) (groupMemberIDs []string, err error)
|
GetGroupMemberIDs(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (groupMemberIDs []string, err error)) (groupMemberIDs []string, err error)
|
||||||
DelGroupMemberIDs(ctx context.Context, groupID string)
|
DelGroupMemberIDs(ctx context.Context, groupID string) error
|
||||||
|
|
||||||
GetJoinedGroupIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) (joinedGroupIDs []string, err error)) (joinedGroupIDs []string, err error)
|
GetJoinedGroupIDs(ctx context.Context, userID string, fn func(ctx context.Context, userID string) (joinedGroupIDs []string, err error)) (joinedGroupIDs []string, err error)
|
||||||
DelJoinedGroupIDs(ctx context.Context, userID string) (err error)
|
DelJoinedGroupIDs(ctx context.Context, userID string) (err error)
|
||||||
|
|
||||||
GetGroupMemberInfo(ctx context.Context, groupID, userID string, fn func(ctx context.Context, groupID, userID string) (groupMember *relationTb.GroupMemberModel, err error)) (groupMember *relationTb.GroupMemberModel, err error)
|
GetGroupMemberInfo(ctx context.Context, groupID, userID string, fn func(ctx context.Context, groupID, userID string) (groupMember *relationTb.GroupMemberModel, err error)) (groupMember *relationTb.GroupMemberModel, err error)
|
||||||
GetGroupMembersInfo(ctx context.Context, count, offset int32, groupID string, fn func(ctx context.Context, count, offset int32, groupID string) (groupMembers []*relationTb.GroupMemberModel, err error)) (groupMembers []*relationTb.GroupMemberModel, err error)
|
|
||||||
DelGroupMemberInfo(ctx context.Context, groupID, userID string) (err error)
|
DelGroupMemberInfo(ctx context.Context, groupID, userID string) (err error)
|
||||||
|
|
||||||
GetGroupMemberNum(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (num int, err error)) (num int, err error)
|
GetGroupMemberNum(ctx context.Context, groupID string, fn func(ctx context.Context, groupID string) (num int, err error)) (num int, err error)
|
||||||
@ -117,7 +116,7 @@ func (g *GroupCacheRedis) GetGroupsInfo(ctx context.Context, groupIDs []string)
|
|||||||
return groups, nil
|
return groups, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupInfo(ctx context.Context, groupID string) (group *relation.GroupGorm, err error) {
|
func (g *GroupCacheRedis) GetGroupInfo(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||||
getGroup := func() (string, error) {
|
getGroup := func() (string, error) {
|
||||||
groupInfo, err := g.group.Take(ctx, groupID)
|
groupInfo, err := g.group.Take(ctx, groupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -129,7 +128,7 @@ func (g *GroupCacheRedis) GetGroupInfo(ctx context.Context, groupID string) (gro
|
|||||||
}
|
}
|
||||||
return string(bytes), nil
|
return string(bytes), nil
|
||||||
}
|
}
|
||||||
group = &relation.GroupGorm{}
|
group = &relationTb.GroupModel{}
|
||||||
defer func() {
|
defer func() {
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group)
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", *group)
|
||||||
}()
|
}()
|
||||||
@ -339,6 +338,11 @@ func (g *GroupCacheRedis) GetGroupMemberInfo(ctx context.Context, groupID, userI
|
|||||||
return groupMember, utils.Wrap(err, "")
|
return groupMember, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, groupID, userIDs []string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||||
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, count, offset int32, groupID string) (groupMembers []*relation.GroupMember, err error) {
|
func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, count, offset int32, groupID string) (groupMembers []*relation.GroupMember, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "count", count, "offset", offset, "groupID", groupID, "groupMember", groupMembers)
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "count", count, "offset", offset, "groupID", groupID, "groupMember", groupMembers)
|
||||||
|
@ -236,12 +236,34 @@ type GroupDataBase struct {
|
|||||||
groupRequestDB relationTb.GroupRequestModelInterface
|
groupRequestDB relationTb.GroupRequestModelInterface
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
|
|
||||||
cache *cache.GroupCache
|
//cache cache.GroupCache
|
||||||
|
cache *cache.GroupCacheRedis
|
||||||
mongoDB *unrelation.SuperGroupMongoDriver
|
mongoDB *unrelation.SuperGroupMongoDriver
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *GroupDataBase) delGroupMemberCache(ctx context.Context, groupID string, userIDs []string) error {
|
||||||
|
for _, userID := range userIDs {
|
||||||
|
if err := g.cache.DelJoinedGroupIDs(ctx, userID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.cache.DelJoinedSuperGroupIDs(ctx, userID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := g.cache.DelGroupMemberIDs(ctx, groupID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.cache.DelGroupMemberNum(ctx, groupID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.cache.DelGroupMembersHash(ctx, groupID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error) {
|
func (g *GroupDataBase) FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error) {
|
||||||
return g.groupMemberDB.FindMemberUserID(ctx, groupID)
|
return g.cache.GetGroupMemberIDs(ctx, groupID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
|
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
|
||||||
@ -263,11 +285,11 @@ func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.Gr
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
func (g *GroupDataBase) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||||
return g.groupDB.Take(ctx, groupID)
|
return g.cache.GetGroupInfo(ctx, groupID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
func (g *GroupDataBase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
||||||
return g.groupDB.Find(ctx, groupIDs)
|
return g.cache.GetGroupsInfo(ctx, groupIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error) {
|
func (g *GroupDataBase) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error) {
|
||||||
@ -275,7 +297,15 @@ func (g *GroupDataBase) SearchGroup(ctx context.Context, keyword string, pageNum
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
|
func (g *GroupDataBase) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
|
||||||
return g.groupDB.UpdateMap(ctx, groupID, data)
|
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := g.groupDB.UpdateMap(ctx, groupID, data, tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.cache.DelGroupInfo(ctx, groupID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) DismissGroup(ctx context.Context, groupID string) error {
|
func (g *GroupDataBase) DismissGroup(ctx context.Context, groupID string) error {
|
||||||
@ -283,13 +313,22 @@ func (g *GroupDataBase) DismissGroup(ctx context.Context, groupID string) error
|
|||||||
if err := g.groupDB.UpdateStatus(ctx, groupID, constant.GroupStatusDismissed, tx); err != nil {
|
if err := g.groupDB.UpdateStatus(ctx, groupID, constant.GroupStatusDismissed, tx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return g.groupMemberDB.DeleteGroup(ctx, []string{groupID}, tx)
|
if err := g.groupMemberDB.DeleteGroup(ctx, []string{groupID}, tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
userIDs, err := g.cache.GetGroupMemberIDs(ctx, groupID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.delGroupMemberCache(ctx, groupID, userIDs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
|
func (g *GroupDataBase) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||||
return g.groupMemberDB.Take(ctx, groupID, userID)
|
return g.cache.GetGroupMemberInfo(ctx, groupID, userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
|
func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
|
||||||
@ -297,6 +336,7 @@ func (g *GroupDataBase) TakeGroupOwner(ctx context.Context, groupID string) (*re
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error) {
|
func (g *GroupDataBase) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error) {
|
||||||
|
//g.cache.GetGroupMembersInfo()
|
||||||
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels)
|
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,19 +349,32 @@ func (g *GroupDataBase) SearchGroupMember(ctx context.Context, keyword string, g
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
|
func (g *GroupDataBase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
|
||||||
if member == nil {
|
|
||||||
return g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult)
|
|
||||||
}
|
|
||||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||||
if err := g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult, tx); err != nil {
|
if err := g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult, tx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return g.groupMemberDB.Create(ctx, []*relationTb.GroupMemberModel{member}, tx)
|
if member != nil {
|
||||||
|
if err := g.groupMemberDB.Create(ctx, []*relationTb.GroupMemberModel{member}, tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.delGroupMemberCache(ctx, groupID, []string{userID}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
func (g *GroupDataBase) DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||||
return g.groupMemberDB.Delete(ctx, groupID, userIDs)
|
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := g.groupMemberDB.Delete(ctx, groupID, userIDs, tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.delGroupMemberCache(ctx, groupID, userIDs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string][]string, error) {
|
func (g *GroupDataBase) MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string][]string, error) {
|
||||||
@ -348,12 +401,23 @@ func (g *GroupDataBase) TransferGroupOwner(ctx context.Context, groupID string,
|
|||||||
if rowsAffected != 1 {
|
if rowsAffected != 1 {
|
||||||
return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "")
|
return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "")
|
||||||
}
|
}
|
||||||
|
if err := g.delGroupMemberCache(ctx, groupID, []string{oldOwnerUserID, newOwnerUserID}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) UpdateGroupMember(ctx context.Context, groupID, userID string, data map[string]any) error {
|
func (g *GroupDataBase) UpdateGroupMember(ctx context.Context, groupID, userID string, data map[string]any) error {
|
||||||
return g.groupMemberDB.Update(ctx, groupID, userID, data)
|
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := g.groupMemberDB.Update(ctx, groupID, userID, data, tx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := g.cache.DelGroupMemberInfo(ctx, groupID, userID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error {
|
func (g *GroupDataBase) CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user