mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
Fix bug 755 (#758)
* Resolving code conflicts after project directory changes and Add subscribe and unsubscribe mongodb operations * Organize and update module dependencies * Get user online status * Get user online status * Get user online status * fix-bug-755 * Modify statusmod to a prime number to reduce hash conflicts
This commit is contained in:
parent
d9dbd739ab
commit
f592a3abd7
28
pkg/common/db/cache/user.go
vendored
28
pkg/common/db/cache/user.go
vendored
@ -18,6 +18,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/OpenIMSDK/protocol/user"
|
"github.com/OpenIMSDK/protocol/user"
|
||||||
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
|
"hash/crc32"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -33,7 +35,7 @@ const (
|
|||||||
userGlobalRecvMsgOptKey = "USER_GLOBAL_RECV_MSG_OPT_KEY:"
|
userGlobalRecvMsgOptKey = "USER_GLOBAL_RECV_MSG_OPT_KEY:"
|
||||||
olineStatusKey = "ONLINE_STATUS:"
|
olineStatusKey = "ONLINE_STATUS:"
|
||||||
userOlineStatusExpireTime = time.Second * 60 * 60 * 24
|
userOlineStatusExpireTime = time.Second * 60 * 60 * 24
|
||||||
statusMod = 500
|
statusMod = 501
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserCache interface {
|
type UserCache interface {
|
||||||
@ -165,11 +167,8 @@ func (u *UserCacheRedis) getOnlineStatusKey(userID string) string {
|
|||||||
func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
||||||
var res []*user.OnlineStatus
|
var res []*user.OnlineStatus
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
UserIDNum, err := strconv.Atoi(userID)
|
UserIDNum := crc32.ChecksumIEEE([]byte(userID))
|
||||||
if err != nil {
|
var modKey = strconv.Itoa(int(UserIDNum % statusMod))
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var modKey = strconv.Itoa(UserIDNum % statusMod)
|
|
||||||
var onlineStatus user.OnlineStatus
|
var onlineStatus user.OnlineStatus
|
||||||
key := olineStatusKey + modKey
|
key := olineStatusKey + modKey
|
||||||
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
||||||
@ -183,12 +182,12 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([
|
|||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
return nil, err
|
return nil, errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = json.Unmarshal([]byte(result), &onlineStatus)
|
err = json.Unmarshal([]byte(result), &onlineStatus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, errs.Wrap(err)
|
||||||
}
|
}
|
||||||
onlineStatus.UserID = userID
|
onlineStatus.UserID = userID
|
||||||
res = append(res, &onlineStatus)
|
res = append(res, &onlineStatus)
|
||||||
@ -200,23 +199,20 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([
|
|||||||
func (u *UserCacheRedis) SetUserStatus(ctx context.Context, list []*user.OnlineStatus) error {
|
func (u *UserCacheRedis) SetUserStatus(ctx context.Context, list []*user.OnlineStatus) error {
|
||||||
for _, status := range list {
|
for _, status := range list {
|
||||||
var isNewKey int64
|
var isNewKey int64
|
||||||
UserIDNum, err := strconv.Atoi(status.UserID)
|
UserIDNum := crc32.ChecksumIEEE([]byte(status.UserID))
|
||||||
if err != nil {
|
var modKey = strconv.Itoa(int(UserIDNum % statusMod))
|
||||||
return err
|
|
||||||
}
|
|
||||||
var modKey = strconv.Itoa(UserIDNum % statusMod)
|
|
||||||
key := olineStatusKey + modKey
|
key := olineStatusKey + modKey
|
||||||
jsonData, err := json.Marshal(status)
|
jsonData, err := json.Marshal(status)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
isNewKey, err = u.rdb.Exists(ctx, key).Result()
|
isNewKey, err = u.rdb.Exists(ctx, key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
_, err = u.rdb.HSet(ctx, key, status.UserID, string(jsonData)).Result()
|
_, err = u.rdb.HSet(ctx, key, status.UserID, string(jsonData)).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
if isNewKey > 0 {
|
if isNewKey > 0 {
|
||||||
u.rdb.Expire(ctx, key, userOlineStatusExpireTime)
|
u.rdb.Expire(ctx, key, userOlineStatusExpireTime)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user