Files
open-im-server/pkg/common/storage/cache/redis/friend.go
T

168 lines
5.7 KiB
Go
Raw Normal View History

package redis
2023-06-30 09:45:02 +08:00
import (
"context"
"time"
2024-03-10 10:24:20 +08:00
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/cache/cachekey"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/database"
"github.com/openimsdk/open-im-server/v3/pkg/common/storage/model"
2024-04-19 22:23:08 +08:00
"github.com/openimsdk/tools/utils/datautil"
2024-03-05 10:51:55 +08:00
"github.com/redis/go-redis/v9"
2023-06-30 09:45:02 +08:00
)
const (
friendExpireTime = time.Second * 60 * 60 * 12
2023-06-30 09:45:02 +08:00
)
// FriendCacheRedis is an implementation of the FriendCache interface using Redis.
2023-06-30 09:45:02 +08:00
type FriendCacheRedis struct {
cache.BatchDeleter
friendDB database.Friend
2023-06-30 09:45:02 +08:00
expireTime time.Duration
rcClient *rocksCacheClient
syncCount int
2023-06-30 09:45:02 +08:00
}
// NewFriendCacheRedis creates a new instance of FriendCacheRedis.
func NewFriendCacheRedis(rdb redis.UniversalClient, localCache *config.LocalCache, friendDB database.Friend) cache.FriendCache {
rc := newRocksCacheClient(rdb)
2023-06-30 09:45:02 +08:00
return &FriendCacheRedis{
BatchDeleter: rc.GetBatchDeleter(localCache.Friend.Topic),
friendDB: friendDB,
expireTime: friendExpireTime,
rcClient: rc,
2023-06-30 09:45:02 +08:00
}
}
func (f *FriendCacheRedis) CloneFriendCache() cache.FriendCache {
2023-07-03 16:29:22 +08:00
return &FriendCacheRedis{
BatchDeleter: f.BatchDeleter.Clone(),
friendDB: f.friendDB,
expireTime: f.expireTime,
rcClient: f.rcClient,
2023-07-03 16:29:22 +08:00
}
2023-06-30 09:45:02 +08:00
}
// getFriendIDsKey returns the key for storing friend IDs in the cache.
2023-06-30 09:45:02 +08:00
func (f *FriendCacheRedis) getFriendIDsKey(ownerUserID string) string {
return cachekey.GetFriendIDsKey(ownerUserID)
2023-06-30 09:45:02 +08:00
}
func (f *FriendCacheRedis) getFriendMaxVersionKey(ownerUserID string) string {
return cachekey.GetFriendMaxVersionKey(ownerUserID)
}
// getTwoWayFriendsIDsKey returns the key for storing two-way friend IDs in the cache.
2023-06-30 09:45:02 +08:00
func (f *FriendCacheRedis) getTwoWayFriendsIDsKey(ownerUserID string) string {
return cachekey.GetTwoWayFriendsIDsKey(ownerUserID)
2023-06-30 09:45:02 +08:00
}
// getFriendKey returns the key for storing friend info in the cache.
2023-06-30 09:45:02 +08:00
func (f *FriendCacheRedis) getFriendKey(ownerUserID, friendUserID string) string {
return cachekey.GetFriendKey(ownerUserID, friendUserID)
2023-06-30 09:45:02 +08:00
}
// GetFriendIDs retrieves friend IDs from the cache or the database if not found.
2023-06-30 09:45:02 +08:00
func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
return getCache(ctx, f.rcClient, f.getFriendIDsKey(ownerUserID), f.expireTime, func(ctx context.Context) ([]string, error) {
return f.friendDB.FindFriendUserIDs(ctx, ownerUserID)
})
2023-06-30 09:45:02 +08:00
}
// DelFriendIDs deletes friend IDs from the cache.
func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) cache.FriendCache {
newFriendCache := f.CloneFriendCache()
2023-10-23 16:24:55 +08:00
keys := make([]string, 0, len(ownerUserIDs))
for _, userID := range ownerUserIDs {
2023-06-30 09:45:02 +08:00
keys = append(keys, f.getFriendIDsKey(userID))
}
newFriendCache.AddKeys(keys...)
2023-10-23 16:24:55 +08:00
return newFriendCache
2023-06-30 09:45:02 +08:00
}
// GetTwoWayFriendIDs retrieves two-way friend IDs from the cache.
func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) {
2023-06-30 09:45:02 +08:00
friendIDs, err := f.GetFriendIDs(ctx, ownerUserID)
if err != nil {
return nil, err
}
for _, friendID := range friendIDs {
friendFriendID, err := f.GetFriendIDs(ctx, friendID)
if err != nil {
return nil, err
}
2024-04-19 22:23:08 +08:00
if datautil.Contain(ownerUserID, friendFriendID...) {
2023-06-30 09:45:02 +08:00
twoWayFriendIDs = append(twoWayFriendIDs, ownerUserID)
}
}
2023-10-23 16:24:55 +08:00
2023-06-30 09:45:02 +08:00
return twoWayFriendIDs, nil
}
// DelTwoWayFriendIDs deletes two-way friend IDs from the cache.
func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) cache.FriendCache {
newFriendCache := f.CloneFriendCache()
2023-10-23 16:24:55 +08:00
newFriendCache.AddKeys(f.getTwoWayFriendsIDsKey(ownerUserID))
return newFriendCache
2023-06-30 09:45:02 +08:00
}
// GetFriend retrieves friend info from the cache or the database if not found.
func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *model.Friend, err error) {
2023-10-24 20:28:22 +08:00
return getCache(ctx, f.rcClient, f.getFriendKey(ownerUserID,
friendUserID), f.expireTime, func(ctx context.Context) (*model.Friend, error) {
return f.friendDB.Take(ctx, ownerUserID, friendUserID)
})
2023-06-30 09:45:02 +08:00
}
// DelFriend deletes friend info from the cache.
func (f *FriendCacheRedis) DelFriend(ownerUserID, friendUserID string) cache.FriendCache {
newFriendCache := f.CloneFriendCache()
2023-10-23 16:24:55 +08:00
newFriendCache.AddKeys(f.getFriendKey(ownerUserID, friendUserID))
return newFriendCache
2023-06-30 09:45:02 +08:00
}
2024-01-10 10:27:03 +08:00
// DelFriends deletes multiple friend infos from the cache.
func (f *FriendCacheRedis) DelFriends(ownerUserID string, friendUserIDs []string) cache.FriendCache {
newFriendCache := f.CloneFriendCache()
2024-01-10 10:27:03 +08:00
for _, friendUserID := range friendUserIDs {
key := f.getFriendKey(ownerUserID, friendUserID)
newFriendCache.AddKeys(key) // Assuming AddKeys marks the keys for deletion
}
return newFriendCache
}
func (f *FriendCacheRedis) DelOwner(friendUserID string, ownerUserIDs []string) cache.FriendCache {
newFriendCache := f.CloneFriendCache()
for _, ownerUserID := range ownerUserIDs {
key := f.getFriendKey(ownerUserID, friendUserID)
newFriendCache.AddKeys(key) // Assuming AddKeys marks the keys for deletion
}
return newFriendCache
}
func (f *FriendCacheRedis) DelMaxFriendVersion(ownerUserIDs ...string) cache.FriendCache {
newFriendCache := f.CloneFriendCache()
for _, ownerUserID := range ownerUserIDs {
key := f.getFriendMaxVersionKey(ownerUserID)
newFriendCache.AddKeys(key) // Assuming AddKeys marks the keys for deletion
}
return newFriendCache
}
func (f *FriendCacheRedis) FindMaxFriendVersion(ctx context.Context, ownerUserID string) (*model.VersionLog, error) {
return getCache(ctx, f.rcClient, f.getFriendMaxVersionKey(ownerUserID), f.expireTime, func(ctx context.Context) (*model.VersionLog, error) {
return f.friendDB.FindIncrVersion(ctx, ownerUserID, 0, 0)
})
}