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.
304 lines
8.7 KiB
304 lines
8.7 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"
|
||
1 year ago
|
"encoding/json"
|
||
1 year ago
|
"errors"
|
||
1 year ago
|
"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/protocol/constant"
|
||
|
"github.com/openimsdk/protocol/user"
|
||
|
"github.com/openimsdk/tools/errs"
|
||
|
"github.com/openimsdk/tools/log"
|
||
9 months ago
|
"github.com/redis/go-redis/v9"
|
||
6 months ago
|
"hash/crc32"
|
||
|
"strconv"
|
||
|
"time"
|
||
1 year ago
|
)
|
||
|
|
||
|
const (
|
||
7 months ago
|
userExpireTime = time.Second * 60 * 60 * 12
|
||
1 year ago
|
userOlineStatusExpireTime = time.Second * 60 * 60 * 24
|
||
1 year ago
|
statusMod = 501
|
||
1 year ago
|
)
|
||
|
|
||
|
type UserCacheRedis struct {
|
||
6 months ago
|
cache.BatchDeleter
|
||
|
rdb redis.UniversalClient
|
||
|
userDB database.User
|
||
1 year ago
|
expireTime time.Duration
|
||
|
rcClient *rockscache.Client
|
||
|
}
|
||
|
|
||
6 months ago
|
func NewUserCacheRedis(rdb redis.UniversalClient, localCache *config.LocalCache, userDB database.User, options *rockscache.Options) cache.UserCache {
|
||
|
batchHandler := NewBatchDeleterRedis(rdb, options, []string{localCache.User.Topic})
|
||
7 months ago
|
u := localCache.User
|
||
9 months ago
|
log.ZDebug(context.Background(), "user local cache init", "Topic", u.Topic, "SlotNum", u.SlotNum, "SlotSize", u.SlotSize, "enable", u.Enable())
|
||
1 year ago
|
return &UserCacheRedis{
|
||
6 months ago
|
BatchDeleter: batchHandler,
|
||
|
rdb: rdb,
|
||
|
userDB: userDB,
|
||
|
expireTime: userExpireTime,
|
||
|
rcClient: rockscache.NewClient(rdb, *options),
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
6 months ago
|
func (u *UserCacheRedis) getOnlineStatusKey(modKey string) string {
|
||
|
return cachekey.GetOnlineStatusKey(modKey)
|
||
|
}
|
||
|
|
||
|
func (u *UserCacheRedis) CloneUserCache() cache.UserCache {
|
||
1 year ago
|
return &UserCacheRedis{
|
||
6 months ago
|
BatchDeleter: u.BatchDeleter.Clone(),
|
||
|
rdb: u.rdb,
|
||
|
userDB: u.userDB,
|
||
|
expireTime: u.expireTime,
|
||
|
rcClient: u.rcClient,
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
|
func (u *UserCacheRedis) getUserInfoKey(userID string) string {
|
||
9 months ago
|
return cachekey.GetUserInfoKey(userID)
|
||
1 year ago
|
}
|
||
|
|
||
|
func (u *UserCacheRedis) getUserGlobalRecvMsgOptKey(userID string) string {
|
||
9 months ago
|
return cachekey.GetUserGlobalRecvMsgOptKey(userID)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userInfo *model.User, err error) {
|
||
|
return getCache(ctx, u.rcClient, u.getUserInfoKey(userID), u.expireTime, func(ctx context.Context) (*model.User, error) {
|
||
12 months ago
|
return u.userDB.Take(ctx, userID)
|
||
9 months ago
|
})
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*model.User, error) {
|
||
|
return batchGetCache(ctx, u.rcClient, u.expireTime, userIDs, func(userID string) string {
|
||
1 year ago
|
return u.getUserInfoKey(userID)
|
||
6 months ago
|
}, func(ctx context.Context, userID string) (*model.User, error) {
|
||
1 year ago
|
return u.userDB.Take(ctx, userID)
|
||
|
})
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (u *UserCacheRedis) DelUsersInfo(userIDs ...string) cache.UserCache {
|
||
1 year ago
|
keys := make([]string, 0, len(userIDs))
|
||
1 year ago
|
for _, userID := range userIDs {
|
||
|
keys = append(keys, u.getUserInfoKey(userID))
|
||
|
}
|
||
6 months ago
|
cache := u.CloneUserCache()
|
||
1 year ago
|
cache.AddKeys(keys...)
|
||
1 year ago
|
|
||
1 year ago
|
return cache
|
||
|
}
|
||
|
|
||
|
func (u *UserCacheRedis) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
|
||
1 year ago
|
return getCache(
|
||
|
ctx,
|
||
|
u.rcClient,
|
||
|
u.getUserGlobalRecvMsgOptKey(userID),
|
||
|
u.expireTime,
|
||
|
func(ctx context.Context) (int, error) {
|
||
|
return u.userDB.GetUserGlobalRecvMsgOpt(ctx, userID)
|
||
|
},
|
||
|
)
|
||
1 year ago
|
}
|
||
|
|
||
6 months ago
|
func (u *UserCacheRedis) DelUsersGlobalRecvMsgOpt(userIDs ...string) cache.UserCache {
|
||
1 year ago
|
keys := make([]string, 0, len(userIDs))
|
||
1 year ago
|
for _, userID := range userIDs {
|
||
|
keys = append(keys, u.getUserGlobalRecvMsgOptKey(userID))
|
||
|
}
|
||
6 months ago
|
cache := u.CloneUserCache()
|
||
1 year ago
|
cache.AddKeys(keys...)
|
||
1 year ago
|
|
||
1 year ago
|
return cache
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// GetUserStatus get user status.
|
||
1 year ago
|
func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
||
1 year ago
|
userStatus := make([]*user.OnlineStatus, 0, len(userIDs))
|
||
1 year ago
|
for _, userID := range userIDs {
|
||
1 year ago
|
UserIDNum := crc32.ChecksumIEEE([]byte(userID))
|
||
1 year ago
|
modKey := strconv.Itoa(int(UserIDNum % statusMod))
|
||
1 year ago
|
var onlineStatus user.OnlineStatus
|
||
6 months ago
|
key := u.getOnlineStatusKey(modKey)
|
||
1 year ago
|
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
||
|
if err != nil {
|
||
1 year ago
|
if errors.Is(err, redis.Nil) {
|
||
1 year ago
|
// key or field does not exist
|
||
1 year ago
|
userStatus = append(userStatus, &user.OnlineStatus{
|
||
1 year ago
|
UserID: userID,
|
||
|
Status: constant.Offline,
|
||
|
PlatformIDs: nil,
|
||
1 year ago
|
})
|
||
1 year ago
|
|
||
1 year ago
|
continue
|
||
|
} else {
|
||
1 year ago
|
return nil, errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
}
|
||
|
err = json.Unmarshal([]byte(result), &onlineStatus)
|
||
|
if err != nil {
|
||
1 year ago
|
return nil, errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
onlineStatus.UserID = userID
|
||
1 year ago
|
onlineStatus.Status = constant.Online
|
||
1 year ago
|
userStatus = append(userStatus, &onlineStatus)
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
|
return userStatus, nil
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
// SetUserStatus Set the user status and save it in redis.
|
||
1 year ago
|
func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, status, platformID int32) error {
|
||
|
UserIDNum := crc32.ChecksumIEEE([]byte(userID))
|
||
|
modKey := strconv.Itoa(int(UserIDNum % statusMod))
|
||
6 months ago
|
key := u.getOnlineStatusKey(modKey)
|
||
12 months ago
|
log.ZDebug(ctx, "SetUserStatus args", "userID", userID, "status", status, "platformID", platformID, "modKey", modKey, "key", key)
|
||
1 year ago
|
isNewKey, err := u.rdb.Exists(ctx, key).Result()
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
|
if isNewKey == 0 {
|
||
|
if status == constant.Online {
|
||
|
onlineStatus := user.OnlineStatus{
|
||
|
UserID: userID,
|
||
|
Status: constant.Online,
|
||
|
PlatformIDs: []int32{platformID},
|
||
|
}
|
||
9 months ago
|
jsonData, err := json.Marshal(&onlineStatus)
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
1 year ago
|
}
|
||
9 months ago
|
_, err = u.rdb.HSet(ctx, key, userID, string(jsonData)).Result()
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
u.rdb.Expire(ctx, key, userOlineStatusExpireTime)
|
||
1 year ago
|
|
||
1 year ago
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
isNil := false
|
||
|
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
||
|
if err != nil {
|
||
1 year ago
|
if errors.Is(err, redis.Nil) {
|
||
1 year ago
|
isNil = true
|
||
|
} else {
|
||
1 year ago
|
return errs.Wrap(err)
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
if status == constant.Offline {
|
||
1 year ago
|
err = u.refreshStatusOffline(ctx, userID, status, platformID, isNil, err, result, key)
|
||
|
if err != nil {
|
||
|
return err
|
||
1 year ago
|
}
|
||
1 year ago
|
} else {
|
||
|
err = u.refreshStatusOnline(ctx, userID, platformID, isNil, err, result, key)
|
||
1 year ago
|
if err != nil {
|
||
1 year ago
|
return errs.Wrap(err)
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (u *UserCacheRedis) refreshStatusOffline(ctx context.Context, userID string, status, platformID int32, isNil bool, err error, result, key string) error {
|
||
|
if isNil {
|
||
|
log.ZWarn(ctx, "this user not online,maybe trigger order not right",
|
||
|
err, "userStatus", status)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
var onlineStatus user.OnlineStatus
|
||
|
err = json.Unmarshal([]byte(result), &onlineStatus)
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
|
var newPlatformIDs []int32
|
||
|
for _, val := range onlineStatus.PlatformIDs {
|
||
|
if val != platformID {
|
||
|
newPlatformIDs = append(newPlatformIDs, val)
|
||
1 year ago
|
}
|
||
1 year ago
|
}
|
||
|
if newPlatformIDs == nil {
|
||
|
_, err = u.rdb.HDel(ctx, key, userID).Result()
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
} else {
|
||
1 year ago
|
onlineStatus.PlatformIDs = newPlatformIDs
|
||
1 year ago
|
newjsonData, err := json.Marshal(&onlineStatus)
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
|
_, err = u.rdb.HSet(ctx, key, userID, string(newjsonData)).Result()
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
func (u *UserCacheRedis) refreshStatusOnline(ctx context.Context, userID string, platformID int32, isNil bool, err error, result, key string) error {
|
||
|
var onlineStatus user.OnlineStatus
|
||
|
if !isNil {
|
||
9 months ago
|
err := json.Unmarshal([]byte(result), &onlineStatus)
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
1 year ago
|
}
|
||
|
onlineStatus.PlatformIDs = RemoveRepeatedElementsInList(append(onlineStatus.PlatformIDs, platformID))
|
||
|
} else {
|
||
|
onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID)
|
||
|
}
|
||
|
onlineStatus.Status = constant.Online
|
||
|
onlineStatus.UserID = userID
|
||
|
newjsonData, err := json.Marshal(&onlineStatus)
|
||
|
if err != nil {
|
||
7 months ago
|
return errs.WrapMsg(err, "json.Marshal failed")
|
||
1 year ago
|
}
|
||
|
_, err = u.rdb.HSet(ctx, key, userID, string(newjsonData)).Result()
|
||
|
if err != nil {
|
||
|
return errs.Wrap(err)
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
return nil
|
||
|
}
|
||
1 year ago
|
|
||
|
type Comparable interface {
|
||
|
~int | ~string | ~float64 | ~int32
|
||
|
}
|
||
|
|
||
|
func RemoveRepeatedElementsInList[T Comparable](slc []T) []T {
|
||
|
var result []T
|
||
|
tempMap := map[T]struct{}{}
|
||
|
for _, e := range slc {
|
||
|
if _, found := tempMap[e]; !found {
|
||
|
tempMap[e] = struct{}{}
|
||
|
result = append(result, e)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return result
|
||
|
}
|