diff --git a/pkg/rpccache/conversation.go b/pkg/rpccache/conversation.go index a0aba105a..b6e190bb9 100644 --- a/pkg/rpccache/conversation.go +++ b/pkg/rpccache/conversation.go @@ -10,13 +10,16 @@ import ( ) func NewConversationLocalCache(client rpcclient.ConversationRpcClient, cli redis.UniversalClient) *ConversationLocalCache { - return &ConversationLocalCache{ + lc := config.Config.LocalCache.Conversation + x := &ConversationLocalCache{ client: client, local: localcache.New[any]( - localcache.WithLocalSlotNum(config.Config.LocalCache.Conversation.SlotNum), - localcache.WithLocalSlotSize(config.Config.LocalCache.Conversation.SlotSize), + localcache.WithLocalSlotNum(lc.SlotNum), + localcache.WithLocalSlotSize(lc.SlotSize), ), } + go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal) + return x } type ConversationLocalCache struct { diff --git a/pkg/rpccache/friend.go b/pkg/rpccache/friend.go index fc5f0e96c..0e1faa78b 100644 --- a/pkg/rpccache/friend.go +++ b/pkg/rpccache/friend.go @@ -11,13 +11,16 @@ import ( ) func NewFriendLocalCache(client rpcclient.FriendRpcClient, cli redis.UniversalClient) *FriendLocalCache { - return &FriendLocalCache{ + lc := config.Config.LocalCache.Friend + x := &FriendLocalCache{ client: client, local: localcache.New[any]( - localcache.WithLocalSlotNum(config.Config.LocalCache.Friend.SlotNum), - localcache.WithLocalSlotSize(config.Config.LocalCache.Friend.SlotSize), + localcache.WithLocalSlotNum(lc.SlotNum), + localcache.WithLocalSlotSize(lc.SlotSize), ), } + go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal) + return x } type FriendLocalCache struct { diff --git a/pkg/rpccache/group.go b/pkg/rpccache/group.go index 333966fbb..6e5595a42 100644 --- a/pkg/rpccache/group.go +++ b/pkg/rpccache/group.go @@ -10,13 +10,16 @@ import ( ) func NewGroupLocalCache(client rpcclient.GroupRpcClient, cli redis.UniversalClient) *GroupLocalCache { - return &GroupLocalCache{ + lc := config.Config.LocalCache.Group + x := &GroupLocalCache{ client: client, local: localcache.New[any]( - localcache.WithLocalSlotNum(config.Config.LocalCache.Group.SlotNum), - localcache.WithLocalSlotSize(config.Config.LocalCache.Group.SlotSize), + localcache.WithLocalSlotNum(lc.SlotNum), + localcache.WithLocalSlotSize(lc.SlotSize), ), } + go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal) + return x } type GroupLocalCache struct { diff --git a/pkg/rpccache/subscriber.go b/pkg/rpccache/subscriber.go new file mode 100644 index 000000000..571ff6d2d --- /dev/null +++ b/pkg/rpccache/subscriber.go @@ -0,0 +1,23 @@ +package rpccache + +import ( + "context" + "encoding/json" + "github.com/OpenIMSDK/tools/log" + "github.com/redis/go-redis/v9" +) + +func subscriberRedisDeleteCache(ctx context.Context, client redis.UniversalClient, channel string, del func(ctx context.Context, key ...string)) { + for message := range client.Subscribe(ctx, channel).Channel() { + log.ZDebug(ctx, "subscriberRedisDeleteCache", "channel", channel, "payload", message.Payload) + var keys []string + if err := json.Unmarshal([]byte(message.Payload), &keys); err != nil { + log.ZError(ctx, "subscriberRedisDeleteCache json.Unmarshal error", err) + continue + } + if len(keys) == 0 { + continue + } + del(ctx, keys...) + } +}