mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-10 12:05:58 +08:00
cache
This commit is contained in:
@@ -120,6 +120,9 @@ func (g *groupDatabase) CreateGroup(ctx context.Context, groups []*relationTb.Gr
|
||||
return err
|
||||
}
|
||||
}
|
||||
createGroupIDs := utils.DistinctAnyGetComparable(groups, func(group *relationTb.GroupModel) string {
|
||||
return group.GroupID
|
||||
})
|
||||
m := make(map[string]struct{})
|
||||
var cache = g.cache.NewCache()
|
||||
for _, groupMember := range groupMembers {
|
||||
@@ -127,8 +130,9 @@ func (g *groupDatabase) CreateGroup(ctx context.Context, groups []*relationTb.Gr
|
||||
m[groupMember.GroupID] = struct{}{}
|
||||
cache = cache.DelGroupMemberIDs(groupMember.GroupID).DelGroupMembersHash(groupMember.GroupID).DelGroupsMemberNum(groupMember.GroupID)
|
||||
}
|
||||
cache.DelJoinedGroupID(groupMember.UserID).DelGroupMembersInfo(groupMember.GroupID, groupMember.UserID)
|
||||
cache = cache.DelJoinedGroupID(groupMember.UserID).DelGroupMembersInfo(groupMember.GroupID, groupMember.UserID)
|
||||
}
|
||||
cache = cache.DelGroupsInfo(createGroupIDs...)
|
||||
return cache.ExecDel(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -76,7 +76,17 @@ func (u *userDatabase) Find(ctx context.Context, userIDs []string) (users []*rel
|
||||
|
||||
// 插入多条 外部保证userID 不重复 且在db中不存在
|
||||
func (u *userDatabase) Create(ctx context.Context, users []*relation.UserModel) (err error) {
|
||||
return u.userDB.Create(ctx, users)
|
||||
return u.tx.Transaction(func(tx any) error {
|
||||
err = u.userDB.Create(ctx, users)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var userIDs []string
|
||||
for _, user := range users {
|
||||
userIDs = append(userIDs, user.UserID)
|
||||
}
|
||||
return u.cache.DelUsersInfo(userIDs...).ExecDel(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
// 更新(非零值) 外部保证userID存在
|
||||
|
||||
@@ -64,6 +64,20 @@ func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E {
|
||||
return v
|
||||
}
|
||||
|
||||
func DistinctAnyGetComparable[E any, K comparable](es []E, fn func(e E) K) []K {
|
||||
v := make([]K, 0, len(es))
|
||||
tmp := map[K]struct{}{}
|
||||
for i := 0; i < len(es); i++ {
|
||||
t := es[i]
|
||||
k := fn(t)
|
||||
if _, ok := tmp[k]; !ok {
|
||||
tmp[k] = struct{}{}
|
||||
v = append(v, k)
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Distinct 去重
|
||||
func Distinct[T comparable](ts []T) []T {
|
||||
return DistinctAny(ts, func(t T) T {
|
||||
|
||||
Reference in New Issue
Block a user