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

42 lines
1.9 KiB
Go
Raw Normal View History

2024-01-08 15:39:39 +08:00
package rpccache
import (
"context"
"github.com/openimsdk/open-im-server/v3/pkg/common/cachekey"
2024-01-09 17:01:50 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
2024-01-08 15:39:39 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/localcache"
2024-01-08 20:36:41 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/localcache/option"
2024-01-08 15:39:39 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/rpcclient"
2024-01-09 17:01:50 +08:00
"github.com/redis/go-redis/v9"
2024-01-08 15:39:39 +08:00
)
2024-01-09 17:01:50 +08:00
func NewFriendLocalCache(client rpcclient.FriendRpcClient, cli redis.UniversalClient) *FriendLocalCache {
2024-01-08 15:39:39 +08:00
return &FriendLocalCache{
2024-01-09 17:01:50 +08:00
local: localcache.New[any](localcache.WithRedisDeleteSubscribe(config.Config.LocalCache.Friend.Topic, cli)),
2024-01-08 15:39:39 +08:00
client: client,
}
}
type FriendLocalCache struct {
local localcache.Cache[any]
client rpcclient.FriendRpcClient
}
func (f *FriendLocalCache) GetFriendIDs(ctx context.Context, ownerUserID string) ([]string, error) {
return localcache.AnyValue[[]string](f.local.Get(ctx, cachekey.GetFriendIDsKey(ownerUserID), func(ctx context.Context) (any, error) {
return f.client.GetFriendIDs(ctx, ownerUserID)
}))
}
func (f *FriendLocalCache) IsFriend(ctx context.Context, possibleFriendUserID, userID string) (bool, error) {
return localcache.AnyValue[bool](f.local.Get(ctx, cachekey.GetIsFriendKey(possibleFriendUserID, userID), func(ctx context.Context) (any, error) {
return f.client.IsFriend(ctx, possibleFriendUserID, userID)
2024-01-08 20:36:41 +08:00
}, option.NewOption().WithLink(cachekey.GetFriendIDsKey(possibleFriendUserID), cachekey.GetFriendIDsKey(userID))))
2024-01-08 15:39:39 +08:00
}
func (f *FriendLocalCache) IsBlocked(ctx context.Context, possibleBlackUserID, userID string) (bool, error) {
return localcache.AnyValue[bool](f.local.Get(ctx, cachekey.GetIsBlackIDsKey(possibleBlackUserID, userID), func(ctx context.Context) (any, error) {
2024-01-08 20:36:41 +08:00
return f.client.IsBlocked(ctx, possibleBlackUserID, userID)
}, option.NewOption().WithLink(cachekey.GetBlackIDsKey(possibleBlackUserID), cachekey.GetBlackIDsKey(userID))))
2024-01-08 15:39:39 +08:00
}