mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-05-05 17:56:50 +08:00
feat: local cache
This commit is contained in:
parent
c9193302a8
commit
ad5e4e1540
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
23
pkg/rpccache/subscriber.go
Normal file
23
pkg/rpccache/subscriber.go
Normal file
@ -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...)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user