You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/cache/redis.go

564 lines
21 KiB

2 years ago
package cache
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
2 years ago
pbChat "Open_IM/pkg/proto/msg"
pbRtc "Open_IM/pkg/proto/rtc"
2 years ago
"Open_IM/pkg/proto/sdkws"
"Open_IM/pkg/utils"
"context"
"errors"
"fmt"
2 years ago
"strconv"
"time"
2 years ago
"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:"
2 years ago
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:"
)
2 years ago
type Cache interface {
2 years ago
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
2 years ago
AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error
GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error)
2 years ago
SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error
DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error
2 years ago
GetMessageListBySeq(ctx context.Context, userID string, seqList []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
2 years ago
SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) (int, error)
DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error
2 years ago
CleanUpOneUserAllMsg(ctx context.Context, userID string) error
2 years ago
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
2 years ago
DelMsgFromCache(ctx context.Context, userID string, seqList []int64) error
2 years ago
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
2 years ago
}
// native redis operate
2 years ago
//func NewRedis() *RedisClient {
// o := &RedisClient{}
// o.InitRedis()
// return o
//}
2 years ago
2 years ago
func NewRedis() (*RedisClient, error) {
2 years ago
var rdb redis.UniversalClient
2 years ago
if config.Config.Redis.EnableCluster {
2 years ago
rdb = redis.NewClusterClient(&redis.ClusterOptions{
2 years ago
Addrs: config.Config.Redis.DBAddress,
Username: config.Config.Redis.DBUserName,
Password: config.Config.Redis.DBPassWord, // no password set
PoolSize: 50,
})
2 years ago
//if err := rdb.Ping(ctx).Err();err != nil {
// return nil, fmt.Errorf("redis ping %w", err)
//}
//return &RedisClient{rdb: rdb}, nil
2 years ago
} else {
2 years ago
rdb = redis.NewClient(&redis.Options{
2 years ago
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, // 连接池大小
})
2 years ago
//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)
//}
2 years ago
}
2 years ago
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)
2 years ago
}
2 years ago
return &RedisClient{rdb: rdb}, nil
2 years ago
}
2 years ago
type RedisClient struct {
rdb redis.UniversalClient
2 years ago
}
2 years ago
func NewRedisClient(rdb redis.UniversalClient) *RedisClient {
2 years ago
return &RedisClient{rdb: rdb}
}
2 years ago
func (r *RedisClient) GetClient() redis.UniversalClient {
return r.rdb
}
// Perform seq auto-increment operation of user messages
2 years ago
func (r *RedisClient) IncrUserSeq(ctx context.Context, uid string) (int64, error) {
key := userIncrSeq + uid
2 years ago
seq, err := r.rdb.Incr(context.Background(), key).Result()
2 years ago
return seq, err
}
// Get the largest Seq
2 years ago
func (r *RedisClient) GetUserMaxSeq(ctx context.Context, uid string) (int64, error) {
key := userIncrSeq + uid
2 years ago
seq, err := r.rdb.Get(context.Background(), key).Result()
2 years ago
return int64(utils.StringToInt(seq)), err
}
// set the largest Seq
2 years ago
func (r *RedisClient) SetUserMaxSeq(ctx context.Context, uid string, maxSeq int64) error {
key := userIncrSeq + uid
2 years ago
return r.rdb.Set(context.Background(), key, maxSeq, 0).Err()
}
// Set the user's minimum seq
2 years ago
func (r *RedisClient) SetUserMinSeq(ctx context.Context, uid string, minSeq int64) (err error) {
key := userMinSeq + uid
2 years ago
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
}
// Get the smallest Seq
2 years ago
func (r *RedisClient) GetUserMinSeq(ctx context.Context, uid string) (int64, error) {
key := userMinSeq + uid
2 years ago
seq, err := r.rdb.Get(context.Background(), key).Result()
2 years ago
return int64(utils.StringToInt(seq)), err
}
2 years ago
func (r *RedisClient) SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) {
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
2 years ago
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
}
2 years ago
func (r *RedisClient) GetGroupUserMinSeq(ctx context.Context, groupID, userID string) (int64, error) {
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
2 years ago
seq, err := r.rdb.Get(context.Background(), key).Result()
2 years ago
return int64(utils.StringToInt(seq)), err
}
2 years ago
func (r *RedisClient) GetGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
2 years ago
key := groupMaxSeq + groupID
2 years ago
seq, err := r.rdb.Get(context.Background(), key).Result()
2 years ago
return int64(utils.StringToInt(seq)), err
2 years ago
}
2 years ago
func (r *RedisClient) IncrGroupMaxSeq(ctx context.Context, groupID string) (int64, error) {
2 years ago
key := groupMaxSeq + groupID
2 years ago
seq, err := r.rdb.Incr(context.Background(), key).Result()
2 years ago
return seq, err
2 years ago
}
2 years ago
func (r *RedisClient) SetGroupMaxSeq(ctx context.Context, groupID string, maxSeq int64) error {
2 years ago
key := groupMaxSeq + groupID
2 years ago
return r.rdb.Set(context.Background(), key, maxSeq, 0).Err()
2 years ago
}
2 years ago
func (r *RedisClient) SetGroupMinSeq(ctx context.Context, groupID string, minSeq int64) error {
2 years ago
key := groupMinSeq + groupID
2 years ago
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
2 years ago
}
// 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
2 years ago
return r.rdb.HSet(context.Background(), key, token, flag).Err()
}
//key:userID+platform-> <token, flag>
2 years ago
func (r *RedisClient) GetTokenMapByUidPid(ctx context.Context, userID, platformID string) (map[string]int, error) {
key := uidPidToken + userID + ":" + platformID
2 years ago
m, err := r.rdb.HGetAll(context.Background(), 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(context.Background(), key).Result()
if err != nil && err == redis.Nil {
return nil, nil
}
mm := make(map[string]int)
for k, v := range m {
mm[k] = utils.StringToInt(v)
}
return mm, utils.Wrap(err, "")
}
func (r *RedisClient) SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error {
key := uidPidToken + userID + ":" + platform
2 years ago
mm := make(map[string]interface{})
for k, v := range m {
mm[k] = v
}
2 years ago
return r.rdb.HSet(context.Background(), key, mm).Err()
}
2 years ago
func (r *RedisClient) DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error {
key := uidPidToken + userID + ":" + platform
2 years ago
return r.rdb.HDel(context.Background(), key, fields...).Err()
}
2 years ago
func (r *RedisClient) GetMessageListBySeq(ctx context.Context, userID string, seqList []int64, operationID string) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err2 error) {
for _, v := range seqList {
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
key := messageCache + userID + "_" + strconv.Itoa(int(v))
2 years ago
result, err := r.rdb.Get(context.Background(), key).Result()
2 years ago
if err != nil {
2 years ago
if err != redis.Nil {
err2 = err
}
2 years ago
failedSeqList = append(failedSeqList, v)
} else {
2 years ago
msg := sdkws.MsgData{}
2 years ago
err = jsonpb.UnmarshalString(result, &msg)
if err != nil {
2 years ago
err2 = err
2 years ago
failedSeqList = append(failedSeqList, v)
} else {
seqMsg = append(seqMsg, &msg)
}
}
}
2 years ago
return seqMsg, failedSeqList, err2
}
2 years ago
func (r *RedisClient) SetMessageToCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ, uid string) (int, error) {
2 years ago
pipe := r.rdb.Pipeline()
var failedList []pbChat.MsgDataToMQ
for _, msg := range msgList {
key := messageCache + uid + "_" + strconv.Itoa(int(msg.MsgData.Seq))
s, err := utils.Pb2String(msg.MsgData)
if err != nil {
continue
}
err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
2 years ago
//err = r.rdb.HMSet(context.Background(), "12", map[string]interface{}{"1": 2, "343": false}).Err()
if err != nil {
failedList = append(failedList, *msg)
}
}
if len(failedList) != 0 {
2 years ago
return len(failedList), errors.New(fmt.Sprintf("set msg to cache failed, failed lists: %q,%s", failedList))
}
_, err := pipe.Exec(ctx)
2 years ago
return 0, err
}
2 years ago
func (r *RedisClient) DeleteMessageFromCache(ctx context.Context, userID string, msgList []*pbChat.MsgDataToMQ) error {
for _, msg := range msgList {
2 years ago
key := messageCache + userID + "_" + strconv.Itoa(int(msg.MsgData.Seq))
2 years ago
err := r.rdb.Del(ctx, key).Err()
2 years ago
if err != nil {
}
}
2 years ago
return nil
}
2 years ago
func (r *RedisClient) CleanUpOneUserAllMsg(ctx context.Context, userID string) error {
key := messageCache + userID + "_" + "*"
2 years ago
vals, err := r.rdb.Keys(ctx, key).Result()
2 years ago
if err == redis.Nil {
return nil
}
if err != nil {
return utils.Wrap(err, "")
}
2 years ago
for _, v := range vals {
2 years ago
err = r.rdb.Del(ctx, v).Err()
}
return nil
}
2 years ago
func (r *RedisClient) HandleSignalInfo(ctx context.Context, operationID string, msg *sdkws.MsgData, pushToUserID string) (isSend bool, err error) {
req := &pbRtc.SignalReq{}
if err := proto.Unmarshal(msg.Content, req); err != nil {
2 years ago
return false, err
}
//log.NewDebug(pushMsg.OperationID, utils.GetSelfFuncName(), "SignalReq: ", req.String())
var inviteeUserIDList []string
2 years ago
var isInviteSignal bool
switch signalInfo := req.Payload.(type) {
case *pbRtc.SignalReq_Invite:
2 years ago
inviteeUserIDList = signalInfo.Invite.Invitation.InviteeUserIDList
isInviteSignal = true
case *pbRtc.SignalReq_InviteInGroup:
2 years ago
inviteeUserIDList = signalInfo.InviteInGroup.Invitation.InviteeUserIDList
isInviteSignal = true
2 years ago
if !utils.IsContain(pushToUserID, inviteeUserIDList) {
return false, nil
}
2 years ago
case *pbRtc.SignalReq_HungUp, *pbRtc.SignalReq_Cancel, *pbRtc.SignalReq_Reject, *pbRtc.SignalReq_Accept:
2 years ago
return false, nil
default:
2 years ago
return false, nil
}
2 years ago
if isInviteSignal {
for _, userID := range inviteeUserIDList {
timeout, err := strconv.Atoi(config.Config.Rtc.SignalTimeout)
if err != nil {
2 years ago
return false, err
2 years ago
}
2 years ago
keyList := signalListCache + userID
2 years ago
err = r.rdb.LPush(context.Background(), keyList, msg.ClientMsgID).Err()
2 years ago
if err != nil {
2 years ago
return false, err
2 years ago
}
2 years ago
err = r.rdb.Expire(context.Background(), keyList, time.Duration(timeout)*time.Second).Err()
2 years ago
if err != nil {
2 years ago
return false, err
2 years ago
}
2 years ago
key := signalCache + msg.ClientMsgID
2 years ago
err = r.rdb.Set(context.Background(), key, msg.Content, time.Duration(timeout)*time.Second).Err()
2 years ago
if err != nil {
2 years ago
return false, err
2 years ago
}
}
}
2 years ago
return true, nil
}
2 years ago
func (r *RedisClient) GetSignalInfoFromCacheByClientMsgID(ctx context.Context, clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
2 years ago
key := signalCache + clientMsgID
invitationInfo = &pbRtc.SignalInviteReq{}
2 years ago
bytes, err := r.rdb.Get(context.Background(), key).Bytes()
if err != nil {
return nil, err
}
req := &pbRtc.SignalReq{}
if err = proto.Unmarshal(bytes, req); err != nil {
return nil, err
}
switch req2 := req.Payload.(type) {
case *pbRtc.SignalReq_Invite:
invitationInfo.Invitation = req2.Invite.Invitation
2 years ago
invitationInfo.OpUserID = req2.Invite.OpUserID
case *pbRtc.SignalReq_InviteInGroup:
invitationInfo.Invitation = req2.InviteInGroup.Invitation
2 years ago
invitationInfo.OpUserID = req2.InviteInGroup.OpUserID
}
return invitationInfo, err
}
2 years ago
func (r *RedisClient) GetAvailableSignalInvitationInfo(ctx context.Context, userID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
2 years ago
keyList := signalListCache + userID
2 years ago
result := r.rdb.LPop(context.Background(), 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")
}
2 years ago
invitationInfo, err = r.GetSignalInfoFromCacheByClientMsgID(ctx, key)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "GetSignalInfoFromCacheByClientMsgID")
}
2 years ago
err = r.DelUserSignalList(ctx, userID)
2 years ago
if err != nil {
return nil, utils.Wrap(err, "GetSignalInfoFromCacheByClientMsgID")
}
return invitationInfo, nil
}
2 years ago
func (r *RedisClient) DelUserSignalList(ctx context.Context, userID string) error {
2 years ago
keyList := signalListCache + userID
2 years ago
err := r.rdb.Del(context.Background(), keyList).Err()
2 years ago
return err
}
2 years ago
func (r *RedisClient) DelMsgFromCache(ctx context.Context, uid string, seqList []int64, operationID string) {
for _, seq := range seqList {
key := messageCache + uid + "_" + strconv.Itoa(int(seq))
2 years ago
result, err := r.rdb.Get(context.Background(), key).Result()
2 years ago
if err != nil {
2 years ago
if err == redis.Nil {
2 years ago
} else {
}
continue
}
2 years ago
var msg sdkws.MsgData
if err := utils.String2Pb(result, &msg); err != nil {
continue
}
msg.Status = constant.MsgDeleted
s, err := utils.Pb2String(&msg)
if err != nil {
continue
}
2 years ago
if err := r.rdb.Set(context.Background(), key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
}
}
}
2 years ago
2 years ago
func (r *RedisClient) SetGetuiToken(ctx context.Context, token string, expireTime int64) error {
2 years ago
return r.rdb.Set(context.Background(), getuiToken, token, time.Duration(expireTime)*time.Second).Err()
2 years ago
}
2 years ago
func (r *RedisClient) GetGetuiToken(ctx context.Context) (string, error) {
2 years ago
result, err := r.rdb.Get(context.Background(), getuiToken).Result()
2 years ago
return result, err
2 years ago
}
2 years ago
2 years ago
func (r *RedisClient) SetGetuiTaskID(ctx context.Context, taskID string, expireTime int64) error {
2 years ago
return r.rdb.Set(context.Background(), getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err()
2 years ago
}
2 years ago
func (r *RedisClient) GetGetuiTaskID(ctx context.Context) (string, error) {
2 years ago
result, err := r.rdb.Get(context.Background(), getuiTaskID).Result()
2 years ago
return result, err
}
2 years ago
func (r *RedisClient) SetSendMsgStatus(ctx context.Context, status int32, operationID string) error {
2 years ago
return r.rdb.Set(context.Background(), sendMsgFailedFlag+operationID, status, time.Hour*24).Err()
2 years ago
}
2 years ago
func (r *RedisClient) GetSendMsgStatus(ctx context.Context, operationID string) (int, error) {
2 years ago
result, err := r.rdb.Get(context.Background(), sendMsgFailedFlag+operationID).Result()
2 years ago
if err != nil {
return 0, err
}
status, err := strconv.Atoi(result)
return status, err
2 years ago
}
2 years ago
2 years ago
func (r *RedisClient) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
key := FcmToken + account + ":" + strconv.Itoa(platformID)
2 years ago
return r.rdb.Set(context.Background(), key, fcmToken, time.Duration(expireTime)*time.Second).Err()
2 years ago
}
2 years ago
func (r *RedisClient) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
key := FcmToken + account + ":" + strconv.Itoa(platformID)
2 years ago
return r.rdb.Get(context.Background(), key).Result()
2 years ago
}
2 years ago
func (r *RedisClient) DelFcmToken(ctx context.Context, account string, platformID int) error {
key := FcmToken + account + ":" + strconv.Itoa(platformID)
2 years ago
return r.rdb.Del(context.Background(), key).Err()
}
2 years ago
func (r *RedisClient) IncrUserBadgeUnreadCountSum(ctx context.Context, uid string) (int, error) {
key := userBadgeUnreadCountSum + uid
2 years ago
seq, err := r.rdb.Incr(context.Background(), key).Result()
return int(seq), err
}
2 years ago
func (r *RedisClient) SetUserBadgeUnreadCountSum(ctx context.Context, uid string, value int) error {
key := userBadgeUnreadCountSum + uid
2 years ago
return r.rdb.Set(context.Background(), key, value, 0).Err()
}
2 years ago
func (r *RedisClient) GetUserBadgeUnreadCountSum(ctx context.Context, uid string) (int, error) {
2 years ago
key := userBadgeUnreadCountSum + uid
2 years ago
seq, err := r.rdb.Get(context.Background(), key).Result()
2 years ago
return utils.StringToInt(seq), err
}
2 years ago
func (r *RedisClient) JudgeMessageReactionEXISTS(ctx context.Context, clientMsgID string, sessionType int32) (bool, error) {
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
2 years ago
n, err := r.rdb.Exists(context.Background(), key).Result()
2 years ago
return n > 0, err
2 years ago
}
2 years ago
func (r *RedisClient) GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error) {
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
2 years ago
return r.rdb.HGetAll(context.Background(), key).Result()
}
2 years ago
func (r *RedisClient) DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error {
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
2 years ago
return r.rdb.HDel(context.Background(), key, subKey).Err()
}
2 years ago
func (r *RedisClient) SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
2 years ago
return r.rdb.Expire(context.Background(), key, expiration).Result()
}
2 years ago
2 years ago
func (r *RedisClient) GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error) {
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
2 years ago
result, err := r.rdb.HGet(context.Background(), key, typeKey).Result()
2 years ago
return result, err
}
2 years ago
2 years ago
func (r *RedisClient) SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error {
key := r.getMessageReactionExPrefix(clientMsgID, sessionType)
2 years ago
return r.rdb.HSet(context.Background(), key, typeKey, value).Err()
2 years ago
}
2 years ago
2 years ago
func (r *RedisClient) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
2 years ago
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
2 years ago
return r.rdb.SetNX(context.Background(), key, 1, time.Minute).Err()
2 years ago
}
2 years ago
func (r *RedisClient) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
2 years ago
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
2 years ago
return r.rdb.Del(context.Background(), key).Err()
2 years ago
}
2 years ago
func (r *RedisClient) getMessageReactionExPrefix(clientMsgID string, sessionType int32) string {
2 years ago
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 ""
}