mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
cache
This commit is contained in:
parent
3fa1a5c485
commit
6b9353d7b5
@ -4,6 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime/debug"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp"
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
||||||
@ -11,8 +14,6 @@ import (
|
|||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"runtime/debug"
|
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrConnClosed = errors.New("conn has closed")
|
var ErrConnClosed = errors.New("conn has closed")
|
||||||
@ -160,7 +161,7 @@ func (c *Client) handleMessage(message []byte) error {
|
|||||||
case WsSetBackgroundStatus:
|
case WsSetBackgroundStatus:
|
||||||
resp, messageErr = c.setAppBackgroundStatus(ctx, binaryReq)
|
resp, messageErr = c.setAppBackgroundStatus(ctx, binaryReq)
|
||||||
default:
|
default:
|
||||||
return errors.New(fmt.Sprintf("ReqIdentifier failed,sendID:%d,msgIncr:%s,reqIdentifier:%s", binaryReq.SendID, binaryReq.MsgIncr, binaryReq.ReqIdentifier))
|
return fmt.Errorf("ReqIdentifier failed,sendID:%s,msgIncr:%s,reqIdentifier:%d", binaryReq.SendID, binaryReq.MsgIncr, binaryReq.ReqIdentifier)
|
||||||
}
|
}
|
||||||
c.replyMessage(ctx, &binaryReq, messageErr, resp)
|
c.replyMessage(ctx, &binaryReq, messageErr, resp)
|
||||||
return nil
|
return nil
|
||||||
|
@ -120,6 +120,9 @@ func (g *groupDatabase) CreateGroup(ctx context.Context, groups []*relationTb.Gr
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
createGroupIDs := utils.DistinctAnyGetComparable(groups, func(group *relationTb.GroupModel) string {
|
||||||
|
return group.GroupID
|
||||||
|
})
|
||||||
m := make(map[string]struct{})
|
m := make(map[string]struct{})
|
||||||
var cache = g.cache.NewCache()
|
var cache = g.cache.NewCache()
|
||||||
for _, groupMember := range groupMembers {
|
for _, groupMember := range groupMembers {
|
||||||
@ -127,8 +130,9 @@ func (g *groupDatabase) CreateGroup(ctx context.Context, groups []*relationTb.Gr
|
|||||||
m[groupMember.GroupID] = struct{}{}
|
m[groupMember.GroupID] = struct{}{}
|
||||||
cache = cache.DelGroupMemberIDs(groupMember.GroupID).DelGroupMembersHash(groupMember.GroupID).DelGroupsMemberNum(groupMember.GroupID)
|
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)
|
return cache.ExecDel(ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,17 @@ func (u *userDatabase) Find(ctx context.Context, userIDs []string) (users []*rel
|
|||||||
|
|
||||||
// 插入多条 外部保证userID 不重复 且在db中不存在
|
// 插入多条 外部保证userID 不重复 且在db中不存在
|
||||||
func (u *userDatabase) Create(ctx context.Context, users []*relation.UserModel) (err error) {
|
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存在
|
// 更新(非零值) 外部保证userID存在
|
||||||
|
@ -64,6 +64,20 @@ func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E {
|
|||||||
return v
|
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 去重
|
// Distinct 去重
|
||||||
func Distinct[T comparable](ts []T) []T {
|
func Distinct[T comparable](ts []T) []T {
|
||||||
return DistinctAny(ts, func(t T) T {
|
return DistinctAny(ts, func(t T) T {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user