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.
Open-IM-Server/pkg/common/db/cache/friend.go

123 lines
4.3 KiB

2 years ago
package cache
import (
"Open_IM/pkg/common/db/relation"
2 years ago
relationTb "Open_IM/pkg/common/db/table/relation"
2 years ago
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"context"
"encoding/json"
"github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
"time"
)
const (
2 years ago
friendExpireTime = time.Second * 60 * 60 * 12
friendIDsKey = "FRIEND_IDS:"
TwoWayFriendsIDsKey = "COMMON_FRIENDS_IDS:"
friendKey = "FRIEND_INFO:"
2 years ago
)
2 years ago
// args fn will exec when no data in cache
2 years ago
type FriendCache interface {
GetFriendIDs(ctx context.Context, ownerUserID string, fn func(ctx context.Context, ownerUserID string) (friendIDs []string, err error)) (friendIDs []string, err error)
// call when friendID List changed
DelFriendIDs(ctx context.Context, ownerUserID string) (err error)
2 years ago
// get single friendInfo from cache
2 years ago
GetFriend(ctx context.Context, ownerUserID, friendUserID string, fn func(ctx context.Context, ownerUserID, friendUserID string) (friend *relationTb.FriendModel, err error)) (friend *relationTb.FriendModel, err error)
2 years ago
// del friend when friend info changed
2 years ago
DelFriend(ctx context.Context, ownerUserID, friendUserID string) (err error)
}
type FriendCacheRedis struct {
2 years ago
friendDB *relation.FriendGorm
2 years ago
expireTime time.Duration
rcClient *rockscache.Client
}
2 years ago
func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB *relation.FriendGorm, options rockscache.Options) *FriendCacheRedis {
return &FriendCacheRedis{
2 years ago
friendDB: friendDB,
expireTime: friendExpireTime,
rcClient: rockscache.NewClient(rdb, options),
}
}
2 years ago
func (f *FriendCacheRedis) getFriendIDsKey(ownerUserID string) string {
2 years ago
return friendIDsKey + ownerUserID
}
2 years ago
func (f *FriendCacheRedis) getTwoWayFriendsIDsKey(ownerUserID string) string {
2 years ago
return TwoWayFriendsIDsKey + ownerUserID
}
2 years ago
func (f *FriendCacheRedis) getFriendKey(ownerUserID, friendUserID string) string {
2 years ago
return friendKey + ownerUserID + "-" + friendUserID
}
2 years ago
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)
})
2 years ago
}
2 years ago
func (f *FriendCacheRedis) DelFriendIDs(ctx context.Context, ownerUserID string) (err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID)
}()
2 years ago
return f.rcClient.TagAsDeleted(f.getFriendIDsKey(ownerUserID))
}
2 years ago
func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) {
2 years 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
}
if utils.IsContain(ownerUserID, friendFriendID) {
twoWayFriendIDs = append(twoWayFriendIDs, ownerUserID)
}
}
return twoWayFriendIDs, nil
}
2 years ago
func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) (err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID)
}()
return f.rcClient.TagAsDeleted(f.getTwoWayFriendsIDsKey(ownerUserID))
}
2 years ago
func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, friendUserID string, fn func(ctx context.Context, ownerUserID, friendUserID string) (friend *relationTb.FriendModel, err error)) (friend *relationTb.FriendModel, err error) {
2 years ago
getFriend := func() (string, error) {
friend, err = f.friendDB.Take(ctx, ownerUserID, friendUserID)
if err != nil {
return "", err
}
bytes, err := json.Marshal(friend)
if err != nil {
return "", utils.Wrap(err, "")
}
return string(bytes), nil
}
friendStr, err := f.rcClient.Fetch(f.getFriendKey(ownerUserID, friendUserID), f.expireTime, getFriend)
if err != nil {
return nil, err
}
2 years ago
friend = &relationTb.FriendModel{}
2 years ago
err = json.Unmarshal([]byte(friendStr), friend)
return friend, utils.Wrap(err, "")
}
2 years ago
func (f *FriendCacheRedis) DelFriend(ctx context.Context, ownerUserID, friendUserID string) (err error) {
2 years ago
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "ownerUserID", ownerUserID, "friendUserID", friendUserID)
}()
return f.rcClient.TagAsDeleted(f.getFriendKey(ownerUserID, friendUserID))
2 years ago
}