Files
open-im-server/pkg/common/db/cache/redis.go
T

464 lines
18 KiB
Go
Raw Normal View History

2023-01-16 20:14:26 +08:00
package cache
2022-06-06 18:15:32 +08:00
import (
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
2023-02-24 11:01:33 +08:00
"OpenIM/pkg/common/tracelog"
2023-03-02 12:00:31 +08:00
pbMsg "OpenIM/pkg/proto/msg"
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/proto/sdkws"
"OpenIM/pkg/utils"
2022-06-06 18:15:32 +08:00
"context"
"errors"
"fmt"
2023-02-24 11:01:33 +08:00
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
2022-08-18 11:14:38 +08:00
"strconv"
"time"
2023-01-29 11:37:11 +08:00
"github.com/go-redis/redis/v8"
2022-06-06 18:15:32 +08:00
)
2022-07-20 10:18:43 +08:00
2022-06-23 10:18:44 +08:00
const (
2023-03-02 12:00:31 +08:00
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
appleDeviceToken = "DEVICE_TOKEN"
userMinSeq = "REDIS_USER_MIN_SEQ:"
2023-02-09 14:40:49 +08:00
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:"
2023-03-02 12:00:31 +08:00
uidPidToken = "UID_PID_TOKEN_STATUS:"
2022-06-23 10:18:44 +08:00
)
2023-03-03 17:42:26 +08:00
type Model interface {
2023-02-16 15:20:59 +08:00
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)
2023-02-24 11:01:33 +08:00
GetGroupMinSeq(ctx context.Context, groupID string) (int64, error)
2023-02-16 15:20:59 +08:00
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
2023-02-15 15:52:32 +08:00
AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error
2023-02-20 17:13:15 +08:00
GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error)
2023-03-01 15:32:26 +08:00
SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error
DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error
2023-02-23 19:20:58 +08:00
GetMessagesBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
2023-03-02 12:00:31 +08:00
SetMessageToCache(ctx context.Context, userID string, msgList []*pbMsg.MsgDataToMQ) (int, error)
DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbMsg.MsgDataToMQ) error
2023-02-15 17:00:43 +08:00
CleanUpOneUserAllMsg(ctx context.Context, userID string) error
2023-03-02 12:00:31 +08:00
HandleSignalInvite(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error)
2023-03-01 15:32:26 +08:00
GetSignalInvitationInfoByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *sdkws.SignalInviteReq, err error)
GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *sdkws.SignalInviteReq, err error)
2023-02-15 15:52:32 +08:00
DelUserSignalList(ctx context.Context, userID string) error
2023-02-16 15:20:59 +08:00
DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error
2023-02-15 15:52:32 +08:00
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)
2023-02-24 11:01:33 +08:00
SetSendMsgStatus(ctx context.Context, id string, status int32) error
GetSendMsgStatus(ctx context.Context, id string) (int32, error)
2023-02-15 15:52:32 +08:00
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)
2023-03-03 17:42:26 +08:00
JudgeMessageReactionExist(ctx context.Context, clientMsgID string, sessionType int32) (bool, error)
2023-02-15 15:52:32 +08:00
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
2023-02-09 14:40:49 +08:00
}
2023-03-03 17:42:26 +08:00
func NewCacheModel(client redis.UniversalClient) Model {
2023-03-01 15:32:26 +08:00
return &cache{rdb: client}
2023-01-16 20:14:26 +08:00
}
2023-03-01 15:32:26 +08:00
type cache struct {
2023-02-14 16:33:18 +08:00
rdb redis.UniversalClient
2023-01-16 20:14:26 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) IncrUserSeq(ctx context.Context, userID string) (int64, error) {
return utils.Wrap2(c.rdb.Get(ctx, userIncrSeq+userID).Int64())
2023-01-17 16:36:34 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetUserMaxSeq(ctx context.Context, userID string) (int64, error) {
return utils.Wrap2(c.rdb.Get(ctx, userIncrSeq+userID).Int64())
2023-02-20 10:49:39 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetUserMaxSeq(ctx context.Context, userID string, maxSeq int64) error {
return utils.Wrap1(c.rdb.Set(ctx, userIncrSeq+userID, maxSeq, 0).Err())
2022-06-15 16:33:30 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error) {
return utils.Wrap1(c.rdb.Set(ctx, userMinSeq+userID, minSeq, 0).Err())
2022-06-15 16:33:30 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetUserMinSeq(ctx context.Context, userID string) (int64, error) {
return utils.Wrap2(c.rdb.Get(ctx, userMinSeq+userID).Int64())
2022-06-15 16:33:30 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) {
2023-02-24 11:01:33 +08:00
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
2023-03-01 15:32:26 +08:00
return utils.Wrap1(c.rdb.Set(ctx, key, minSeq, 0).Err())
2022-06-15 16:33:30 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error) {
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
return utils.Wrap2(c.rdb.Get(ctx, key).Int64())
2022-06-15 16:33:30 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
return utils.Wrap2(c.rdb.Get(ctx, groupMaxSeq+groupID).Int64())
2022-07-20 10:18:43 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetGroupMinSeq(ctx context.Context, groupID string) (int64, error) {
return utils.Wrap2(c.rdb.Get(ctx, groupMinSeq+groupID).Int64())
2022-07-22 16:55:29 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
2022-07-22 16:55:29 +08:00
key := groupMaxSeq + groupID
2023-03-01 15:32:26 +08:00
seq, err := c.rdb.Incr(ctx, key).Uint64()
2023-02-24 11:01:33 +08:00
return int64(seq), utils.Wrap1(err)
2022-07-22 16:55:29 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error {
2022-07-22 16:55:29 +08:00
key := groupMaxSeq + groupID
2023-03-01 15:32:26 +08:00
return utils.Wrap1(c.rdb.Set(ctx, key, maxSeq, 0).Err())
2022-07-22 16:55:29 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error {
2022-08-04 17:20:33 +08:00
key := groupMinSeq + groupID
2023-03-01 15:32:26 +08:00
return utils.Wrap1(c.rdb.Set(ctx, key, minSeq, 0).Err())
2022-08-04 17:20:33 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
2023-02-23 17:28:57 +08:00
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
2023-03-01 15:32:26 +08:00
return utils.Wrap1(c.rdb.HSet(ctx, key, token, flag).Err())
2022-06-15 16:33:30 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error) {
2022-06-15 16:33:30 +08:00
key := uidPidToken + userID + ":" + platformID
2023-03-01 15:32:26 +08:00
m, err := c.rdb.HGetAll(ctx, key).Result()
2023-02-24 11:01:33 +08:00
if err != nil {
return nil, utils.Wrap1(err)
2023-02-20 17:13:15 +08:00
}
mm := make(map[string]int)
for k, v := range m {
mm[k] = utils.StringToInt(v)
}
2023-02-24 11:01:33 +08:00
return mm, nil
2023-02-20 17:13:15 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error {
key := uidPidToken + userID + ":" + platform
2022-06-21 21:27:00 +08:00
mm := make(map[string]interface{})
for k, v := range m {
mm[k] = v
}
2023-03-01 15:32:26 +08:00
return utils.Wrap1(c.rdb.HSet(ctx, key, mm).Err())
2022-06-15 16:33:30 +08:00
}
2023-02-09 14:40:49 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error {
key := uidPidToken + userID + ":" + platform
return utils.Wrap1(c.rdb.HDel(ctx, key, fields...).Err())
2022-06-15 16:33:30 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetMessagesBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error) {
2023-02-24 11:01:33 +08:00
var errResult error
2022-06-06 18:15:32 +08:00
for _, v := range seqList {
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
key := messageCache + userID + "_" + strconv.Itoa(int(v))
2023-03-01 15:32:26 +08:00
result, err := c.rdb.Get(ctx, key).Result()
2022-06-15 23:30:33 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
errResult = err
failedSeqList = append(failedSeqList, v)
2022-06-15 23:30:33 +08:00
} else {
2023-02-15 15:52:32 +08:00
msg := sdkws.MsgData{}
2022-06-15 23:30:33 +08:00
err = jsonpb.UnmarshalString(result, &msg)
2022-06-06 18:15:32 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
errResult = err
failedSeqList = append(failedSeqList, v)
2022-06-06 18:15:32 +08:00
} else {
2023-02-24 11:01:33 +08:00
seqMsg = append(seqMsg, &msg)
2022-06-06 18:15:32 +08:00
}
2023-02-24 11:01:33 +08:00
2022-06-06 18:15:32 +08:00
}
}
2023-02-24 11:01:33 +08:00
return seqMsg, failedSeqList, errResult
2022-06-06 18:15:32 +08:00
}
2022-08-05 15:16:43 +08:00
2023-03-02 12:00:31 +08:00
func (c *cache) SetMessageToCache(ctx context.Context, userID string, msgList []*pbMsg.MsgDataToMQ) (int, error) {
2023-03-01 15:32:26 +08:00
pipe := c.rdb.Pipeline()
2023-03-03 17:42:26 +08:00
var failedMsgs []pbMsg.MsgDataToMQ
2023-02-24 11:01:33 +08:00
for _, msg := range msgList {
key := messageCache + userID + "_" + strconv.Itoa(int(msg.MsgData.Seq))
2022-06-13 15:58:14 +08:00
s, err := utils.Pb2String(msg.MsgData)
2022-06-06 18:15:32 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
return 0, utils.Wrap1(err)
2022-06-06 18:15:32 +08:00
}
2022-06-15 19:17:49 +08:00
err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
2022-06-06 18:15:32 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
return 0, utils.Wrap1(err)
2022-06-06 18:15:32 +08:00
}
}
2023-03-03 17:42:26 +08:00
if len(failedMsgs) != 0 {
2023-03-03 18:44:14 +08:00
return len(failedMsgs), errors.New(fmt.Sprintf("set msg to cache failed, failed lists: %q,%s", failedMsgs, tracelog.GetOperationID(ctx)))
2022-06-06 18:15:32 +08:00
}
2022-06-15 19:17:49 +08:00
_, err := pipe.Exec(ctx)
2023-02-15 15:52:32 +08:00
return 0, err
2022-06-06 18:15:32 +08:00
}
2023-02-24 11:01:33 +08:00
2023-03-02 12:00:31 +08:00
func (c *cache) DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbMsg.MsgDataToMQ) error {
for _, v := range msgList {
if err := c.rdb.Del(ctx, messageCache+userID+"_"+strconv.Itoa(int(v.MsgData.Seq))).Err(); err != nil {
2023-02-24 11:01:33 +08:00
return utils.Wrap1(err)
2022-08-18 11:14:38 +08:00
}
2022-07-27 12:15:32 +08:00
}
2022-08-18 11:14:38 +08:00
return nil
2022-07-27 12:15:32 +08:00
}
2022-06-06 18:15:32 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) CleanUpOneUserAllMsg(ctx context.Context, userID string) error {
2022-06-06 18:15:32 +08:00
key := messageCache + userID + "_" + "*"
2023-03-01 15:32:26 +08:00
vals, err := c.rdb.Keys(ctx, key).Result()
2023-01-29 11:37:11 +08:00
if err == redis.Nil {
2022-06-06 18:15:32 +08:00
return nil
}
if err != nil {
2023-02-24 11:01:33 +08:00
return utils.Wrap1(err)
2022-06-06 18:15:32 +08:00
}
2022-08-11 20:25:33 +08:00
for _, v := range vals {
2023-03-01 15:32:26 +08:00
if err := c.rdb.Del(ctx, v).Err(); err != nil {
2023-02-24 11:01:33 +08:00
return utils.Wrap1(err)
}
2022-06-06 18:15:32 +08:00
}
return nil
}
2023-03-02 12:00:31 +08:00
func (c *cache) HandleSignalInvite(ctx context.Context, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error) {
req := &sdkws.SignalReq{}
2022-06-06 18:52:17 +08:00
if err := proto.Unmarshal(msg.Content, req); err != nil {
2023-02-24 11:01:33 +08:00
return false, utils.Wrap1(err)
2022-06-06 18:15:32 +08:00
}
2023-03-02 12:00:31 +08:00
var inviteeUserIDs []string
2022-06-10 17:31:30 +08:00
var isInviteSignal bool
switch signalInfo := req.Payload.(type) {
2023-03-02 12:00:31 +08:00
case *sdkws.SignalReq_Invite:
inviteeUserIDs = signalInfo.Invite.Invitation.InviteeUserIDList
2022-06-10 17:31:30 +08:00
isInviteSignal = true
2023-03-02 12:00:31 +08:00
case *sdkws.SignalReq_InviteInGroup:
inviteeUserIDs = signalInfo.InviteInGroup.Invitation.InviteeUserIDList
2022-06-10 17:31:30 +08:00
isInviteSignal = true
2023-03-02 12:00:31 +08:00
if !utils.Contain(pushToUserID, inviteeUserIDs...) {
2022-09-02 17:22:27 +08:00
return false, nil
}
2023-03-02 12:00:31 +08:00
case *sdkws.SignalReq_HungUp, *sdkws.SignalReq_Cancel, *sdkws.SignalReq_Reject, *sdkws.SignalReq_Accept:
2023-02-24 11:01:33 +08:00
return false, utils.Wrap1(errors.New("signalInfo do not need offlinePush"))
2022-06-06 21:04:11 +08:00
default:
2022-09-02 17:22:27 +08:00
return false, nil
2022-06-06 21:04:11 +08:00
}
2022-06-10 17:31:30 +08:00
if isInviteSignal {
2023-03-02 12:00:31 +08:00
for _, userID := range inviteeUserIDs {
2022-06-10 17:31:30 +08:00
timeout, err := strconv.Atoi(config.Config.Rtc.SignalTimeout)
if err != nil {
2023-02-24 11:01:33 +08:00
return false, utils.Wrap1(err)
2022-06-10 17:31:30 +08:00
}
2023-03-02 12:00:31 +08:00
keyList := signalListCache + userID
2023-03-01 15:32:26 +08:00
err = c.rdb.LPush(ctx, keyList, msg.ClientMsgID).Err()
2022-06-10 17:31:30 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
return false, utils.Wrap1(err)
2022-06-10 17:31:30 +08:00
}
2023-03-01 15:32:26 +08:00
err = c.rdb.Expire(ctx, keyList, time.Duration(timeout)*time.Second).Err()
2022-06-10 17:31:30 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
return false, utils.Wrap1(err)
2022-06-10 17:31:30 +08:00
}
2023-03-02 12:00:31 +08:00
key := signalCache + msg.ClientMsgID
2023-03-01 15:32:26 +08:00
err = c.rdb.Set(ctx, key, msg.Content, time.Duration(timeout)*time.Second).Err()
2022-06-10 17:31:30 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
return false, utils.Wrap1(err)
2022-06-10 17:31:30 +08:00
}
2022-06-06 18:52:17 +08:00
}
2022-06-06 18:15:32 +08:00
}
2022-09-02 17:22:27 +08:00
return true, nil
2022-06-06 18:15:32 +08:00
}
2023-03-02 12:00:31 +08:00
func (c *cache) GetSignalInvitationInfoByClientMsgID(ctx context.Context, clientMsgID string) (signalInviteReq *sdkws.SignalInviteReq, err error) {
bytes, err := c.rdb.Get(ctx, signalCache+clientMsgID).Bytes()
2022-06-06 18:15:32 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
return nil, utils.Wrap1(err)
2022-06-06 18:15:32 +08:00
}
2023-03-02 12:00:31 +08:00
signalReq := &sdkws.SignalReq{}
if err = proto.Unmarshal(bytes, signalReq); err != nil {
2023-02-24 11:01:33 +08:00
return nil, utils.Wrap1(err)
2022-06-06 18:15:32 +08:00
}
2023-03-02 12:00:31 +08:00
signalInviteReq = &sdkws.SignalInviteReq{}
switch req := signalReq.Payload.(type) {
case *sdkws.SignalReq_Invite:
signalInviteReq.Invitation = req.Invite.Invitation
signalInviteReq.OpUserID = req.Invite.OpUserID
case *sdkws.SignalReq_InviteInGroup:
signalInviteReq.Invitation = req.InviteInGroup.Invitation
signalInviteReq.OpUserID = req.InviteInGroup.OpUserID
2022-06-06 19:36:25 +08:00
}
2023-03-02 12:00:31 +08:00
return signalInviteReq, nil
2022-06-06 18:15:32 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *sdkws.SignalInviteReq, err error) {
2023-03-02 12:00:31 +08:00
key, err := c.rdb.LPop(ctx, signalListCache+userID).Result()
2022-06-06 19:20:12 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
return nil, utils.Wrap1(err)
2022-06-06 19:20:12 +08:00
}
2023-03-01 15:32:26 +08:00
invitationInfo, err = c.GetSignalInvitationInfoByClientMsgID(ctx, key)
2022-06-06 23:27:16 +08:00
if err != nil {
2023-02-24 11:01:33 +08:00
return nil, err
2022-06-06 23:27:16 +08:00
}
2023-03-01 15:32:26 +08:00
return invitationInfo, utils.Wrap1(c.DelUserSignalList(ctx, userID))
2022-06-06 23:27:16 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) DelUserSignalList(ctx context.Context, userID string) error {
2023-03-02 12:00:31 +08:00
return utils.Wrap1(c.rdb.Del(ctx, signalListCache+userID).Err())
2022-06-06 18:15:32 +08:00
}
2022-06-15 14:57:21 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error {
2022-06-15 14:57:21 +08:00
for _, seq := range seqList {
2023-02-24 11:01:33 +08:00
key := messageCache + userID + "_" + strconv.Itoa(int(seq))
2023-03-01 15:32:26 +08:00
result, err := c.rdb.Get(ctx, key).Result()
2022-08-09 19:44:27 +08:00
if err != nil {
2023-01-29 11:37:11 +08:00
if err == redis.Nil {
2023-02-24 11:01:33 +08:00
continue
2022-08-09 19:44:27 +08:00
}
2023-02-24 11:01:33 +08:00
return utils.Wrap1(err)
2022-08-09 19:44:27 +08:00
}
2023-02-15 15:52:32 +08:00
var msg sdkws.MsgData
2023-02-24 11:01:33 +08:00
if err := jsonpb.UnmarshalString(result, &msg); err != nil {
return err
2022-06-15 14:57:21 +08:00
}
msg.Status = constant.MsgDeleted
s, err := utils.Pb2String(&msg)
if err != nil {
2023-02-24 11:01:33 +08:00
return utils.Wrap1(err)
2022-06-15 14:57:21 +08:00
}
2023-03-01 15:32:26 +08:00
if err := c.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
2023-02-24 11:01:33 +08:00
return utils.Wrap1(err)
2022-06-15 14:57:21 +08:00
}
}
2023-02-24 11:01:33 +08:00
return nil
2022-06-15 14:57:21 +08:00
}
2022-06-15 16:16:35 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) SetGetuiToken(ctx context.Context, token string, expireTime int64) error {
return utils.Wrap1(c.rdb.Set(ctx, getuiToken, token, time.Duration(expireTime)*time.Second).Err())
2022-06-15 16:16:35 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetGetuiToken(ctx context.Context) (string, error) {
return utils.Wrap2(c.rdb.Get(ctx, getuiToken).Result())
2022-06-15 16:16:35 +08:00
}
2022-07-26 15:16:46 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error {
return utils.Wrap1(c.rdb.Set(ctx, getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err())
2022-12-21 16:46:16 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetGetuiTaskID(ctx context.Context) (string, error) {
return utils.Wrap2(c.rdb.Get(ctx, getuiTaskID).Result())
2022-12-21 16:46:16 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetSendMsgStatus(ctx context.Context, id string, status int32) error {
return utils.Wrap1(c.rdb.Set(ctx, sendMsgFailedFlag+id, status, time.Hour*24).Err())
2022-07-26 15:16:46 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
result, err := c.rdb.Get(ctx, sendMsgFailedFlag+id).Int()
2023-02-24 11:01:33 +08:00
return int32(result), utils.Wrap1(err)
2022-07-26 15:16:46 +08:00
}
2022-07-26 15:20:48 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
return utils.Wrap1(c.rdb.Set(ctx, FcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).Err())
2022-07-25 17:57:58 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
return utils.Wrap2(c.rdb.Get(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Result())
2022-12-09 20:53:13 +08:00
}
2022-12-12 19:15:31 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) DelFcmToken(ctx context.Context, account string, platformID int) error {
return utils.Wrap1(c.rdb.Del(ctx, FcmToken+account+":"+strconv.Itoa(platformID)).Err())
2022-12-12 20:41:40 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
seq, err := c.rdb.Incr(ctx, userBadgeUnreadCountSum+userID).Result()
2023-02-24 11:01:33 +08:00
return int(seq), utils.Wrap1(err)
2022-12-12 12:12:53 +08:00
}
2023-02-09 14:40:49 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) SetUserBadgeUnreadCountSum(ctx context.Context, userID string, value int) error {
return utils.Wrap1(c.rdb.Set(ctx, userBadgeUnreadCountSum+userID, value, 0).Err())
2022-12-09 20:53:13 +08:00
}
2023-02-09 14:40:49 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) GetUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
return utils.Wrap2(c.rdb.Get(ctx, userBadgeUnreadCountSum+userID).Int())
2022-12-09 20:53:13 +08:00
}
2023-02-09 14:40:49 +08:00
2023-03-01 15:32:26 +08:00
func (c *cache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
2022-12-09 20:53:13 +08:00
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
2023-03-01 15:32:26 +08:00
return utils.Wrap1(c.rdb.SetNX(ctx, key, 1, time.Minute).Err())
2022-12-09 20:53:13 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
2023-02-24 11:01:33 +08:00
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
2023-03-01 15:32:26 +08:00
return utils.Wrap1(c.rdb.Del(ctx, key).Err())
2022-12-09 20:53:13 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
2022-12-09 20:53:13 +08:00
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 ""
}
2023-02-24 11:01:33 +08:00
2023-03-03 17:42:26 +08:00
func (c *cache) JudgeMessageReactionExist(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
2023-03-01 15:32:26 +08:00
n, err := c.rdb.Exists(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType)).Result()
2023-02-24 11:01:33 +08:00
if err != nil {
return false, utils.Wrap(err, "")
}
return n > 0, nil
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
return utils.Wrap1(c.rdb.HSet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey, value).Err())
2023-02-24 11:01:33 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
return utils.Wrap2(c.rdb.Expire(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), expiration).Result())
2023-02-24 11:01:33 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
return utils.Wrap2(c.rdb.HGet(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), typeKey).Result())
2023-02-24 11:01:33 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
return utils.Wrap2(c.rdb.HGetAll(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType)).Result())
2023-02-24 11:01:33 +08:00
}
2023-03-01 15:32:26 +08:00
func (c *cache) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
return utils.Wrap1(c.rdb.HDel(ctx, c.getMessageReactionExPrefix(clientMsgID, sessionType), subKey).Err())
2023-02-24 11:01:33 +08:00
}