Files
open-im-server/pkg/rpccache/conversation.go
T

47 lines
1.6 KiB
Go
Raw Normal View History

2024-01-12 15:41:05 +08:00
package rpccache
import (
"context"
2024-01-15 16:48:21 +08:00
"github.com/OpenIMSDK/tools/log"
2024-01-12 17:51:01 +08:00
"github.com/openimsdk/localcache"
2024-01-12 15:41:05 +08:00
"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/rpcclient"
"github.com/redis/go-redis/v9"
)
func NewConversationLocalCache(client rpcclient.ConversationRpcClient, cli redis.UniversalClient) *ConversationLocalCache {
2024-01-15 16:02:34 +08:00
lc := config.Config.LocalCache.Conversation
x := &ConversationLocalCache{
2024-01-12 15:41:05 +08:00
client: client,
2024-01-15 15:23:42 +08:00
local: localcache.New[any](
2024-01-15 16:02:34 +08:00
localcache.WithLocalSlotNum(lc.SlotNum),
localcache.WithLocalSlotSize(lc.SlotSize),
2024-01-15 15:23:42 +08:00
),
2024-01-12 15:41:05 +08:00
}
2024-01-16 10:20:57 +08:00
if lc.Enable() {
go subscriberRedisDeleteCache(context.Background(), cli, lc.Topic, x.local.DelLocal)
}
2024-01-15 16:02:34 +08:00
return x
2024-01-12 15:41:05 +08:00
}
type ConversationLocalCache struct {
client rpcclient.ConversationRpcClient
2024-01-15 15:23:42 +08:00
local localcache.Cache[any]
2024-01-12 15:41:05 +08:00
}
2024-01-15 16:48:21 +08:00
func (c *ConversationLocalCache) GetConversationIDs(ctx context.Context, ownerUserID string) (val []string, err error) {
log.ZDebug(ctx, "ConversationLocalCache GetConversationIDs req", "ownerUserID", ownerUserID)
defer func() {
if err == nil {
log.ZDebug(ctx, "ConversationLocalCache GetConversationIDs return", "value", val)
} else {
log.ZError(ctx, "ConversationLocalCache GetConversationIDs return", err)
}
}()
2024-01-12 15:41:05 +08:00
return localcache.AnyValue[[]string](c.local.Get(ctx, cachekey.GetConversationIDsKey(ownerUserID), func(ctx context.Context) (any, error) {
2024-01-15 16:48:21 +08:00
log.ZDebug(ctx, "ConversationLocalCache GetConversationIDs rpc", "ownerUserID", ownerUserID)
2024-01-12 15:41:05 +08:00
return c.client.GetConversationIDs(ctx, ownerUserID)
}))
}