mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-25 19:22:46 +08:00
feat: msg local cache
This commit is contained in:
parent
0248fbb47d
commit
bbb5ef5ccc
deployments/templates
internal/rpc/msg
pkg
common
rpccache
@ -519,3 +519,10 @@ prometheus:
|
||||
rtcPrometheusPort: [ ${ RTC_PROM_PORT } ]
|
||||
thirdPrometheusPort: [ ${ THIRD_PROM_PORT } ]
|
||||
messageTransferPrometheusPort: [ ${ MSG_TRANSFER_PROM_PORT } ] # List of ports
|
||||
|
||||
localCache:
|
||||
friend:
|
||||
topic: "friend"
|
||||
slotNum: 500
|
||||
slotSize: 20000
|
||||
|
||||
|
@ -90,7 +90,7 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
|
||||
RegisterCenter: client,
|
||||
GroupLocalCache: localcache.NewGroupLocalCache(&groupRpcClient),
|
||||
ConversationLocalCache: localcache.NewConversationLocalCache(&conversationClient),
|
||||
friend: rpccache.NewFriendLocalCache(rpcclient.NewFriendRpcClient(client)),
|
||||
friend: rpccache.NewFriendLocalCache(rpcclient.NewFriendRpcClient(client), rdb),
|
||||
}
|
||||
s.notificationSender = rpcclient.NewNotificationSender(rpcclient.WithLocalSendMsg(s.SendMsg))
|
||||
s.addInterceptorHandler(MessageHasReadEnabled)
|
||||
|
@ -258,6 +258,8 @@ type configStruct struct {
|
||||
FriendVerify *bool `yaml:"friendVerify"`
|
||||
} `yaml:"messageVerify"`
|
||||
|
||||
LocalCache localCache `yaml:"localCache"`
|
||||
|
||||
IOSPush struct {
|
||||
PushSound string `yaml:"pushSound"`
|
||||
BadgeCount bool `yaml:"badgeCount"`
|
||||
@ -370,6 +372,16 @@ type notification struct {
|
||||
ConversationSetPrivate NotificationConf `yaml:"conversationSetPrivate"`
|
||||
}
|
||||
|
||||
type LocalCache struct {
|
||||
Topic string `yaml:"topic"`
|
||||
SlotNum int `yaml:"slotNum"`
|
||||
SlotSize int `yaml:"slotSize"`
|
||||
}
|
||||
|
||||
type localCache struct {
|
||||
Friend LocalCache `yaml:"friend"`
|
||||
}
|
||||
|
||||
func (c *configStruct) GetServiceNames() []string {
|
||||
return []string{
|
||||
c.RpcRegisterName.OpenImUserName,
|
||||
|
7
pkg/common/db/cache/black.go
vendored
7
pkg/common/db/cache/black.go
vendored
@ -17,6 +17,7 @@ package cache
|
||||
import (
|
||||
"context"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/cachekey"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"time"
|
||||
|
||||
"github.com/dtm-labs/rockscache"
|
||||
@ -53,11 +54,13 @@ func NewBlackCacheRedis(
|
||||
options rockscache.Options,
|
||||
) BlackCache {
|
||||
rcClient := rockscache.NewClient(rdb, options)
|
||||
|
||||
mc := NewMetaCacheRedis(rcClient)
|
||||
mc.SetTopic(config.Config.LocalCache.Friend.Topic)
|
||||
mc.SetRawRedisClient(rdb)
|
||||
return &BlackCacheRedis{
|
||||
expireTime: blackExpireTime,
|
||||
rcClient: rcClient,
|
||||
metaCache: NewMetaCacheRedis(rcClient),
|
||||
metaCache: mc,
|
||||
blackDB: blackDB,
|
||||
}
|
||||
}
|
||||
|
6
pkg/common/db/cache/friend.go
vendored
6
pkg/common/db/cache/friend.go
vendored
@ -17,6 +17,7 @@ package cache
|
||||
import (
|
||||
"context"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/cachekey"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"time"
|
||||
|
||||
"github.com/dtm-labs/rockscache"
|
||||
@ -59,8 +60,11 @@ type FriendCacheRedis struct {
|
||||
func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendModelInterface,
|
||||
options rockscache.Options) FriendCache {
|
||||
rcClient := rockscache.NewClient(rdb, options)
|
||||
mc := NewMetaCacheRedis(rcClient)
|
||||
mc.SetTopic(config.Config.LocalCache.Friend.Topic)
|
||||
mc.SetRawRedisClient(rdb)
|
||||
return &FriendCacheRedis{
|
||||
metaCache: NewMetaCacheRedis(rcClient),
|
||||
metaCache: mc,
|
||||
friendDB: friendDB,
|
||||
expireTime: friendExpireTime,
|
||||
rcClient: rcClient,
|
||||
|
46
pkg/common/db/cache/meta_cache.go
vendored
46
pkg/common/db/cache/meta_cache.go
vendored
@ -18,6 +18,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"time"
|
||||
|
||||
"github.com/OpenIMSDK/tools/mw/specialerror"
|
||||
@ -44,6 +45,8 @@ type metaCache interface {
|
||||
AddKeys(keys ...string)
|
||||
ClearKeys()
|
||||
GetPreDelKeys() []string
|
||||
SetTopic(topic string)
|
||||
SetRawRedisClient(cli redis.UniversalClient)
|
||||
}
|
||||
|
||||
func NewMetaCacheRedis(rcClient *rockscache.Client, keys ...string) metaCache {
|
||||
@ -51,10 +54,20 @@ func NewMetaCacheRedis(rcClient *rockscache.Client, keys ...string) metaCache {
|
||||
}
|
||||
|
||||
type metaCacheRedis struct {
|
||||
topic string
|
||||
rcClient *rockscache.Client
|
||||
keys []string
|
||||
maxRetryTimes int
|
||||
retryInterval time.Duration
|
||||
redisClient redis.UniversalClient
|
||||
}
|
||||
|
||||
func (m *metaCacheRedis) SetTopic(topic string) {
|
||||
m.topic = topic
|
||||
}
|
||||
|
||||
func (m *metaCacheRedis) SetRawRedisClient(cli redis.UniversalClient) {
|
||||
m.redisClient = cli
|
||||
}
|
||||
|
||||
func (m *metaCacheRedis) ExecDel(ctx context.Context, distinct ...bool) error {
|
||||
@ -72,31 +85,18 @@ func (m *metaCacheRedis) ExecDel(ctx context.Context, distinct ...bool) error {
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
//retryTimes := 0
|
||||
//for {
|
||||
// m.rcClient.TagAsDeleted()
|
||||
// if err := m.rcClient.TagAsDeletedBatch2(ctx, []string{key}); err != nil {
|
||||
// if retryTimes >= m.maxRetryTimes {
|
||||
// err = errs.ErrInternalServer.Wrap(
|
||||
// fmt.Sprintf(
|
||||
// "delete cache error: %v, keys: %v, retry times %d, please check redis server",
|
||||
// err,
|
||||
// key,
|
||||
// retryTimes,
|
||||
// ),
|
||||
// )
|
||||
// log.ZWarn(ctx, "delete cache failed, please handle keys", err, "keys", key)
|
||||
// return err
|
||||
// }
|
||||
// retryTimes++
|
||||
// } else {
|
||||
// break
|
||||
// }
|
||||
//}
|
||||
}
|
||||
if m.topic != "" && m.redisClient != nil {
|
||||
data, err := json.Marshal(m.keys)
|
||||
if err != nil {
|
||||
log.ZError(ctx, "keys json marshal failed", err, "topic", m.topic, "keys", m.keys)
|
||||
} else {
|
||||
if err := m.redisClient.Publish(ctx, m.topic, string(data)).Err(); err != nil {
|
||||
log.ZError(ctx, "redis publish cache delete error", err, "topic", m.topic, "keys", m.keys)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@ func WithRedisDeleteSubscribe(topic string, cli redis.UniversalClient) Option {
|
||||
}
|
||||
msg := cli.Subscribe(context.Background(), topic).Channel()
|
||||
for m := range msg {
|
||||
log.ZDebug(context.Background(), "WithRedisDeleteSubscribe delete", "topic", m.Channel, "payload", m.Payload)
|
||||
var key []string
|
||||
if err := json.Unmarshal([]byte(m.Payload), &key); err != nil {
|
||||
log.ZError(context.Background(), "WithRedisDeleteSubscribe json unmarshal error", err, "topic", topic, "payload", m.Payload)
|
||||
|
@ -33,11 +33,9 @@ type cache[V any] struct {
|
||||
|
||||
func (c *cache[V]) onEvict(key string, value V) {
|
||||
for k := range c.link.Del(key) {
|
||||
if key != k {
|
||||
c.local.Del(k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cache[V]) del(key ...string) {
|
||||
for _, k := range key {
|
||||
|
@ -3,14 +3,16 @@ package rpccache
|
||||
import (
|
||||
"context"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/cachekey"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/localcache"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/common/localcache/option"
|
||||
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func NewFriendLocalCache(client rpcclient.FriendRpcClient) *FriendLocalCache {
|
||||
func NewFriendLocalCache(client rpcclient.FriendRpcClient, cli redis.UniversalClient) *FriendLocalCache {
|
||||
return &FriendLocalCache{
|
||||
local: localcache.New[any](),
|
||||
local: localcache.New[any](localcache.WithRedisDeleteSubscribe(config.Config.LocalCache.Friend.Topic, cli)),
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user