mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-08-29 15:45:53 +08:00
feat: local cache
This commit is contained in:
parent
abbb701192
commit
c9193302a8
@ -95,13 +95,13 @@ func (m *msgServer) messageVerification(ctx context.Context, data *msg.SendMsgRe
|
|||||||
data.MsgData.ContentType >= constant.NotificationBegin {
|
data.MsgData.ContentType >= constant.NotificationBegin {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// memberIDs, err := m.GroupLocalCache.GetGroupMemberIDs(ctx, data.MsgData.GroupID)
|
memberIDs, err := m.GroupLocalCache.GetGroupMemberIDMap(ctx, data.MsgData.GroupID)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
// if !utils.IsContain(data.MsgData.SendID, memberIDs) {
|
if _, ok := memberIDs[data.MsgData.SendID]; !ok {
|
||||||
// return errs.ErrNotInGroupYet.Wrap()
|
return errs.ErrNotInGroupYet.Wrap()
|
||||||
// }
|
}
|
||||||
|
|
||||||
groupMemberInfo, err := m.Group.GetGroupMemberCache(ctx, data.MsgData.GroupID, data.MsgData.SendID)
|
groupMemberInfo, err := m.Group.GetGroupMemberCache(ctx, data.MsgData.GroupID, data.MsgData.SendID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -107,15 +107,6 @@ func WithDeleteKeyBefore(fn func(ctx context.Context, key ...string)) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithDeleteLocal(fn func(fn func(key ...string))) Option {
|
|
||||||
if fn == nil {
|
|
||||||
panic("fn should not be nil")
|
|
||||||
}
|
|
||||||
return func(o *option) {
|
|
||||||
o.delCh = fn
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type emptyTarget struct{}
|
type emptyTarget struct{}
|
||||||
|
|
||||||
func (e emptyTarget) IncrGetHit() {}
|
func (e emptyTarget) IncrGetHit() {}
|
||||||
|
@ -24,8 +24,43 @@ type GroupLocalCache struct {
|
|||||||
local localcache.Cache[any]
|
local localcache.Cache[any]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
|
type listMap[V comparable] struct {
|
||||||
return localcache.AnyValue[[]string](g.local.Get(ctx, cachekey.GetGroupMemberIDsKey(groupID), func(ctx context.Context) (any, error) {
|
List []V
|
||||||
return g.client.GetGroupMemberIDs(ctx, groupID)
|
Map map[V]struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newListMap[V comparable](values []V, err error) (*listMap[V], error) {
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
lm := &listMap[V]{
|
||||||
|
List: values,
|
||||||
|
Map: make(map[V]struct{}, len(values)),
|
||||||
|
}
|
||||||
|
for _, value := range values {
|
||||||
|
lm.Map[value] = struct{}{}
|
||||||
|
}
|
||||||
|
return lm, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupLocalCache) getGroupMemberIDs(ctx context.Context, groupID string) (*listMap[string], error) {
|
||||||
|
return localcache.AnyValue[*listMap[string]](g.local.Get(ctx, cachekey.GetGroupMemberIDsKey(groupID), func(ctx context.Context) (any, error) {
|
||||||
|
return newListMap(g.client.GetGroupMemberIDs(ctx, groupID))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
|
||||||
|
res, err := g.getGroupMemberIDs(ctx, groupID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return res.List, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupLocalCache) GetGroupMemberIDMap(ctx context.Context, groupID string) (map[string]struct{}, error) {
|
||||||
|
res, err := g.getGroupMemberIDs(ctx, groupID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return res.Map, nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user