feat: local cache

This commit is contained in:
withchao 2024-01-16 10:20:57 +08:00
parent 6e1f96a720
commit d26a1b2090
4 changed files with 29 additions and 22 deletions

20
pkg/rpccache/common.go Normal file
View File

@ -0,0 +1,20 @@
package rpccache
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
}
type listMap[V comparable] struct {
List []V
Map map[V]struct{}
}

View File

@ -19,7 +19,9 @@ func NewConversationLocalCache(client rpcclient.ConversationRpcClient, cli redis
localcache.WithLocalSlotSize(lc.SlotSize),
),
}
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
if lc.Enable() {
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
}
return x
}

View File

@ -19,7 +19,9 @@ func NewFriendLocalCache(client rpcclient.FriendRpcClient, cli redis.UniversalCl
localcache.WithLocalSlotSize(lc.SlotSize),
),
}
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
if lc.Enable() {
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
}
return x
}

View File

@ -19,7 +19,9 @@ func NewGroupLocalCache(client rpcclient.GroupRpcClient, cli redis.UniversalClie
localcache.WithLocalSlotSize(lc.SlotSize),
),
}
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
if lc.Enable() {
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
}
return x
}
@ -28,25 +30,6 @@ type GroupLocalCache struct {
local localcache.Cache[any]
}
type listMap[V comparable] struct {
List []V
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) (val *listMap[string], err error) {
log.ZDebug(ctx, "GroupLocalCache getGroupMemberIDs req", "groupID", groupID)
defer func() {