You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
5.5 KiB
154 lines
5.5 KiB
1 year ago
|
// Copyright © 2023 OpenIM. All rights reserved.
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
6 months ago
|
package redis
|
||
1 year ago
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/dtm-labs/rockscache"
|
||
8 months ago
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||
6 months ago
|
"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"
|
||
7 months ago
|
"github.com/openimsdk/tools/log"
|
||
|
"github.com/openimsdk/tools/utils/datautil"
|
||
9 months ago
|
"github.com/redis/go-redis/v9"
|
||
6 months ago
|
"time"
|
||
1 year ago
|
)
|
||
|
|
||
|
const (
|
||
9 months ago
|
friendExpireTime = time.Second * 60 * 60 * 12
|
||
1 year ago
|
)
|
||
|
|
||
12 months ago
|
// FriendCacheRedis is an implementation of the FriendCache interface using Redis.
|
||
1 year ago
|
type FriendCacheRedis struct {
|
||
6 months ago
|
cache.BatchDeleter
|
||
|
friendDB database.Friend
|
||
1 year ago
|
expireTime time.Duration
|
||
|
rcClient *rockscache.Client
|
||
|
}
|
||
|
|
||
12 months ago
|
// NewFriendCacheRedis creates a new instance of FriendCacheRedis.
|
||
6 months ago
|
func NewFriendCacheRedis(rdb redis.UniversalClient, localCache *config.LocalCache, friendDB database.Friend,
|
||
|
options *rockscache.Options) cache.FriendCache {
|
||
|
batchHandler := NewBatchDeleterRedis(rdb, options, []string{localCache.Friend.Topic})
|
||
7 months ago
|
f := localCache.Friend
|
||
9 months ago
|
log.ZDebug(context.Background(), "friend local cache init", "Topic", f.Topic, "SlotNum", f.SlotNum, "SlotSize", f.SlotSize, "enable", f.Enable())
|
||
1 year ago
|
return &FriendCacheRedis{
|
||
6 months ago
|
BatchDeleter: batchHandler,
|
||
|
friendDB: friendDB,
|
||
|
expireTime: friendExpireTime,
|
||
|
rcClient: rockscache.NewClient(rdb, *options),
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
6 months ago
|
func (f *FriendCacheRedis) CloneFriendCache() cache.FriendCache {
|
||
1 year ago
|
return &FriendCacheRedis{
|
||
6 months ago
|
BatchDeleter: f.BatchDeleter.Clone(),
|
||
|
friendDB: f.friendDB,
|
||
|
expireTime: f.expireTime,
|
||
|
rcClient: f.rcClient,
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// getFriendIDsKey returns the key for storing friend IDs in the cache.
|
||
1 year ago
|
func (f *FriendCacheRedis) getFriendIDsKey(ownerUserID string) string {
|
||
9 months ago
|
return cachekey.GetFriendIDsKey(ownerUserID)
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// getTwoWayFriendsIDsKey returns the key for storing two-way friend IDs in the cache.
|
||
1 year ago
|
func (f *FriendCacheRedis) getTwoWayFriendsIDsKey(ownerUserID string) string {
|
||
9 months ago
|
return cachekey.GetTwoWayFriendsIDsKey(ownerUserID)
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// getFriendKey returns the key for storing friend info in the cache.
|
||
1 year ago
|
func (f *FriendCacheRedis) getFriendKey(ownerUserID, friendUserID string) string {
|
||
9 months ago
|
return cachekey.GetFriendKey(ownerUserID, friendUserID)
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// GetFriendIDs retrieves friend IDs from the cache or the database if not found.
|
||
1 year ago
|
func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
|
||
1 year ago
|
return getCache(ctx, f.rcClient, f.getFriendIDsKey(ownerUserID), f.expireTime, func(ctx context.Context) ([]string, error) {
|
||
|
return f.friendDB.FindFriendUserIDs(ctx, ownerUserID)
|
||
|
})
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// DelFriendIDs deletes friend IDs from the cache.
|
||
6 months ago
|
func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) cache.FriendCache {
|
||
|
newFriendCache := f.CloneFriendCache()
|
||
1 year ago
|
keys := make([]string, 0, len(ownerUserIDs))
|
||
|
for _, userID := range ownerUserIDs {
|
||
1 year ago
|
keys = append(keys, f.getFriendIDsKey(userID))
|
||
|
}
|
||
6 months ago
|
newFriendCache.AddKeys(keys...)
|
||
1 year ago
|
|
||
6 months ago
|
return newFriendCache
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// GetTwoWayFriendIDs retrieves two-way friend IDs from the cache.
|
||
1 year ago
|
func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) {
|
||
1 year ago
|
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
|
||
|
}
|
||
7 months ago
|
if datautil.Contain(ownerUserID, friendFriendID...) {
|
||
1 year ago
|
twoWayFriendIDs = append(twoWayFriendIDs, ownerUserID)
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return twoWayFriendIDs, nil
|
||
|
}
|
||
|
|
||
12 months ago
|
// DelTwoWayFriendIDs deletes two-way friend IDs from the cache.
|
||
6 months ago
|
func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) cache.FriendCache {
|
||
|
newFriendCache := f.CloneFriendCache()
|
||
1 year ago
|
newFriendCache.AddKeys(f.getTwoWayFriendsIDsKey(ownerUserID))
|
||
|
|
||
|
return newFriendCache
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// GetFriend retrieves friend info from the cache or the database if not found.
|
||
6 months ago
|
func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *model.Friend, err error) {
|
||
1 year ago
|
return getCache(ctx, f.rcClient, f.getFriendKey(ownerUserID,
|
||
6 months ago
|
friendUserID), f.expireTime, func(ctx context.Context) (*model.Friend, error) {
|
||
1 year ago
|
return f.friendDB.Take(ctx, ownerUserID, friendUserID)
|
||
|
})
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
// DelFriend deletes friend info from the cache.
|
||
6 months ago
|
func (f *FriendCacheRedis) DelFriend(ownerUserID, friendUserID string) cache.FriendCache {
|
||
|
newFriendCache := f.CloneFriendCache()
|
||
1 year ago
|
newFriendCache.AddKeys(f.getFriendKey(ownerUserID, friendUserID))
|
||
|
|
||
|
return newFriendCache
|
||
1 year ago
|
}
|
||
10 months ago
|
|
||
|
// DelFriends deletes multiple friend infos from the cache.
|
||
6 months ago
|
func (f *FriendCacheRedis) DelFriends(ownerUserID string, friendUserIDs []string) cache.FriendCache {
|
||
|
newFriendCache := f.CloneFriendCache()
|
||
10 months ago
|
|
||
|
for _, friendUserID := range friendUserIDs {
|
||
|
key := f.getFriendKey(ownerUserID, friendUserID)
|
||
|
newFriendCache.AddKeys(key) // Assuming AddKeys marks the keys for deletion
|
||
|
}
|
||
|
|
||
|
return newFriendCache
|
||
|
}
|