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:
pluto 2023-08-03 09:44:10 +08:00 committed by GitHub
parent d9dbd739ab
commit f592a3abd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,6 +18,8 @@ import (
"context"
"encoding/json"
"github.com/OpenIMSDK/protocol/user"
"github.com/OpenIMSDK/tools/errs"
"hash/crc32"
"strconv"
"time"
@ -33,7 +35,7 @@ const (
userGlobalRecvMsgOptKey = "USER_GLOBAL_RECV_MSG_OPT_KEY:"
olineStatusKey = "ONLINE_STATUS:"
userOlineStatusExpireTime = time.Second * 60 * 60 * 24
statusMod = 500
statusMod = 501
)
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) {
var res []*user.OnlineStatus
for _, userID := range userIDs {
UserIDNum, err := strconv.Atoi(userID)
if err != nil {
return nil, err
}
var modKey = strconv.Itoa(UserIDNum % statusMod)
UserIDNum := crc32.ChecksumIEEE([]byte(userID))
var modKey = strconv.Itoa(int(UserIDNum % statusMod))
var onlineStatus user.OnlineStatus
key := olineStatusKey + modKey
result, err := u.rdb.HGet(ctx, key, userID).Result()
@ -183,12 +182,12 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([
})
continue
} else {
return nil, err
return nil, errs.Wrap(err)
}
}
err = json.Unmarshal([]byte(result), &onlineStatus)
if err != nil {
return nil, err
return nil, errs.Wrap(err)
}
onlineStatus.UserID = userID
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 {
for _, status := range list {
var isNewKey int64
UserIDNum, err := strconv.Atoi(status.UserID)
if err != nil {
return err
}
var modKey = strconv.Itoa(UserIDNum % statusMod)
UserIDNum := crc32.ChecksumIEEE([]byte(status.UserID))
var modKey = strconv.Itoa(int(UserIDNum % statusMod))
key := olineStatusKey + modKey
jsonData, err := json.Marshal(status)
if err != nil {
return err
return errs.Wrap(err)
}
isNewKey, err = u.rdb.Exists(ctx, key).Result()
if err != nil {
return err
return errs.Wrap(err)
}
_, err = u.rdb.HSet(ctx, key, status.UserID, string(jsonData)).Result()
if err != nil {
return err
return errs.Wrap(err)
}
if isNewKey > 0 {
u.rdb.Expire(ctx, key, userOlineStatusExpireTime)