|
|
|
|
@@ -18,6 +18,88 @@ import (
|
|
|
|
|
"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:"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Cache 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)
|
|
|
|
|
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, status int32) error
|
|
|
|
|
GetSendMsgStatus(ctx context.Context) (int, 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
@@ -131,13 +213,13 @@ func (r *RedisClient) SetGroupMinSeq(ctx context.Context, groupID string, minSeq
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store userid and platform class to redis
|
|
|
|
|
func (r *RedisClient) AddTokenFlag(ctx context.Context, userID string, platform string, token string, flag int) error {
|
|
|
|
|
key := uidPidToken + userID + ":" + platform
|
|
|
|
|
func (r *RedisClient) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
|
|
|
|
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
|
|
|
|
return r.rdb.HSet(context.Background(), key, token, flag).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//key:userID+platform-> <token, flag>
|
|
|
|
|
func (r *RedisClient) GetTokenMapByUidPid(ctx context.Context, userID, platformID string) (map[string]int, error) {
|
|
|
|
|
func (r *RedisClient) GetTokenMapByUidPid(ctx context.Context, userID, platformID int) (map[string]int, error) {
|
|
|
|
|
key := uidPidToken + userID + ":" + platformID
|
|
|
|
|
m, err := r.rdb.HGetAll(context.Background(), key).Result()
|
|
|
|
|
mm := make(map[string]int)
|
|
|
|
|
@@ -174,7 +256,7 @@ func (r *RedisClient) DeleteTokenByUidPid(ctx context.Context, userID string, pl
|
|
|
|
|
return r.rdb.HDel(context.Background(), key, fields...).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *RedisClient) GetMessageListBySeq(ctx context.Context, userID string, seqList []int64, operationID string) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err2 error) {
|
|
|
|
|
func (r *RedisClient) GetMessagesBySeq(ctx context.Context, userID string, seqList []int64, operationID string) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err2 error) {
|
|
|
|
|
for _, v := range seqList {
|
|
|
|
|
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
|
|
|
|
|
key := messageCache + userID + "_" + strconv.Itoa(int(v))
|
|
|
|
|
@@ -183,25 +265,25 @@ func (r *RedisClient) GetMessageListBySeq(ctx context.Context, userID string, se
|
|
|
|
|
if err != redis.Nil {
|
|
|
|
|
err2 = err
|
|
|
|
|
}
|
|
|
|
|
failedSeqList = append(failedSeqList, v)
|
|
|
|
|
failedSeqs = append(failedSeqs, v)
|
|
|
|
|
} else {
|
|
|
|
|
msg := sdkws.MsgData{}
|
|
|
|
|
err = jsonpb.UnmarshalString(result, &msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err2 = err
|
|
|
|
|
failedSeqList = append(failedSeqList, v)
|
|
|
|
|
failedSeqs = append(failedSeqs, v)
|
|
|
|
|
} else {
|
|
|
|
|
seqMsg = append(seqMsg, &msg)
|
|
|
|
|
seqMsgs = append(seqMsgs, &msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return seqMsg, failedSeqList, err2
|
|
|
|
|
return seqMsgs, failedSeqs, err2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *RedisClient) SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ, uid string) (int, error) {
|
|
|
|
|
func (r *RedisClient) SetMessageToCache(ctx context.Context, userID string, msgs []*pbChat.MsgDataToMQ, uid string) (int, error) {
|
|
|
|
|
pipe := r.rdb.Pipeline()
|
|
|
|
|
var failedList []pbChat.MsgDataToMQ
|
|
|
|
|
for _, msg := range msgList {
|
|
|
|
|
var failedMsgs []pbChat.MsgDataToMQ
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
|
key := messageCache + uid + "_" + strconv.Itoa(int(msg.MsgData.Seq))
|
|
|
|
|
s, err := utils.Pb2String(msg.MsgData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@@ -210,17 +292,17 @@ func (r *RedisClient) SetMessageToCache(ctx context.Context, userID string, msgL
|
|
|
|
|
err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
|
|
|
|
|
//err = r.rdb.HMSet(context.Background(), "12", map[string]interface{}{"1": 2, "343": false}).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
failedList = append(failedList, *msg)
|
|
|
|
|
failedMsgs = append(failedMsgs, *msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(failedList) != 0 {
|
|
|
|
|
return len(failedList), errors.New(fmt.Sprintf("set msg to cache failed, failed lists: %q,%s", failedList))
|
|
|
|
|
if len(failedMsgs) != 0 {
|
|
|
|
|
return len(failedMsgs), errors.New(fmt.Sprintf("set msg to cache failed, failed lists: %q,%s", failedList))
|
|
|
|
|
}
|
|
|
|
|
_, err := pipe.Exec(ctx)
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
func (r *RedisClient) DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error {
|
|
|
|
|
for _, msg := range msgList {
|
|
|
|
|
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 {
|
|
|
|
|
|