mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-02 16:15:59 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
# Conflicts: # internal/rpc/msg/delete.go # internal/rpc/msg/send_pull.go # pkg/common/db/cache/msg.go # pkg/common/db/cache/redis.go # pkg/common/db/cache/redis_test.go
This commit is contained in:
Vendored
+3
-3
@@ -1,9 +1,9 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"OpenIM/pkg/common/db/relation"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
||||
Vendored
+4
-4
@@ -1,10 +1,10 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"OpenIM/pkg/common/db/relation"
|
||||
relationTb "OpenIM/pkg/common/db/table/relation"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/table/unrelation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"OpenIM/pkg/common/db/table/unrelation"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
"time"
|
||||
|
||||
Vendored
+4
-4
@@ -1,10 +1,10 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"OpenIM/pkg/common/db/relation"
|
||||
relationTb "OpenIM/pkg/common/db/table/relation"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
|
||||
Vendored
+4
-4
@@ -1,10 +1,10 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
unrelation2 "Open_IM/pkg/common/db/table/unrelation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
relationTb "OpenIM/pkg/common/db/table/relation"
|
||||
unrelation2 "OpenIM/pkg/common/db/table/unrelation"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
||||
Vendored
-475
@@ -1,475 +0,0 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
pbChat "Open_IM/pkg/proto/msg"
|
||||
pbRtc "Open_IM/pkg/proto/rtc"
|
||||
"Open_IM/pkg/proto/sdkws"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
const (
|
||||
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
|
||||
appleDeviceToken = "DEVICE_TOKEN"
|
||||
userMinSeq = "REDIS_USER_MIN_SEQ:"
|
||||
|
||||
getuiToken = "GETUI_TOKEN"
|
||||
getuiTaskID = "GETUI_TASK_ID"
|
||||
messageCache = "MESSAGE_CACHE:"
|
||||
signalCache = "SIGNAL_CACHE:"
|
||||
signalListCache = "SIGNAL_LIST_CACHE:"
|
||||
FcmToken = "FCM_TOKEN:"
|
||||
groupUserMinSeq = "GROUP_USER_MIN_SEQ:"
|
||||
groupMaxSeq = "GROUP_MAX_SEQ:"
|
||||
groupMinSeq = "GROUP_MIN_SEQ:"
|
||||
sendMsgFailedFlag = "SEND_MSG_FAILED_FLAG:"
|
||||
userBadgeUnreadCountSum = "USER_BADGE_UNREAD_COUNT_SUM:"
|
||||
exTypeKeyLocker = "EX_LOCK:"
|
||||
|
||||
uidPidToken = "UID_PID_TOKEN_STATUS:"
|
||||
|
||||
SignalListCache = "SIGNAL_LIST_CACHE:"
|
||||
|
||||
SignalCache = "SIGNAL_CACHE:"
|
||||
)
|
||||
|
||||
type MsgCache interface {
|
||||
IncrUserSeq(ctx context.Context, userID string) (int64, error)
|
||||
GetUserMaxSeq(ctx context.Context, userID string) (int64, error)
|
||||
SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error
|
||||
SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error)
|
||||
GetUserMinSeq(ctx context.Context, userID string) (int64, error)
|
||||
|
||||
SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error)
|
||||
GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error)
|
||||
GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
|
||||
GetGroupMinSeq(ctx context.Context, groupID string) (int64, error)
|
||||
IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
|
||||
SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error
|
||||
SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error
|
||||
|
||||
AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error
|
||||
|
||||
GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error)
|
||||
|
||||
SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error
|
||||
DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error
|
||||
GetMessageListBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
|
||||
SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) (int, error)
|
||||
DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error
|
||||
CleanUpOneUserAllMsg(ctx context.Context, userID string) error
|
||||
HandleSignalInfo(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error)
|
||||
GetSignalInfoFromCacheByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error)
|
||||
GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *pbRtc.SignalInviteReq, err error)
|
||||
DelUserSignalList(ctx context.Context, userID string) error
|
||||
DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error
|
||||
|
||||
SetGetuiToken(ctx context.Context, token string, expireTime int64) error
|
||||
GetGetuiToken(ctx context.Context) (string, error)
|
||||
SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error
|
||||
GetGetuiTaskID(ctx context.Context) (string, error)
|
||||
|
||||
SetSendMsgStatus(ctx context.Context, id string, status int32) error
|
||||
GetSendMsgStatus(ctx context.Context, id string) (int32, error)
|
||||
SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error)
|
||||
GetFcmToken(ctx context.Context, account string, platformID int) (string, error)
|
||||
DelFcmToken(ctx context.Context, account string, platformID int) error
|
||||
IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error)
|
||||
SetUserBadgeUnreadCountSum(ctx context.Context, userID string, value int) error
|
||||
GetUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error)
|
||||
JudgeMessageReactionEXISTS(ctx context.Context, clientMsgID string, sessionType int32) (bool, error)
|
||||
GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error)
|
||||
DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error
|
||||
SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error)
|
||||
GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error)
|
||||
SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error
|
||||
LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error
|
||||
UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error
|
||||
}
|
||||
|
||||
func NewMsgCache(client redis.UniversalClient) MsgCache {
|
||||
return &msgCache{rdb: client}
|
||||
}
|
||||
|
||||
type msgCache struct {
|
||||
rdb redis.UniversalClient
|
||||
}
|
||||
|
||||
func (m *msgCache) IncrUserSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userIncrSeq+userID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetUserMaxSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userIncrSeq+userID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userIncrSeq+userID, maxSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error) {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userMinSeq+userID, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetUserMinSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userMinSeq+userID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) {
|
||||
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMinSeq+groupID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMaxSeq+groupID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGroupMinSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMinSeq+groupID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
key := groupMaxSeq + groupID
|
||||
seq, err := m.rdb.Incr(ctx, key).Uint64()
|
||||
return int64(seq), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error {
|
||||
key := groupMaxSeq + groupID
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, maxSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error {
|
||||
key := groupMinSeq + groupID
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, key, token, flag).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error) {
|
||||
key := uidPidToken + userID + ":" + platformID
|
||||
r, err := m.rdb.HGetAll(ctx, key).Result()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
mm := make(map[string]int)
|
||||
for k, v := range r {
|
||||
mm[k] = utils.StringToInt(v)
|
||||
}
|
||||
return mm, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, r map[string]int) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
mm := make(map[string]interface{})
|
||||
for k, v := range r {
|
||||
mm[k] = v
|
||||
}
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, key, mm).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
return utils.Wrap1(m.rdb.HDel(ctx, key, fields...).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetMessageListBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error) {
|
||||
var errResult error
|
||||
for _, v := range seqList {
|
||||
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(v))
|
||||
result, err := m.rdb.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
errResult = err
|
||||
failedSeqList = append(failedSeqList, v)
|
||||
} else {
|
||||
msg := sdkws.MsgData{}
|
||||
err = jsonpb.UnmarshalString(result, &msg)
|
||||
if err != nil {
|
||||
errResult = err
|
||||
failedSeqList = append(failedSeqList, v)
|
||||
} else {
|
||||
seqMsg = append(seqMsg, &msg)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return seqMsg, failedSeqList, errResult
|
||||
}
|
||||
|
||||
func (m *msgCache) SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) (int, error) {
|
||||
pipe := m.rdb.Pipeline()
|
||||
var failedList []pbChat.MsgDataToMQ
|
||||
for _, msg := range msgList {
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(msg.MsgData.Seq))
|
||||
s, err := utils.Pb2String(msg.MsgData)
|
||||
if err != nil {
|
||||
return 0, utils.Wrap1(err)
|
||||
}
|
||||
err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
|
||||
if err != nil {
|
||||
return 0, utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
if len(failedList) != 0 {
|
||||
return len(failedList), errors.New(fmt.Sprintf("set msg to cache failed, failed lists: %q,%s", failedList, tracelog.GetOperationID(ctx)))
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
func (m *msgCache) DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error {
|
||||
for _, msg := range msgList {
|
||||
if err := m.rdb.Del(ctx, messageCache+userID+"_"+strconv.Itoa(int(msg.MsgData.Seq))).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *msgCache) CleanUpOneUserAllMsg(ctx context.Context, userID string) error {
|
||||
key := messageCache + userID + "_" + "*"
|
||||
vals, err := m.rdb.Keys(ctx, key).Result()
|
||||
if err == redis.Nil {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
for _, v := range vals {
|
||||
if err := m.rdb.Del(ctx, v).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *msgCache) HandleSignalInfo(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error) {
|
||||
req := &pbRtc.SignalReq{}
|
||||
if err := proto.Unmarshal(msg.Content, req); err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
var inviteeUserIDList []string
|
||||
var isInviteSignal bool
|
||||
switch signalInfo := req.Payload.(type) {
|
||||
case *pbRtc.SignalReq_Invite:
|
||||
inviteeUserIDList = signalInfo.Invite.Invitation.InviteeUserIDList
|
||||
isInviteSignal = true
|
||||
case *pbRtc.SignalReq_InviteInGroup:
|
||||
inviteeUserIDList = signalInfo.InviteInGroup.Invitation.InviteeUserIDList
|
||||
isInviteSignal = true
|
||||
if !utils.Contain(pushToUserID, inviteeUserIDList...) {
|
||||
return false, nil
|
||||
}
|
||||
case *pbRtc.SignalReq_HungUp, *pbRtc.SignalReq_Cancel, *pbRtc.SignalReq_Reject, *pbRtc.SignalReq_Accept:
|
||||
return false, utils.Wrap1(errors.New("signalInfo do not need offlinePush"))
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
if isInviteSignal {
|
||||
for _, userID := range inviteeUserIDList {
|
||||
timeout, err := strconv.Atoi(config.Config.Rtc.SignalTimeout)
|
||||
if err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
keyList := SignalListCache + userID
|
||||
err = m.rdb.LPush(ctx, keyList, msg.ClientMsgID).Err()
|
||||
if err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
err = m.rdb.Expire(ctx, keyList, time.Duration(timeout)*time.Second).Err()
|
||||
if err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
key := SignalCache + msg.ClientMsgID
|
||||
err = m.rdb.Set(ctx, key, msg.Content, time.Duration(timeout)*time.Second).Err()
|
||||
if err != nil {
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) GetSignalInfoFromCacheByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
||||
bytes, err := m.rdb.Get(ctx, SignalCache+clientMsgID).Bytes()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
req := &pbRtc.SignalReq{}
|
||||
if err = proto.Unmarshal(bytes, req); err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
invitationInfo = &pbRtc.SignalInviteReq{}
|
||||
switch req2 := req.Payload.(type) {
|
||||
case *pbRtc.SignalReq_Invite:
|
||||
invitationInfo.Invitation = req2.Invite.Invitation
|
||||
invitationInfo.OpUserID = req2.Invite.OpUserID
|
||||
case *pbRtc.SignalReq_InviteInGroup:
|
||||
invitationInfo.Invitation = req2.InviteInGroup.Invitation
|
||||
invitationInfo.OpUserID = req2.InviteInGroup.OpUserID
|
||||
}
|
||||
return invitationInfo, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
||||
key, err := m.rdb.LPop(ctx, SignalListCache+userID).Result()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
invitationInfo, err = m.GetSignalInfoFromCacheByClientMsgID(ctx, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return invitationInfo, m.DelUserSignalList(ctx, userID)
|
||||
}
|
||||
|
||||
func (m *msgCache) DelUserSignalList(ctx context.Context, userID string) error {
|
||||
return utils.Wrap1(m.rdb.Del(ctx, SignalListCache+userID).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error {
|
||||
for _, seq := range seqList {
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(seq))
|
||||
result, err := m.rdb.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
continue
|
||||
}
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
var msg sdkws.MsgData
|
||||
if err := jsonpb.UnmarshalString(result, &msg); err != nil {
|
||||
return err
|
||||
}
|
||||
msg.Status = constant.MsgDeleted
|
||||
s, err := utils.Pb2String(&msg)
|
||||
if err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
if err := m.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGetuiToken(ctx context.Context, token string, expireTime int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, getuiToken, token, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGetuiToken(ctx context.Context) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, getuiToken).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGetuiTaskID(ctx context.Context) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, getuiTaskID).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetSendMsgStatus(ctx context.Context, id string, status int32) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, sendMsgFailedFlag+id, status, time.Hour*24).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
|
||||
result, err := m.rdb.Get(ctx, sendMsgFailedFlag+id).Int()
|
||||
return int32(result), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (m *msgCache) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, FcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) DelFcmToken(ctx context.Context, account string, platformID int) error {
|
||||
return utils.Wrap1(m.rdb.Del(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||
seq, err := m.rdb.Incr(ctx, userBadgeUnreadCountSum+userID).Result()
|
||||
return int(seq), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (m *msgCache) SetUserBadgeUnreadCountSum(ctx context.Context, userID string, value int) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userBadgeUnreadCountSum+userID, value, 0).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userBadgeUnreadCountSum+userID).Int())
|
||||
}
|
||||
|
||||
func (m *msgCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||||
return utils.Wrap1(m.rdb.SetNX(ctx, key, 1, time.Minute).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||||
return utils.Wrap1(m.rdb.Del(ctx, key).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
|
||||
switch sessionType {
|
||||
case constant.SingleChatType:
|
||||
return "EX_SINGLE_" + clientMsgID
|
||||
case constant.GroupChatType:
|
||||
return "EX_GROUP_" + clientMsgID
|
||||
case constant.SuperGroupChatType:
|
||||
return "EX_SUPER_GROUP_" + clientMsgID
|
||||
case constant.NotificationChatType:
|
||||
return "EX_NOTIFICATION" + clientMsgID
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *msgCache) JudgeMessageReactionEXISTS(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
|
||||
n, err := m.rdb.Exists(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType)).Result()
|
||||
if err != nil {
|
||||
return false, utils.Wrap(err, "")
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
||||
return utils.Wrap2(m.rdb.Expire(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
||||
return utils.Wrap2(m.rdb.HGet(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
|
||||
return utils.Wrap2(m.rdb.HGetAll(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType)).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
|
||||
return utils.Wrap1(m.rdb.HDel(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), subKey).Err())
|
||||
}
|
||||
Vendored
+225
-291
@@ -1,21 +1,47 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/constant"
|
||||
pbChat "Open_IM/pkg/proto/msg"
|
||||
pbRtc "Open_IM/pkg/proto/rtc"
|
||||
"Open_IM/pkg/proto/sdkws"
|
||||
"Open_IM/pkg/utils"
|
||||
"OpenIM/pkg/common/config"
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
pbChat "OpenIM/pkg/proto/msg"
|
||||
pbRtc "OpenIM/pkg/proto/rtc"
|
||||
"OpenIM/pkg/proto/sdkws"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
"github.com/golang/protobuf/proto"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
const (
|
||||
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
|
||||
appleDeviceToken = "DEVICE_TOKEN"
|
||||
userMinSeq = "REDIS_USER_MIN_SEQ:"
|
||||
|
||||
getuiToken = "GETUI_TOKEN"
|
||||
getuiTaskID = "GETUI_TASK_ID"
|
||||
messageCache = "MESSAGE_CACHE:"
|
||||
signalCache = "SIGNAL_CACHE:"
|
||||
signalListCache = "SIGNAL_LIST_CACHE:"
|
||||
FcmToken = "FCM_TOKEN:"
|
||||
groupUserMinSeq = "GROUP_USER_MIN_SEQ:"
|
||||
groupMaxSeq = "GROUP_MAX_SEQ:"
|
||||
groupMinSeq = "GROUP_MIN_SEQ:"
|
||||
sendMsgFailedFlag = "SEND_MSG_FAILED_FLAG:"
|
||||
userBadgeUnreadCountSum = "USER_BADGE_UNREAD_COUNT_SUM:"
|
||||
exTypeKeyLocker = "EX_LOCK:"
|
||||
|
||||
uidPidToken = "UID_PID_TOKEN_STATUS:"
|
||||
|
||||
SignalListCache = "SIGNAL_LIST_CACHE:"
|
||||
|
||||
SignalCache = "SIGNAL_CACHE:"
|
||||
)
|
||||
|
||||
type Cache interface {
|
||||
@@ -28,6 +54,7 @@ type Cache interface {
|
||||
SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error)
|
||||
GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error)
|
||||
GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
|
||||
GetGroupMinSeq(ctx context.Context, groupID string) (int64, error)
|
||||
IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error)
|
||||
SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error
|
||||
SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error
|
||||
@@ -38,7 +65,7 @@ type Cache interface {
|
||||
|
||||
SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error
|
||||
DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error
|
||||
GetMessageListBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
|
||||
GetMessagesBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
|
||||
SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) (int, error)
|
||||
DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error
|
||||
CleanUpOneUserAllMsg(ctx context.Context, userID string) error
|
||||
@@ -53,8 +80,8 @@ type Cache interface {
|
||||
SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error
|
||||
GetGetuiTaskID(ctx context.Context) (string, error)
|
||||
|
||||
SetSendMsgStatus(ctx context.Context, status int32) error
|
||||
GetSendMsgStatus(ctx context.Context) (int, error)
|
||||
SetSendMsgStatus(ctx context.Context, id string, status int32) error
|
||||
GetSendMsgStatus(ctx context.Context, id string) (int32, error)
|
||||
SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error)
|
||||
GetFcmToken(ctx context.Context, account string, platformID int) (string, error)
|
||||
DelFcmToken(ctx context.Context, account string, platformID int) error
|
||||
@@ -71,298 +98,227 @@ type Cache interface {
|
||||
UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error
|
||||
}
|
||||
|
||||
// native redis operate
|
||||
|
||||
//func NewRedis() *RedisClient {
|
||||
// o := &RedisClient{}
|
||||
// o.InitRedis()
|
||||
// return o
|
||||
//}
|
||||
|
||||
func NewRedis() (*RedisClient, error) {
|
||||
var rdb redis.UniversalClient
|
||||
if config.Config.Redis.EnableCluster {
|
||||
rdb = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: config.Config.Redis.DBAddress,
|
||||
Username: config.Config.Redis.DBUserName,
|
||||
Password: config.Config.Redis.DBPassWord, // no password set
|
||||
PoolSize: 50,
|
||||
})
|
||||
//if err := rdb.Ping(ctx).Err();err != nil {
|
||||
// return nil, fmt.Errorf("redis ping %w", err)
|
||||
//}
|
||||
//return &RedisClient{rdb: rdb}, nil
|
||||
} else {
|
||||
rdb = redis.NewClient(&redis.Options{
|
||||
Addr: config.Config.Redis.DBAddress[0],
|
||||
Username: config.Config.Redis.DBUserName,
|
||||
Password: config.Config.Redis.DBPassWord, // no password set
|
||||
DB: 0, // use default DB
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
//err := rdb.Ping(ctx).Err()
|
||||
//if err != nil {
|
||||
// panic(err.Error() + " redis " + config.Config.Redis.DBAddress[0] + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
||||
//}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
||||
defer cancel()
|
||||
err := rdb.Ping(ctx).Err()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("redis ping %w", err)
|
||||
}
|
||||
return &RedisClient{rdb: rdb}, nil
|
||||
func NewMsgCache(client redis.UniversalClient) Cache {
|
||||
return &msgCache{rdb: client}
|
||||
}
|
||||
|
||||
type RedisClient struct {
|
||||
type msgCache struct {
|
||||
rdb redis.UniversalClient
|
||||
}
|
||||
|
||||
func NewRedisClient(rdb redis.UniversalClient) *RedisClient {
|
||||
return &RedisClient{rdb: rdb}
|
||||
func (m *msgCache) IncrUserSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userIncrSeq+userID).Int64())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetClient() redis.UniversalClient {
|
||||
return r.rdb
|
||||
func (m *msgCache) GetUserMaxSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userIncrSeq+userID).Int64())
|
||||
}
|
||||
|
||||
// Perform seq auto-increment operation of user messages
|
||||
func (r *RedisClient) IncrUserSeq(ctx context.Context, uid string) (int64, error) {
|
||||
key := userIncrSeq + uid
|
||||
seq, err := r.rdb.Incr(ctx, key).Result()
|
||||
return seq, err
|
||||
func (m *msgCache) SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userIncrSeq+userID, maxSeq, 0).Err())
|
||||
}
|
||||
|
||||
// Get the largest Seq
|
||||
func (r *RedisClient) GetUserMaxSeq(ctx context.Context, uid string) (int64, error) {
|
||||
key := userIncrSeq + uid
|
||||
seq, err := r.rdb.Get(ctx, key).Result()
|
||||
return int64(utils.StringToInt(seq)), err
|
||||
func (m *msgCache) SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error) {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userMinSeq+userID, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
// set the largest Seq
|
||||
func (r *RedisClient) SetUserMaxSeq(ctx context.Context, uid string, maxSeq int64) error {
|
||||
key := userIncrSeq + uid
|
||||
return r.rdb.Set(ctx, key, maxSeq, 0).Err()
|
||||
func (m *msgCache) GetUserMinSeq(ctx context.Context, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userMinSeq+userID).Int64())
|
||||
}
|
||||
|
||||
// Set the user's minimum seq
|
||||
func (r *RedisClient) SetUserMinSeq(ctx context.Context, uid string, minSeq int64) (err error) {
|
||||
key := userMinSeq + uid
|
||||
return r.rdb.Set(ctx, key, minSeq, 0).Err()
|
||||
}
|
||||
|
||||
// Get the smallest Seq
|
||||
func (r *RedisClient) GetUserMinSeq(ctx context.Context, uid string) (int64, error) {
|
||||
key := userMinSeq + uid
|
||||
seq, err := r.rdb.Get(ctx, key).Result()
|
||||
return int64(utils.StringToInt(seq)), err
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) {
|
||||
func (m *msgCache) SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) {
|
||||
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
|
||||
return r.rdb.Set(ctx, key, minSeq, 0).Err()
|
||||
}
|
||||
func (r *RedisClient) GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error) {
|
||||
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
|
||||
seq, err := r.rdb.Get(ctx, key).Result()
|
||||
return int64(utils.StringToInt(seq)), err
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
func (m *msgCache) GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMinSeq+groupID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMaxSeq+groupID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetGroupMinSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, groupMinSeq+groupID).Int64())
|
||||
}
|
||||
|
||||
func (m *msgCache) IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
key := groupMaxSeq + groupID
|
||||
seq, err := r.rdb.Get(ctx, key).Result()
|
||||
return int64(utils.StringToInt(seq)), err
|
||||
seq, err := m.rdb.Incr(ctx, key).Uint64()
|
||||
return int64(seq), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (r *RedisClient) IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
|
||||
func (m *msgCache) SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error {
|
||||
key := groupMaxSeq + groupID
|
||||
seq, err := r.rdb.Incr(ctx, key).Result()
|
||||
return seq, err
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, maxSeq, 0).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error {
|
||||
key := groupMaxSeq + groupID
|
||||
return r.rdb.Set(ctx, key, maxSeq, 0).Err()
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error {
|
||||
func (m *msgCache) SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error {
|
||||
key := groupMinSeq + groupID
|
||||
return r.rdb.Set(ctx, key, minSeq, 0).Err()
|
||||
return utils.Wrap1(m.rdb.Set(ctx, key, minSeq, 0).Err())
|
||||
}
|
||||
|
||||
// Store userid and platform class to redis
|
||||
func (r *RedisClient) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
||||
func (m *msgCache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
return r.rdb.HSet(ctx, key, token, flag).Err()
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, key, token, flag).Err())
|
||||
}
|
||||
|
||||
//key:userID+platform-> <token, flag>
|
||||
func (r *RedisClient) GetTokenMapByUidPid(ctx context.Context, userID string, platformID int) (map[string]int, error) {
|
||||
key := uidPidToken + userID + ":" + strconv.Itoa(platformID)
|
||||
m, err := r.rdb.HGetAll(ctx, key).Result()
|
||||
mm := make(map[string]int)
|
||||
for k, v := range m {
|
||||
mm[k] = utils.StringToInt(v)
|
||||
}
|
||||
return mm, err
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
m, err := r.rdb.HGetAll(ctx, key).Result()
|
||||
if err != nil && err == redis.Nil {
|
||||
return nil, nil
|
||||
func (m *msgCache) GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error) {
|
||||
key := uidPidToken + userID + ":" + platformID
|
||||
m, err := m.rdb.HGetAll(ctx, key).Result()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
mm := make(map[string]int)
|
||||
for k, v := range m {
|
||||
mm[k] = utils.StringToInt(v)
|
||||
}
|
||||
return mm, utils.Wrap(err, "")
|
||||
return mm, nil
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
func (m *msgCache) SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
mm := make(map[string]interface{})
|
||||
for k, v := range m {
|
||||
mm[k] = v
|
||||
}
|
||||
return r.rdb.HSet(ctx, key, mm).Err()
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, key, mm).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
return r.rdb.HDel(ctx, key, fields...).Err()
|
||||
func (m *msgCache) DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
return utils.Wrap1(m.rdb.HDel(ctx, key, fields...).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetMessagesBySeq(ctx context.Context, userID string, seqList []int64, operationID string) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err2 error) {
|
||||
func (m *msgCache) GetMessagesBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error) {
|
||||
var errResult error
|
||||
for _, v := range seqList {
|
||||
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(v))
|
||||
result, err := r.rdb.Get(ctx, key).Result()
|
||||
result, err := m.rdb.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
if err != redis.Nil {
|
||||
err2 = err
|
||||
}
|
||||
failedSeqs = append(failedSeqs, v)
|
||||
errResult = err
|
||||
failedSeqList = append(failedSeqList, v)
|
||||
} else {
|
||||
msg := sdkws.MsgData{}
|
||||
err = jsonpb.UnmarshalString(result, &msg)
|
||||
if err != nil {
|
||||
err2 = err
|
||||
failedSeqs = append(failedSeqs, v)
|
||||
errResult = err
|
||||
failedSeqList = append(failedSeqList, v)
|
||||
} else {
|
||||
seqMsgs = append(seqMsgs, &msg)
|
||||
seqMsg = append(seqMsg, &msg)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return seqMsgs, failedSeqs, err2
|
||||
return seqMsg, failedSeqList, errResult
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetMessageToCache(ctx context.Context, userID string, msgs []*pbChat.MsgDataToMQ, uid string) (int, error) {
|
||||
pipe := r.rdb.Pipeline()
|
||||
var failedMsgs []pbChat.MsgDataToMQ
|
||||
for _, msg := range msgs {
|
||||
key := messageCache + uid + "_" + strconv.Itoa(int(msg.MsgData.Seq))
|
||||
func (m *msgCache) SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) (int, error) {
|
||||
pipe := m.rdb.Pipeline()
|
||||
var failedList []pbChat.MsgDataToMQ
|
||||
for _, msg := range msgList {
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(msg.MsgData.Seq))
|
||||
s, err := utils.Pb2String(msg.MsgData)
|
||||
if err != nil {
|
||||
continue
|
||||
return 0, utils.Wrap1(err)
|
||||
}
|
||||
err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
|
||||
//err = r.rdb.HMSet(ctx, "12", map[string]interface{}{"1": 2, "343": false}).Err()
|
||||
if err != nil {
|
||||
failedMsgs = append(failedMsgs, *msg)
|
||||
return 0, utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
if len(failedMsgs) != 0 {
|
||||
return len(failedMsgs), errors.New(fmt.Sprintf("set msg to cache failed, failed lists: %v", failedMsgs))
|
||||
if len(failedList) != 0 {
|
||||
return len(failedList), errors.New(fmt.Sprintf("set msg to cache failed, failed lists: %q,%s", failedList, tracelog.GetOperationID(ctx)))
|
||||
}
|
||||
_, err := pipe.Exec(ctx)
|
||||
return 0, err
|
||||
}
|
||||
func (r *RedisClient) DeleteMessageFromCache(ctx context.Context, userID string, msgs []*pbChat.MsgDataToMQ) error {
|
||||
for _, msg := range msgs {
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(msg.MsgData.Seq))
|
||||
err := r.rdb.Del(ctx, key).Err()
|
||||
if err != nil {
|
||||
|
||||
func (m *msgCache) DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error {
|
||||
for _, msg := range msgList {
|
||||
if err := m.rdb.Del(ctx, messageCache+userID+"_"+strconv.Itoa(int(msg.MsgData.Seq))).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RedisClient) CleanUpOneUserAllMsg(ctx context.Context, userID string) error {
|
||||
func (m *msgCache) CleanUpOneUserAllMsg(ctx context.Context, userID string) error {
|
||||
key := messageCache + userID + "_" + "*"
|
||||
vals, err := r.rdb.Keys(ctx, key).Result()
|
||||
vals, err := m.rdb.Keys(ctx, key).Result()
|
||||
if err == redis.Nil {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return utils.Wrap(err, "")
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
for _, v := range vals {
|
||||
err = r.rdb.Del(ctx, v).Err()
|
||||
if err := m.rdb.Del(ctx, v).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RedisClient) HandleSignalInfo(ctx context.Context, operationID string, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error) {
|
||||
func (m *msgCache) HandleSignalInfo(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error) {
|
||||
req := &pbRtc.SignalReq{}
|
||||
if err := proto.Unmarshal(msg.Content, req); err != nil {
|
||||
return false, err
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
var inviteeUserIDs []string
|
||||
var inviteeUserIDList []string
|
||||
var isInviteSignal bool
|
||||
switch signalInfo := req.Payload.(type) {
|
||||
case *pbRtc.SignalReq_Invite:
|
||||
inviteeUserIDs = signalInfo.Invite.Invitation.InviteeUserIDList
|
||||
inviteeUserIDList = signalInfo.Invite.Invitation.InviteeUserIDList
|
||||
isInviteSignal = true
|
||||
case *pbRtc.SignalReq_InviteInGroup:
|
||||
inviteeUserIDs = signalInfo.InviteInGroup.Invitation.InviteeUserIDList
|
||||
inviteeUserIDList = signalInfo.InviteInGroup.Invitation.InviteeUserIDList
|
||||
isInviteSignal = true
|
||||
if !utils.IsContain(pushToUserID, inviteeUserIDs) {
|
||||
if !utils.Contain(pushToUserID, inviteeUserIDList...) {
|
||||
return false, nil
|
||||
}
|
||||
case *pbRtc.SignalReq_HungUp, *pbRtc.SignalReq_Cancel, *pbRtc.SignalReq_Reject, *pbRtc.SignalReq_Accept:
|
||||
return false, nil
|
||||
return false, utils.Wrap1(errors.New("signalInfo do not need offlinePush"))
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
if isInviteSignal {
|
||||
for _, userID := range inviteeUserIDs {
|
||||
for _, userID := range inviteeUserIDList {
|
||||
timeout, err := strconv.Atoi(config.Config.Rtc.SignalTimeout)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
keyList := signalListCache + userID
|
||||
err = r.rdb.LPush(ctx, keyList, msg.ClientMsgID).Err()
|
||||
keyList := SignalListCache + userID
|
||||
err = m.rdb.LPush(ctx, keyList, msg.ClientMsgID).Err()
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
err = r.rdb.Expire(ctx, keyList, time.Duration(timeout)*time.Second).Err()
|
||||
err = m.rdb.Expire(ctx, keyList, time.Duration(timeout)*time.Second).Err()
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
key := signalCache + msg.ClientMsgID
|
||||
err = r.rdb.Set(ctx, key, msg.Content, time.Duration(timeout)*time.Second).Err()
|
||||
key := SignalCache + msg.ClientMsgID
|
||||
err = m.rdb.Set(ctx, key, msg.Content, time.Duration(timeout)*time.Second).Err()
|
||||
if err != nil {
|
||||
return false, err
|
||||
return false, utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetSignalInfoFromCacheByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
||||
key := signalCache + clientMsgID
|
||||
invitationInfo = &pbRtc.SignalInviteReq{}
|
||||
bytes, err := r.rdb.Get(ctx, key).Bytes()
|
||||
func (m *msgCache) GetSignalInfoFromCacheByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
||||
bytes, err := m.rdb.Get(ctx, SignalCache+clientMsgID).Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
req := &pbRtc.SignalReq{}
|
||||
if err = proto.Unmarshal(bytes, req); err != nil {
|
||||
return nil, err
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
invitationInfo = &pbRtc.SignalInviteReq{}
|
||||
switch req2 := req.Payload.(type) {
|
||||
case *pbRtc.SignalReq_Invite:
|
||||
invitationInfo.Invitation = req2.Invite.Invitation
|
||||
@@ -371,162 +327,112 @@ func (r *RedisClient) GetSignalInfoFromCacheByClientMsgID(ctx context.Context, c
|
||||
invitationInfo.Invitation = req2.InviteInGroup.Invitation
|
||||
invitationInfo.OpUserID = req2.InviteInGroup.OpUserID
|
||||
}
|
||||
return invitationInfo, err
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
||||
keyList := signalListCache + userID
|
||||
result := r.rdb.LPop(ctx, keyList)
|
||||
if err = result.Err(); err != nil {
|
||||
return nil, utils.Wrap(err, "GetAvailableSignalInvitationInfo failed")
|
||||
}
|
||||
key, err := result.Result()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "GetAvailableSignalInvitationInfo failed")
|
||||
}
|
||||
invitationInfo, err = r.GetSignalInfoFromCacheByClientMsgID(ctx, key)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "GetSignalInfoFromCacheByClientMsgID")
|
||||
}
|
||||
err = r.DelUserSignalList(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, utils.Wrap(err, "GetSignalInfoFromCacheByClientMsgID")
|
||||
}
|
||||
return invitationInfo, nil
|
||||
}
|
||||
|
||||
func (r *RedisClient) DelUserSignalList(ctx context.Context, userID string) error {
|
||||
keyList := signalListCache + userID
|
||||
err := r.rdb.Del(ctx, keyList).Err()
|
||||
return err
|
||||
func (m *msgCache) GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
||||
key, err := m.rdb.LPop(ctx, SignalListCache+userID).Result()
|
||||
if err != nil {
|
||||
return nil, utils.Wrap1(err)
|
||||
}
|
||||
invitationInfo, err = m.GetSignalInfoFromCacheByClientMsgID(ctx, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return invitationInfo, m.DelUserSignalList(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *RedisClient) DelMsgFromCache(ctx context.Context, uid string, seqList []int64, operationID string) {
|
||||
func (m *msgCache) DelUserSignalList(ctx context.Context, userID string) error {
|
||||
return utils.Wrap1(m.rdb.Del(ctx, SignalListCache+userID).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error {
|
||||
for _, seq := range seqList {
|
||||
key := messageCache + uid + "_" + strconv.Itoa(int(seq))
|
||||
result, err := r.rdb.Get(ctx, key).Result()
|
||||
key := messageCache + userID + "_" + strconv.Itoa(int(seq))
|
||||
result, err := m.rdb.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
if err == redis.Nil {
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
continue
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
var msg sdkws.MsgData
|
||||
if err := utils.String2Pb(result, &msg); err != nil {
|
||||
continue
|
||||
if err := jsonpb.UnmarshalString(result, &msg); err != nil {
|
||||
return err
|
||||
}
|
||||
msg.Status = constant.MsgDeleted
|
||||
s, err := utils.Pb2String(&msg)
|
||||
if err != nil {
|
||||
continue
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
if err := r.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
||||
if err := m.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
||||
return utils.Wrap1(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetGetuiToken(ctx context.Context, token string, expireTime int64) error {
|
||||
return r.rdb.Set(ctx, getuiToken, token, time.Duration(expireTime)*time.Second).Err()
|
||||
func (m *msgCache) SetGetuiToken(ctx context.Context, token string, expireTime int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, getuiToken, token, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetGetuiToken(ctx context.Context) (string, error) {
|
||||
result, err := r.rdb.Get(ctx, getuiToken).Result()
|
||||
return result, err
|
||||
func (m *msgCache) GetGetuiToken(ctx context.Context) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, getuiToken).Result())
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error {
|
||||
return r.rdb.Set(ctx, getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err()
|
||||
func (m *msgCache) SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetGetuiTaskID(ctx context.Context) (string, error) {
|
||||
result, err := r.rdb.Get(ctx, getuiTaskID).Result()
|
||||
return result, err
|
||||
func (m *msgCache) GetGetuiTaskID(ctx context.Context) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, getuiTaskID).Result())
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetSendMsgStatus(ctx context.Context, status int32, operationID string) error {
|
||||
return r.rdb.Set(ctx, sendMsgFailedFlag+operationID, status, time.Hour*24).Err()
|
||||
func (m *msgCache) SetSendMsgStatus(ctx context.Context, id string, status int32) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, sendMsgFailedFlag+id, status, time.Hour*24).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetSendMsgStatus(ctx context.Context, operationID string) (int, error) {
|
||||
result, err := r.rdb.Get(ctx, sendMsgFailedFlag+operationID).Result()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
status, err := strconv.Atoi(result)
|
||||
return status, err
|
||||
func (m *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
|
||||
result, err := m.rdb.Get(ctx, sendMsgFailedFlag+id).Int()
|
||||
return int32(result), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
||||
key := FcmToken + account + ":" + strconv.Itoa(platformID)
|
||||
return r.rdb.Set(ctx, key, fcmToken, time.Duration(expireTime)*time.Second).Err()
|
||||
func (m *msgCache) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, FcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
|
||||
key := FcmToken + account + ":" + strconv.Itoa(platformID)
|
||||
return r.rdb.Get(ctx, key).Result()
|
||||
}
|
||||
func (r *RedisClient) DelFcmToken(ctx context.Context, account string, platformID int) error {
|
||||
key := FcmToken + account + ":" + strconv.Itoa(platformID)
|
||||
return r.rdb.Del(ctx, key).Err()
|
||||
}
|
||||
func (r *RedisClient) IncrUserBadgeUnreadCountSum(ctx context.Context, uid string) (int, error) {
|
||||
key := userBadgeUnreadCountSum + uid
|
||||
seq, err := r.rdb.Incr(ctx, key).Result()
|
||||
return int(seq), err
|
||||
}
|
||||
func (r *RedisClient) SetUserBadgeUnreadCountSum(ctx context.Context, uid string, value int) error {
|
||||
key := userBadgeUnreadCountSum + uid
|
||||
return r.rdb.Set(ctx, key, value, 0).Err()
|
||||
}
|
||||
func (r *RedisClient) GetUserBadgeUnreadCountSum(ctx context.Context, uid string) (int, error) {
|
||||
key := userBadgeUnreadCountSum + uid
|
||||
seq, err := r.rdb.Get(ctx, key).Result()
|
||||
return utils.StringToInt(seq), err
|
||||
}
|
||||
func (r *RedisClient) JudgeMessageReactionEXISTS(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
|
||||
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
|
||||
n, err := r.rdb.Exists(ctx, key).Result()
|
||||
return n > 0, err
|
||||
func (m *msgCache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Result())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
|
||||
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
|
||||
return r.rdb.HGetAll(ctx, key).Result()
|
||||
|
||||
}
|
||||
func (r *RedisClient) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
|
||||
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
|
||||
return r.rdb.HDel(ctx, key, subKey).Err()
|
||||
|
||||
}
|
||||
func (r *RedisClient) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
||||
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
|
||||
return r.rdb.Expire(ctx, key, expiration).Result()
|
||||
func (m *msgCache) DelFcmToken(ctx context.Context, account string, platformID int) error {
|
||||
return utils.Wrap1(m.rdb.Del(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
||||
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
|
||||
result, err := r.rdb.HGet(ctx, key, typeKey).Result()
|
||||
return result, err
|
||||
func (m *msgCache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||
seq, err := m.rdb.Incr(ctx, userBadgeUnreadCountSum+userID).Result()
|
||||
return int(seq), utils.Wrap1(err)
|
||||
}
|
||||
|
||||
func (r *RedisClient) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
|
||||
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
|
||||
return r.rdb.HSet(ctx, key, typeKey, value).Err()
|
||||
|
||||
func (m *msgCache) SetUserBadgeUnreadCountSum(ctx context.Context, userID string, value int) error {
|
||||
return utils.Wrap1(m.rdb.Set(ctx, userBadgeUnreadCountSum+userID, value, 0).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
func (m *msgCache) GetUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||
return utils.Wrap2(m.rdb.Get(ctx, userBadgeUnreadCountSum+userID).Int())
|
||||
}
|
||||
|
||||
func (m *msgCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||||
return r.rdb.SetNX(ctx, key, 1, time.Minute).Err()
|
||||
return utils.Wrap1(m.rdb.SetNX(ctx, key, 1, time.Minute).Err())
|
||||
}
|
||||
func (r *RedisClient) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
|
||||
func (m *msgCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||||
return r.rdb.Del(ctx, key).Err()
|
||||
|
||||
return utils.Wrap1(m.rdb.Del(ctx, key).Err())
|
||||
}
|
||||
|
||||
func (r *RedisClient) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
|
||||
func (m *msgCache) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
|
||||
switch sessionType {
|
||||
case constant.SingleChatType:
|
||||
return "EX_SINGLE_" + clientMsgID
|
||||
@@ -539,3 +445,31 @@ func (r *RedisClient) getMessageReactionExPrefix(clientMsgID string, sessionType
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *msgCache) JudgeMessageReactionEXISTS(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
|
||||
n, err := m.rdb.Exists(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType)).Result()
|
||||
if err != nil {
|
||||
return false, utils.Wrap(err, "")
|
||||
}
|
||||
return n > 0, nil
|
||||
}
|
||||
|
||||
func (m *msgCache) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
|
||||
return utils.Wrap1(m.rdb.HSet(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err())
|
||||
}
|
||||
|
||||
func (m *msgCache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
||||
return utils.Wrap2(m.rdb.Expire(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
||||
return utils.Wrap2(m.rdb.HGet(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
|
||||
return utils.Wrap2(m.rdb.HGetAll(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType)).Result())
|
||||
}
|
||||
|
||||
func (m *msgCache) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
|
||||
return utils.Wrap1(m.rdb.HDel(ctx, m.getMessageReactionExPrefix(clientMsgID, sessionType), subKey).Err())
|
||||
}
|
||||
|
||||
Vendored
+103
-103
@@ -1,107 +1,107 @@
|
||||
package cache
|
||||
|
||||
//import (
|
||||
// "Open_IM/pkg/common/constant"
|
||||
// pbChat "Open_IM/pkg/proto/msg"
|
||||
// common "Open_IM/pkg/proto/sdkws"
|
||||
// "context"
|
||||
// "flag"
|
||||
// "fmt"
|
||||
// "github.com/stretchr/testify/assert"
|
||||
// "testing"
|
||||
//)
|
||||
//
|
||||
//var DB RedisClient
|
||||
//
|
||||
//func Test_SetTokenMapByUidPid(t *testing.T) {
|
||||
// //m := make(map[string]int, 0)
|
||||
// //m["test1"] = 1
|
||||
// //m["test2"] = 2
|
||||
// //m["2332"] = 4
|
||||
// //err := DB.SetTokenMapByUidPid("1234", 2, m)
|
||||
// //assert.Nil(t, err)
|
||||
//
|
||||
//}
|
||||
//func Test_GetTokenMapByUidPid(t *testing.T) {
|
||||
// //m, err := DB.GetTokenMapByUidPid("1234", "Android")
|
||||
// //assert.Nil(t, err)
|
||||
// //fmt.Println(m)
|
||||
//}
|
||||
//
|
||||
////func TestDataBases_GetMultiConversationMsgOpt(t *testing.T) {
|
||||
//// m, err := DB.GetMultiConversationMsgOpt("fg", []string{"user", "age", "color"})
|
||||
//// assert.Nil(t, err)
|
||||
//// fmt.Println(m)
|
||||
////}
|
||||
//func Test_GetKeyTTL(t *testing.T) {
|
||||
// ctx := context.Background()
|
||||
// key := flag.String("key", "key", "key value")
|
||||
// flag.Parse()
|
||||
// ttl, err := DB.GetClient().TTL(ctx, *key).Result()
|
||||
import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
pbChat "OpenIM/pkg/proto/msg"
|
||||
common "OpenIM/pkg/proto/sdkws"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var DB RedisClient
|
||||
|
||||
func Test_SetTokenMapByUidPid(t *testing.T) {
|
||||
m := make(map[string]int, 0)
|
||||
m["test1"] = 1
|
||||
m["test2"] = 2
|
||||
m["2332"] = 4
|
||||
err := DB.SetTokenMapByUidPid("1234", 2, m)
|
||||
assert.Nil(t, err)
|
||||
|
||||
}
|
||||
func Test_GetTokenMapByUidPid(t *testing.T) {
|
||||
m, err := DB.GetTokenMapByUidPid("1234", "Android")
|
||||
assert.Nil(t, err)
|
||||
fmt.Println(m)
|
||||
}
|
||||
|
||||
//func TestDataBases_GetMultiConversationMsgOpt(t *testing.T) {
|
||||
// m, err := DB.GetMultiConversationMsgOpt("fg", []string{"user", "age", "color"})
|
||||
// assert.Nil(t, err)
|
||||
// fmt.Println(ttl)
|
||||
//}
|
||||
//func Test_HGetAll(t *testing.T) {
|
||||
// ctx := context.Background()
|
||||
// key := flag.String("key", "key", "key value")
|
||||
// flag.Parse()
|
||||
// ttl, err := DB.GetClient().TTL(ctx, *key).Result()
|
||||
// assert.Nil(t, err)
|
||||
// fmt.Println(ttl)
|
||||
//}
|
||||
//
|
||||
//func Test_NewSetMessageToCache(t *testing.T) {
|
||||
// var msg pbChat.MsgDataToMQ
|
||||
// m := make(map[string]bool)
|
||||
// var offlinePush common.OfflinePushInfo
|
||||
// offlinePush.Title = "3"
|
||||
// offlinePush.Ex = "34"
|
||||
// offlinePush.IOSPushSound = "+1"
|
||||
// offlinePush.IOSBadgeCount = true
|
||||
// m[constant.IsPersistent] = true
|
||||
// m[constant.IsHistory] = true
|
||||
// var data common.MsgData
|
||||
// uid := "test_uid"
|
||||
// data.Seq = 11
|
||||
// data.ClientMsgID = "23jwhjsdf"
|
||||
// data.SendID = "111"
|
||||
// data.RecvID = "222"
|
||||
// data.Content = []byte{1, 2, 3, 4, 5, 6, 7}
|
||||
// data.Seq = 1212
|
||||
// data.Options = m
|
||||
// data.OfflinePushInfo = &offlinePush
|
||||
// data.AtUserIDList = []string{"1212", "23232"}
|
||||
// msg.MsgData = &data
|
||||
// messageList := []*pbChat.MsgDataToMQ{&msg}
|
||||
// err, _ := DB.SetMessageToCache(messageList, uid, "cacheTest")
|
||||
// assert.Nil(t, err)
|
||||
//
|
||||
//}
|
||||
//func Test_NewGetMessageListBySeq(t *testing.T) {
|
||||
// var msg pbChat.MsgDataToMQ
|
||||
// var data common.MsgData
|
||||
// uid := "test_uid"
|
||||
// data.Seq = 11
|
||||
// data.ClientMsgID = "23jwhjsdf"
|
||||
// msg.MsgData = &data
|
||||
//
|
||||
// seqMsg, failedSeqList, err := DB.GetMessageListBySeq(uid, []uint32{1212}, "cacheTest")
|
||||
// assert.Nil(t, err)
|
||||
// fmt.Println(seqMsg, failedSeqList)
|
||||
//
|
||||
//}
|
||||
//
|
||||
//func Test_SetFcmToken(t *testing.T) {
|
||||
// uid := "test_uid"
|
||||
// token := "dfnWBtOjSj-XIZnUvDlegv:APA91bG09XTtiXfpE6U7gUVMOhnKcUkNCv4WHn0UZr2clUi-tS1jEH-HiCEW8GIAhjLIGcfUJ6NIKteC023ZxDH7J0PJ5sTxoup3fHDUPLU7KgQoZS4tPyFqCbZ6bRB7esDPEnD1n_s0"
|
||||
// platformID := 2
|
||||
// err := DB.SetFcmToken(uid, platformID, token, 0)
|
||||
// assert.Nil(t, err)
|
||||
//}
|
||||
//func Test_GetFcmToken(t *testing.T) {
|
||||
// uid := "test_uid"
|
||||
// platformID := 2
|
||||
// token, err := DB.GetFcmToken(uid, platformID)
|
||||
// assert.Nil(t, err)
|
||||
// fmt.Println("token is :", token)
|
||||
// fmt.Println(m)
|
||||
//}
|
||||
func Test_GetKeyTTL(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
key := flag.String("key", "key", "key value")
|
||||
flag.Parse()
|
||||
ttl, err := DB.GetClient().TTL(ctx, *key).Result()
|
||||
assert.Nil(t, err)
|
||||
fmt.Println(ttl)
|
||||
}
|
||||
func Test_HGetAll(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
key := flag.String("key", "key", "key value")
|
||||
flag.Parse()
|
||||
ttl, err := DB.GetClient().TTL(ctx, *key).Result()
|
||||
assert.Nil(t, err)
|
||||
fmt.Println(ttl)
|
||||
}
|
||||
|
||||
func Test_NewSetMessageToCache(t *testing.T) {
|
||||
var msg pbChat.MsgDataToMQ
|
||||
m := make(map[string]bool)
|
||||
var offlinePush common.OfflinePushInfo
|
||||
offlinePush.Title = "3"
|
||||
offlinePush.Ex = "34"
|
||||
offlinePush.IOSPushSound = "+1"
|
||||
offlinePush.IOSBadgeCount = true
|
||||
m[constant.IsPersistent] = true
|
||||
m[constant.IsHistory] = true
|
||||
var data common.MsgData
|
||||
uid := "test_uid"
|
||||
data.Seq = 11
|
||||
data.ClientMsgID = "23jwhjsdf"
|
||||
data.SendID = "111"
|
||||
data.RecvID = "222"
|
||||
data.Content = []byte{1, 2, 3, 4, 5, 6, 7}
|
||||
data.Seq = 1212
|
||||
data.Options = m
|
||||
data.OfflinePushInfo = &offlinePush
|
||||
data.AtUserIDList = []string{"1212", "23232"}
|
||||
msg.MsgData = &data
|
||||
messageList := []*pbChat.MsgDataToMQ{&msg}
|
||||
err, _ := DB.SetMessageToCache(messageList, uid, "cacheTest")
|
||||
assert.Nil(t, err)
|
||||
|
||||
}
|
||||
func Test_NewGetMessagesBySeq(t *testing.T) {
|
||||
var msg pbChat.MsgDataToMQ
|
||||
var data common.MsgData
|
||||
uid := "test_uid"
|
||||
data.Seq = 11
|
||||
data.ClientMsgID = "23jwhjsdf"
|
||||
msg.MsgData = &data
|
||||
|
||||
seqMsg, failedSeqList, err := DB.GetMessagesBySeq(uid, []uint32{1212}, "cacheTest")
|
||||
assert.Nil(t, err)
|
||||
fmt.Println(seqMsg, failedSeqList)
|
||||
|
||||
}
|
||||
|
||||
func Test_SetFcmToken(t *testing.T) {
|
||||
uid := "test_uid"
|
||||
token := "dfnWBtOjSj-XIZnUvDlegv:APA91bG09XTtiXfpE6U7gUVMOhnKcUkNCv4WHn0UZr2clUi-tS1jEH-HiCEW8GIAhjLIGcfUJ6NIKteC023ZxDH7J0PJ5sTxoup3fHDUPLU7KgQoZS4tPyFqCbZ6bRB7esDPEnD1n_s0"
|
||||
platformID := 2
|
||||
err := DB.SetFcmToken(uid, platformID, token, 0)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
func Test_GetFcmToken(t *testing.T) {
|
||||
uid := "test_uid"
|
||||
platformID := 2
|
||||
token, err := DB.GetFcmToken(uid, platformID)
|
||||
assert.Nil(t, err)
|
||||
fmt.Println("token is :", token)
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -1,7 +1,7 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
|
||||
Vendored
+3
-3
@@ -1,9 +1,9 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/tokenverify"
|
||||
"Open_IM/pkg/utils"
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/tokenverify"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
Vendored
+4
-4
@@ -1,10 +1,10 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"OpenIM/pkg/common/db/relation"
|
||||
relationTb "OpenIM/pkg/common/db/table/relation"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/utils"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
|
||||
Reference in New Issue
Block a user