mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-16 14:59:01 +08:00
feat: local cache
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
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/rpcclient"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func NewConversationLocalCache(client rpcclient.ConversationRpcClient, cli redis.UniversalClient) *ConversationLocalCache {
|
||||
return &ConversationLocalCache{
|
||||
local: localcache.New[any](localcache.WithRedisDeleteSubscribe(config.Config.LocalCache.Conversation.Topic, cli)),
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
type ConversationLocalCache struct {
|
||||
local localcache.Cache[any]
|
||||
client rpcclient.ConversationRpcClient
|
||||
}
|
||||
|
||||
func (c *ConversationLocalCache) GetConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) {
|
||||
return localcache.AnyValue[[]string](c.local.Get(ctx, cachekey.GetConversationIDsKey(ownerUserID), func(ctx context.Context) (any, error) {
|
||||
return c.client.GetConversationIDs(ctx, ownerUserID)
|
||||
}))
|
||||
}
|
||||
+14
-14
@@ -23,20 +23,20 @@ type FriendLocalCache struct {
|
||||
client rpcclient.FriendRpcClient
|
||||
}
|
||||
|
||||
func (f *FriendLocalCache) GetFriendIDs(ctx context.Context, ownerUserID string) (val []string, err error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache GetFriendIDs req", "ownerUserID", ownerUserID)
|
||||
defer func() {
|
||||
if err == nil {
|
||||
log.ZDebug(ctx, "FriendLocalCache GetFriendIDs return", "value", val)
|
||||
} else {
|
||||
log.ZError(ctx, "FriendLocalCache GetFriendIDs return", err)
|
||||
}
|
||||
}()
|
||||
return localcache.AnyValue[[]string](f.local.Get(ctx, cachekey.GetFriendIDsKey(ownerUserID), func(ctx context.Context) (any, error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache GetFriendIDs call rpc", "ownerUserID", ownerUserID)
|
||||
return f.client.GetFriendIDs(ctx, ownerUserID)
|
||||
}))
|
||||
}
|
||||
//func (f *FriendLocalCache) GetFriendIDs(ctx context.Context, ownerUserID string) (val []string, err error) {
|
||||
// log.ZDebug(ctx, "FriendLocalCache GetFriendIDs req", "ownerUserID", ownerUserID)
|
||||
// defer func() {
|
||||
// if err == nil {
|
||||
// log.ZDebug(ctx, "FriendLocalCache GetFriendIDs return", "value", val)
|
||||
// } else {
|
||||
// log.ZError(ctx, "FriendLocalCache GetFriendIDs return", err)
|
||||
// }
|
||||
// }()
|
||||
// return localcache.AnyValue[[]string](f.local.Get(ctx, cachekey.GetFriendIDsKey(ownerUserID), func(ctx context.Context) (any, error) {
|
||||
// log.ZDebug(ctx, "FriendLocalCache GetFriendIDs call rpc", "ownerUserID", ownerUserID)
|
||||
// return f.client.GetFriendIDs(ctx, ownerUserID)
|
||||
// }))
|
||||
//}
|
||||
|
||||
func (f *FriendLocalCache) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (val bool, err error) {
|
||||
log.ZDebug(ctx, "FriendLocalCache IsFriend req", "possibleFriendUserID", possibleFriendUserID, "userID", userID)
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
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/rpcclient"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func NewGroupLocalCache(client rpcclient.GroupRpcClient, cli redis.UniversalClient) *GroupLocalCache {
|
||||
return &GroupLocalCache{
|
||||
local: localcache.New[any](localcache.WithRedisDeleteSubscribe(config.Config.LocalCache.Group.Topic, cli)),
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
type GroupLocalCache struct {
|
||||
local localcache.Cache[any]
|
||||
client rpcclient.GroupRpcClient
|
||||
}
|
||||
|
||||
func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string) ([]string, error) {
|
||||
return localcache.AnyValue[[]string](g.local.Get(ctx, cachekey.GetGroupMemberIDsKey(groupID), func(ctx context.Context) (any, error) {
|
||||
return g.client.GetGroupMemberIDs(ctx, groupID)
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user