|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
package db
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"Open_IM/pkg/common/config"
|
|
|
|
@ -42,135 +42,175 @@ const (
|
|
|
|
|
exTypeKeyLocker = "EX_LOCK:"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) JudgeAccountEXISTS(account string) (bool, error) {
|
|
|
|
|
func InitRedis(ctx context.Context) go_redis.UniversalClient {
|
|
|
|
|
var rdb go_redis.UniversalClient
|
|
|
|
|
var err error
|
|
|
|
|
if config.Config.Redis.EnableCluster {
|
|
|
|
|
rdb = go_redis.NewClusterClient(&go_redis.ClusterOptions{
|
|
|
|
|
Addrs: config.Config.Redis.DBAddress,
|
|
|
|
|
Username: config.Config.Redis.DBUserName,
|
|
|
|
|
Password: config.Config.Redis.DBPassWord, // no password set
|
|
|
|
|
PoolSize: 50,
|
|
|
|
|
})
|
|
|
|
|
_, err = rdb.Ping(ctx).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("redis cluster failed address ", config.Config.Redis.DBAddress)
|
|
|
|
|
panic(err.Error() + " redis cluster " + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
rdb = go_redis.NewClient(&go_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).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err.Error() + " redis " + config.Config.Redis.DBAddress[0] + config.Config.Redis.DBUserName + config.Config.Redis.DBPassWord)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rdb
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewRedisClient(rdb go_redis.UniversalClient) *RedisClient {
|
|
|
|
|
return &RedisClient{rdb: rdb}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type RedisClient struct {
|
|
|
|
|
rdb go_redis.UniversalClient
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *RedisClient) JudgeAccountEXISTS(account string) (bool, error) {
|
|
|
|
|
key := accountTempCode + account
|
|
|
|
|
n, err := d.RDB.Exists(context.Background(), key).Result()
|
|
|
|
|
n, err := r.rdb.Exists(context.Background(), key).Result()
|
|
|
|
|
if n > 0 {
|
|
|
|
|
return true, err
|
|
|
|
|
} else {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) SetAccountCode(account string, code, ttl int) (err error) {
|
|
|
|
|
|
|
|
|
|
func (r *RedisClient) SetAccountCode(account string, code, ttl int) (err error) {
|
|
|
|
|
key := accountTempCode + account
|
|
|
|
|
return d.RDB.Set(context.Background(), key, code, time.Duration(ttl)*time.Second).Err()
|
|
|
|
|
return r.rdb.Set(context.Background(), key, code, time.Duration(ttl)*time.Second).Err()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) GetAccountCode(account string) (string, error) {
|
|
|
|
|
func (r *RedisClient) GetAccountCode(account string) (string, error) {
|
|
|
|
|
key := accountTempCode + account
|
|
|
|
|
return d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
return r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Perform seq auto-increment operation of user messages
|
|
|
|
|
func (d *DataBases) IncrUserSeq(uid string) (uint64, error) {
|
|
|
|
|
func (r *RedisClient) IncrUserSeq(uid string) (uint64, error) {
|
|
|
|
|
key := userIncrSeq + uid
|
|
|
|
|
seq, err := d.RDB.Incr(context.Background(), key).Result()
|
|
|
|
|
seq, err := r.rdb.Incr(context.Background(), key).Result()
|
|
|
|
|
return uint64(seq), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Get the largest Seq
|
|
|
|
|
func (d *DataBases) GetUserMaxSeq(uid string) (uint64, error) {
|
|
|
|
|
func (r *RedisClient) GetUserMaxSeq(uid string) (uint64, error) {
|
|
|
|
|
key := userIncrSeq + uid
|
|
|
|
|
seq, err := d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
seq, err := r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
return uint64(utils.StringToInt(seq)), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//set the largest Seq
|
|
|
|
|
func (d *DataBases) SetUserMaxSeq(uid string, maxSeq uint64) error {
|
|
|
|
|
func (r *RedisClient) SetUserMaxSeq(uid string, maxSeq uint64) error {
|
|
|
|
|
key := userIncrSeq + uid
|
|
|
|
|
return d.RDB.Set(context.Background(), key, maxSeq, 0).Err()
|
|
|
|
|
return r.rdb.Set(context.Background(), key, maxSeq, 0).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Set the user's minimum seq
|
|
|
|
|
func (d *DataBases) SetUserMinSeq(uid string, minSeq uint32) (err error) {
|
|
|
|
|
func (r *RedisClient) SetUserMinSeq(uid string, minSeq uint32) (err error) {
|
|
|
|
|
key := userMinSeq + uid
|
|
|
|
|
return d.RDB.Set(context.Background(), key, minSeq, 0).Err()
|
|
|
|
|
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Get the smallest Seq
|
|
|
|
|
func (d *DataBases) GetUserMinSeq(uid string) (uint64, error) {
|
|
|
|
|
func (r *RedisClient) GetUserMinSeq(uid string) (uint64, error) {
|
|
|
|
|
key := userMinSeq + uid
|
|
|
|
|
seq, err := d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
seq, err := r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
return uint64(utils.StringToInt(seq)), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) SetGroupUserMinSeq(groupID, userID string, minSeq uint64) (err error) {
|
|
|
|
|
func (r *RedisClient) SetGroupUserMinSeq(groupID, userID string, minSeq uint64) (err error) {
|
|
|
|
|
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
|
|
|
|
|
return d.RDB.Set(context.Background(), key, minSeq, 0).Err()
|
|
|
|
|
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) GetGroupUserMinSeq(groupID, userID string) (uint64, error) {
|
|
|
|
|
func (r *RedisClient) GetGroupUserMinSeq(groupID, userID string) (uint64, error) {
|
|
|
|
|
key := groupUserMinSeq + "g:" + groupID + "u:" + userID
|
|
|
|
|
seq, err := d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
seq, err := r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
return uint64(utils.StringToInt(seq)), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetGroupMaxSeq(groupID string) (uint64, error) {
|
|
|
|
|
func (r *RedisClient) GetGroupMaxSeq(groupID string) (uint64, error) {
|
|
|
|
|
key := groupMaxSeq + groupID
|
|
|
|
|
seq, err := d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
seq, err := r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
return uint64(utils.StringToInt(seq)), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) IncrGroupMaxSeq(groupID string) (uint64, error) {
|
|
|
|
|
func (r *RedisClient) IncrGroupMaxSeq(groupID string) (uint64, error) {
|
|
|
|
|
key := groupMaxSeq + groupID
|
|
|
|
|
seq, err := d.RDB.Incr(context.Background(), key).Result()
|
|
|
|
|
seq, err := r.rdb.Incr(context.Background(), key).Result()
|
|
|
|
|
return uint64(seq), err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) SetGroupMaxSeq(groupID string, maxSeq uint64) error {
|
|
|
|
|
func (r *RedisClient) SetGroupMaxSeq(groupID string, maxSeq uint64) error {
|
|
|
|
|
key := groupMaxSeq + groupID
|
|
|
|
|
return d.RDB.Set(context.Background(), key, maxSeq, 0).Err()
|
|
|
|
|
return r.rdb.Set(context.Background(), key, maxSeq, 0).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) SetGroupMinSeq(groupID string, minSeq uint32) error {
|
|
|
|
|
func (r *RedisClient) SetGroupMinSeq(groupID string, minSeq uint32) error {
|
|
|
|
|
key := groupMinSeq + groupID
|
|
|
|
|
return d.RDB.Set(context.Background(), key, minSeq, 0).Err()
|
|
|
|
|
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Store userid and platform class to redis
|
|
|
|
|
func (d *DataBases) AddTokenFlag(userID string, platformID int, token string, flag int) error {
|
|
|
|
|
func (r *RedisClient) AddTokenFlag(userID string, platformID int, token string, flag int) error {
|
|
|
|
|
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
|
|
|
|
log2.NewDebug("", "add token key is ", key)
|
|
|
|
|
return d.RDB.HSet(context.Background(), key, token, flag).Err()
|
|
|
|
|
return r.rdb.HSet(context.Background(), key, token, flag).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetTokenMapByUidPid(userID, platformID string) (map[string]int, error) {
|
|
|
|
|
func (r *RedisClient) GetTokenMapByUidPid(userID, platformID string) (map[string]int, error) {
|
|
|
|
|
key := uidPidToken + userID + ":" + platformID
|
|
|
|
|
log2.NewDebug("", "get token key is ", key)
|
|
|
|
|
m, err := d.RDB.HGetAll(context.Background(), key).Result()
|
|
|
|
|
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 (d *DataBases) SetTokenMapByUidPid(userID string, platformID int, m map[string]int) error {
|
|
|
|
|
func (r *RedisClient) SetTokenMapByUidPid(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 d.RDB.HSet(context.Background(), key, mm).Err()
|
|
|
|
|
return r.rdb.HSet(context.Background(), key, mm).Err()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) DeleteTokenByUidPid(userID string, platformID int, fields []string) error {
|
|
|
|
|
func (r *RedisClient) DeleteTokenByUidPid(userID string, platformID int, fields []string) error {
|
|
|
|
|
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
|
|
|
|
return d.RDB.HDel(context.Background(), key, fields...).Err()
|
|
|
|
|
return r.rdb.HDel(context.Background(), key, fields...).Err()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) SetSingleConversationRecvMsgOpt(userID, conversationID string, opt int32) error {
|
|
|
|
|
func (r *RedisClient) SetSingleConversationRecvMsgOpt(userID, conversationID string, opt int32) error {
|
|
|
|
|
key := conversationReceiveMessageOpt + userID
|
|
|
|
|
return d.RDB.HSet(context.Background(), key, conversationID, opt).Err()
|
|
|
|
|
return r.rdb.HSet(context.Background(), key, conversationID, opt).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetSingleConversationRecvMsgOpt(userID, conversationID string) (int, error) {
|
|
|
|
|
func (r *RedisClient) GetSingleConversationRecvMsgOpt(userID, conversationID string) (int, error) {
|
|
|
|
|
key := conversationReceiveMessageOpt + userID
|
|
|
|
|
result, err := d.RDB.HGet(context.Background(), key, conversationID).Result()
|
|
|
|
|
result, err := r.rdb.HGet(context.Background(), key, conversationID).Result()
|
|
|
|
|
return utils.StringToInt(result), err
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) SetUserGlobalMsgRecvOpt(userID string, opt int32) error {
|
|
|
|
|
func (r *RedisClient) SetUserGlobalMsgRecvOpt(userID string, opt int32) error {
|
|
|
|
|
key := conversationReceiveMessageOpt + userID
|
|
|
|
|
return d.RDB.HSet(context.Background(), key, GlobalMsgRecvOpt, opt).Err()
|
|
|
|
|
return r.rdb.HSet(context.Background(), key, GlobalMsgRecvOpt, opt).Err()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) GetUserGlobalMsgRecvOpt(userID string) (int, error) {
|
|
|
|
|
func (r *RedisClient) GetUserGlobalMsgRecvOpt(userID string) (int, error) {
|
|
|
|
|
key := conversationReceiveMessageOpt + userID
|
|
|
|
|
result, err := d.RDB.HGet(context.Background(), key, GlobalMsgRecvOpt).Result()
|
|
|
|
|
result, err := r.rdb.HGet(context.Background(), key, GlobalMsgRecvOpt).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == go_redis.Nil {
|
|
|
|
|
return 0, nil
|
|
|
|
@ -180,11 +220,11 @@ func (d *DataBases) GetUserGlobalMsgRecvOpt(userID string) (int, error) {
|
|
|
|
|
}
|
|
|
|
|
return utils.StringToInt(result), err
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) GetMessageListBySeq(userID string, seqList []uint32, operationID string) (seqMsg []*pbCommon.MsgData, failedSeqList []uint32, errResult error) {
|
|
|
|
|
func (r *RedisClient) GetMessageListBySeq(userID string, seqList []uint32, operationID string) (seqMsg []*pbCommon.MsgData, failedSeqList []uint32, errResult error) {
|
|
|
|
|
for _, v := range seqList {
|
|
|
|
|
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
|
|
|
|
|
key := messageCache + userID + "_" + strconv.Itoa(int(v))
|
|
|
|
|
result, err := d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
result, err := r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
errResult = err
|
|
|
|
|
failedSeqList = append(failedSeqList, v)
|
|
|
|
@ -206,9 +246,9 @@ func (d *DataBases) GetMessageListBySeq(userID string, seqList []uint32, operati
|
|
|
|
|
return seqMsg, failedSeqList, errResult
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) SetMessageToCache(msgList []*pbChat.MsgDataToMQ, uid string, operationID string) (error, int) {
|
|
|
|
|
func (r *RedisClient) SetMessageToCache(msgList []*pbChat.MsgDataToMQ, uid string, operationID string) (error, int) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
pipe := d.RDB.Pipeline()
|
|
|
|
|
pipe := r.rdb.Pipeline()
|
|
|
|
|
var failedList []pbChat.MsgDataToMQ
|
|
|
|
|
for _, msg := range msgList {
|
|
|
|
|
key := messageCache + uid + "_" + strconv.Itoa(int(msg.MsgData.Seq))
|
|
|
|
@ -219,7 +259,7 @@ func (d *DataBases) SetMessageToCache(msgList []*pbChat.MsgDataToMQ, uid string,
|
|
|
|
|
}
|
|
|
|
|
log2.NewDebug(operationID, "convert string is ", s)
|
|
|
|
|
err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
|
|
|
|
|
//err = d.rdb.HMSet(context.Background(), "12", map[string]interface{}{"1": 2, "343": false}).Err()
|
|
|
|
|
//err = r.rdb.HMSet(context.Background(), "12", map[string]interface{}{"1": 2, "343": false}).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log2.NewWarn(operationID, utils.GetSelfFuncName(), "redis failed", "args:", key, *msg, uid, s, err.Error())
|
|
|
|
|
failedList = append(failedList, *msg)
|
|
|
|
@ -231,11 +271,11 @@ func (d *DataBases) SetMessageToCache(msgList []*pbChat.MsgDataToMQ, uid string,
|
|
|
|
|
_, err := pipe.Exec(ctx)
|
|
|
|
|
return err, 0
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) DeleteMessageFromCache(msgList []*pbChat.MsgDataToMQ, uid string, operationID string) error {
|
|
|
|
|
func (r *RedisClient) DeleteMessageFromCache(msgList []*pbChat.MsgDataToMQ, uid string, operationID string) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
for _, msg := range msgList {
|
|
|
|
|
key := messageCache + uid + "_" + strconv.Itoa(int(msg.MsgData.Seq))
|
|
|
|
|
err := d.RDB.Del(ctx, key).Err()
|
|
|
|
|
err := r.rdb.Del(ctx, key).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log2.NewWarn(operationID, utils.GetSelfFuncName(), "redis failed", "args:", key, uid, err.Error(), msgList)
|
|
|
|
|
}
|
|
|
|
@ -243,10 +283,10 @@ func (d *DataBases) DeleteMessageFromCache(msgList []*pbChat.MsgDataToMQ, uid st
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) CleanUpOneUserAllMsgFromRedis(userID string, operationID string) error {
|
|
|
|
|
func (r *RedisClient) CleanUpOneUserAllMsgFromRedis(userID string, operationID string) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
key := messageCache + userID + "_" + "*"
|
|
|
|
|
vals, err := d.RDB.Keys(ctx, key).Result()
|
|
|
|
|
vals, err := r.rdb.Keys(ctx, key).Result()
|
|
|
|
|
log2.Debug(operationID, "vals: ", vals)
|
|
|
|
|
if err == go_redis.Nil {
|
|
|
|
|
return nil
|
|
|
|
@ -255,12 +295,12 @@ func (d *DataBases) CleanUpOneUserAllMsgFromRedis(userID string, operationID str
|
|
|
|
|
return utils.Wrap(err, "")
|
|
|
|
|
}
|
|
|
|
|
for _, v := range vals {
|
|
|
|
|
err = d.RDB.Del(ctx, v).Err()
|
|
|
|
|
err = r.rdb.Del(ctx, v).Err()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) HandleSignalInfo(operationID string, msg *pbCommon.MsgData, pushToUserID string) (isSend bool, err error) {
|
|
|
|
|
func (r *RedisClient) HandleSignalInfo(operationID string, msg *pbCommon.MsgData, pushToUserID string) (isSend bool, err error) {
|
|
|
|
|
req := &pbRtc.SignalReq{}
|
|
|
|
|
if err := proto.Unmarshal(msg.Content, req); err != nil {
|
|
|
|
|
return false, err
|
|
|
|
@ -293,16 +333,16 @@ func (d *DataBases) HandleSignalInfo(operationID string, msg *pbCommon.MsgData,
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
keyList := SignalListCache + userID
|
|
|
|
|
err = d.RDB.LPush(context.Background(), keyList, msg.ClientMsgID).Err()
|
|
|
|
|
err = r.rdb.LPush(context.Background(), keyList, msg.ClientMsgID).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
err = d.RDB.Expire(context.Background(), keyList, time.Duration(timeout)*time.Second).Err()
|
|
|
|
|
err = r.rdb.Expire(context.Background(), keyList, time.Duration(timeout)*time.Second).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
key := SignalCache + msg.ClientMsgID
|
|
|
|
|
err = d.RDB.Set(context.Background(), key, msg.Content, time.Duration(timeout)*time.Second).Err()
|
|
|
|
|
err = r.rdb.Set(context.Background(), key, msg.Content, time.Duration(timeout)*time.Second).Err()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
@ -311,10 +351,10 @@ func (d *DataBases) HandleSignalInfo(operationID string, msg *pbCommon.MsgData,
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetSignalInfoFromCacheByClientMsgID(clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
|
|
|
|
func (r *RedisClient) GetSignalInfoFromCacheByClientMsgID(clientMsgID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
|
|
|
|
key := SignalCache + clientMsgID
|
|
|
|
|
invitationInfo = &pbRtc.SignalInviteReq{}
|
|
|
|
|
bytes, err := d.RDB.Get(context.Background(), key).Bytes()
|
|
|
|
|
bytes, err := r.rdb.Get(context.Background(), key).Bytes()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -333,9 +373,9 @@ func (d *DataBases) GetSignalInfoFromCacheByClientMsgID(clientMsgID string) (inv
|
|
|
|
|
return invitationInfo, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetAvailableSignalInvitationInfo(userID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
|
|
|
|
func (r *RedisClient) GetAvailableSignalInvitationInfo(userID string) (invitationInfo *pbRtc.SignalInviteReq, err error) {
|
|
|
|
|
keyList := SignalListCache + userID
|
|
|
|
|
result := d.RDB.LPop(context.Background(), keyList)
|
|
|
|
|
result := r.rdb.LPop(context.Background(), keyList)
|
|
|
|
|
if err = result.Err(); err != nil {
|
|
|
|
|
return nil, utils.Wrap(err, "GetAvailableSignalInvitationInfo failed")
|
|
|
|
|
}
|
|
|
|
@ -344,27 +384,27 @@ func (d *DataBases) GetAvailableSignalInvitationInfo(userID string) (invitationI
|
|
|
|
|
return nil, utils.Wrap(err, "GetAvailableSignalInvitationInfo failed")
|
|
|
|
|
}
|
|
|
|
|
log2.NewDebug("", utils.GetSelfFuncName(), result, result.String())
|
|
|
|
|
invitationInfo, err = d.GetSignalInfoFromCacheByClientMsgID(key)
|
|
|
|
|
invitationInfo, err = r.GetSignalInfoFromCacheByClientMsgID(key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, utils.Wrap(err, "GetSignalInfoFromCacheByClientMsgID")
|
|
|
|
|
}
|
|
|
|
|
err = d.DelUserSignalList(userID)
|
|
|
|
|
err = r.DelUserSignalList(userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, utils.Wrap(err, "GetSignalInfoFromCacheByClientMsgID")
|
|
|
|
|
}
|
|
|
|
|
return invitationInfo, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) DelUserSignalList(userID string) error {
|
|
|
|
|
func (r *RedisClient) DelUserSignalList(userID string) error {
|
|
|
|
|
keyList := SignalListCache + userID
|
|
|
|
|
err := d.RDB.Del(context.Background(), keyList).Err()
|
|
|
|
|
err := r.rdb.Del(context.Background(), keyList).Err()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) DelMsgFromCache(uid string, seqList []uint32, operationID string) {
|
|
|
|
|
func (r *RedisClient) DelMsgFromCache(uid string, seqList []uint32, operationID string) {
|
|
|
|
|
for _, seq := range seqList {
|
|
|
|
|
key := messageCache + uid + "_" + strconv.Itoa(int(seq))
|
|
|
|
|
result, err := d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
result, err := r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == go_redis.Nil {
|
|
|
|
|
log2.NewDebug(operationID, utils.GetSelfFuncName(), err.Error(), "redis nil")
|
|
|
|
@ -384,36 +424,36 @@ func (d *DataBases) DelMsgFromCache(uid string, seqList []uint32, operationID st
|
|
|
|
|
log2.Error(operationID, utils.GetSelfFuncName(), "Pb2String failed", msg, err.Error())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err := d.RDB.Set(context.Background(), key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
|
|
|
|
if err := r.rdb.Set(context.Background(), key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
|
|
|
|
log2.Error(operationID, utils.GetSelfFuncName(), "Set failed", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) SetGetuiToken(token string, expireTime int64) error {
|
|
|
|
|
return d.RDB.Set(context.Background(), getuiToken, token, time.Duration(expireTime)*time.Second).Err()
|
|
|
|
|
func (r *RedisClient) SetGetuiToken(token string, expireTime int64) error {
|
|
|
|
|
return r.rdb.Set(context.Background(), getuiToken, token, time.Duration(expireTime)*time.Second).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetGetuiToken() (string, error) {
|
|
|
|
|
result, err := d.RDB.Get(context.Background(), getuiToken).Result()
|
|
|
|
|
func (r *RedisClient) GetGetuiToken() (string, error) {
|
|
|
|
|
result, err := r.rdb.Get(context.Background(), getuiToken).Result()
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) SetGetuiTaskID(taskID string, expireTime int64) error {
|
|
|
|
|
return d.RDB.Set(context.Background(), getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err()
|
|
|
|
|
func (r *RedisClient) SetGetuiTaskID(taskID string, expireTime int64) error {
|
|
|
|
|
return r.rdb.Set(context.Background(), getuiTaskID, taskID, time.Duration(expireTime)*time.Second).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetGetuiTaskID() (string, error) {
|
|
|
|
|
result, err := d.RDB.Get(context.Background(), getuiTaskID).Result()
|
|
|
|
|
func (r *RedisClient) GetGetuiTaskID() (string, error) {
|
|
|
|
|
result, err := r.rdb.Get(context.Background(), getuiTaskID).Result()
|
|
|
|
|
return result, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) SetSendMsgStatus(status int32, operationID string) error {
|
|
|
|
|
return d.RDB.Set(context.Background(), sendMsgFailedFlag+operationID, status, time.Hour*24).Err()
|
|
|
|
|
func (r *RedisClient) SetSendMsgStatus(status int32, operationID string) error {
|
|
|
|
|
return r.rdb.Set(context.Background(), sendMsgFailedFlag+operationID, status, time.Hour*24).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetSendMsgStatus(operationID string) (int, error) {
|
|
|
|
|
result, err := d.RDB.Get(context.Background(), sendMsgFailedFlag+operationID).Result()
|
|
|
|
|
func (r *RedisClient) GetSendMsgStatus(operationID string) (int, error) {
|
|
|
|
|
result, err := r.rdb.Get(context.Background(), sendMsgFailedFlag+operationID).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
@ -421,36 +461,36 @@ func (d *DataBases) GetSendMsgStatus(operationID string) (int, error) {
|
|
|
|
|
return status, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) SetFcmToken(account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
|
|
|
|
func (r *RedisClient) SetFcmToken(account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
|
|
|
|
key := FcmToken + account + ":" + strconv.Itoa(platformID)
|
|
|
|
|
return d.RDB.Set(context.Background(), key, fcmToken, time.Duration(expireTime)*time.Second).Err()
|
|
|
|
|
return r.rdb.Set(context.Background(), key, fcmToken, time.Duration(expireTime)*time.Second).Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetFcmToken(account string, platformID int) (string, error) {
|
|
|
|
|
func (r *RedisClient) GetFcmToken(account string, platformID int) (string, error) {
|
|
|
|
|
key := FcmToken + account + ":" + strconv.Itoa(platformID)
|
|
|
|
|
return d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
return r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) DelFcmToken(account string, platformID int) error {
|
|
|
|
|
func (r *RedisClient) DelFcmToken(account string, platformID int) error {
|
|
|
|
|
key := FcmToken + account + ":" + strconv.Itoa(platformID)
|
|
|
|
|
return d.RDB.Del(context.Background(), key).Err()
|
|
|
|
|
return r.rdb.Del(context.Background(), key).Err()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) IncrUserBadgeUnreadCountSum(uid string) (int, error) {
|
|
|
|
|
func (r *RedisClient) IncrUserBadgeUnreadCountSum(uid string) (int, error) {
|
|
|
|
|
key := userBadgeUnreadCountSum + uid
|
|
|
|
|
seq, err := d.RDB.Incr(context.Background(), key).Result()
|
|
|
|
|
seq, err := r.rdb.Incr(context.Background(), key).Result()
|
|
|
|
|
return int(seq), err
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) SetUserBadgeUnreadCountSum(uid string, value int) error {
|
|
|
|
|
func (r *RedisClient) SetUserBadgeUnreadCountSum(uid string, value int) error {
|
|
|
|
|
key := userBadgeUnreadCountSum + uid
|
|
|
|
|
return d.RDB.Set(context.Background(), key, value, 0).Err()
|
|
|
|
|
return r.rdb.Set(context.Background(), key, value, 0).Err()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) GetUserBadgeUnreadCountSum(uid string) (int, error) {
|
|
|
|
|
func (r *RedisClient) GetUserBadgeUnreadCountSum(uid string) (int, error) {
|
|
|
|
|
key := userBadgeUnreadCountSum + uid
|
|
|
|
|
seq, err := d.RDB.Get(context.Background(), key).Result()
|
|
|
|
|
seq, err := r.rdb.Get(context.Background(), key).Result()
|
|
|
|
|
return utils.StringToInt(seq), err
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) JudgeMessageReactionEXISTS(clientMsgID string, sessionType int32) (bool, error) {
|
|
|
|
|
func (r *RedisClient) JudgeMessageReactionEXISTS(clientMsgID string, sessionType int32) (bool, error) {
|
|
|
|
|
key := getMessageReactionExPrefix(clientMsgID, sessionType)
|
|
|
|
|
n, err := d.RDB.Exists(context.Background(), key).Result()
|
|
|
|
|
n, err := r.rdb.Exists(context.Background(), key).Result()
|
|
|
|
|
if n > 0 {
|
|
|
|
|
return true, err
|
|
|
|
|
} else {
|
|
|
|
@ -458,38 +498,38 @@ func (d *DataBases) JudgeMessageReactionEXISTS(clientMsgID string, sessionType i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DataBases) GetOneMessageAllReactionList(clientMsgID string, sessionType int32) (map[string]string, error) {
|
|
|
|
|
func (r *RedisClient) GetOneMessageAllReactionList(clientMsgID string, sessionType int32) (map[string]string, error) {
|
|
|
|
|
key := getMessageReactionExPrefix(clientMsgID, sessionType)
|
|
|
|
|
return d.RDB.HGetAll(context.Background(), key).Result()
|
|
|
|
|
return r.rdb.HGetAll(context.Background(), key).Result()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) DeleteOneMessageKey(clientMsgID string, sessionType int32, subKey string) error {
|
|
|
|
|
func (r *RedisClient) DeleteOneMessageKey(clientMsgID string, sessionType int32, subKey string) error {
|
|
|
|
|
key := getMessageReactionExPrefix(clientMsgID, sessionType)
|
|
|
|
|
return d.RDB.HDel(context.Background(), key, subKey).Err()
|
|
|
|
|
return r.rdb.HDel(context.Background(), key, subKey).Err()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) SetMessageReactionExpire(clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
|
|
|
|
func (r *RedisClient) SetMessageReactionExpire(clientMsgID string, sessionType int32, expiration time.Duration) (bool, error) {
|
|
|
|
|
key := getMessageReactionExPrefix(clientMsgID, sessionType)
|
|
|
|
|
return d.RDB.Expire(context.Background(), key, expiration).Result()
|
|
|
|
|
return r.rdb.Expire(context.Background(), key, expiration).Result()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) GetMessageTypeKeyValue(clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
|
|
|
|
func (r *RedisClient) GetMessageTypeKeyValue(clientMsgID string, sessionType int32, typeKey string) (string, error) {
|
|
|
|
|
key := getMessageReactionExPrefix(clientMsgID, sessionType)
|
|
|
|
|
result, err := d.RDB.HGet(context.Background(), key, typeKey).Result()
|
|
|
|
|
result, err := r.rdb.HGet(context.Background(), key, typeKey).Result()
|
|
|
|
|
return result, err
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) SetMessageTypeKeyValue(clientMsgID string, sessionType int32, typeKey, value string) error {
|
|
|
|
|
func (r *RedisClient) SetMessageTypeKeyValue(clientMsgID string, sessionType int32, typeKey, value string) error {
|
|
|
|
|
key := getMessageReactionExPrefix(clientMsgID, sessionType)
|
|
|
|
|
return d.RDB.HSet(context.Background(), key, typeKey, value).Err()
|
|
|
|
|
return r.rdb.HSet(context.Background(), key, typeKey, value).Err()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) LockMessageTypeKey(clientMsgID string, TypeKey string) error {
|
|
|
|
|
func (r *RedisClient) LockMessageTypeKey(clientMsgID string, TypeKey string) error {
|
|
|
|
|
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
|
|
|
|
return d.RDB.SetNX(context.Background(), key, 1, time.Minute).Err()
|
|
|
|
|
return r.rdb.SetNX(context.Background(), key, 1, time.Minute).Err()
|
|
|
|
|
}
|
|
|
|
|
func (d *DataBases) UnLockMessageTypeKey(clientMsgID string, TypeKey string) error {
|
|
|
|
|
func (r *RedisClient) UnLockMessageTypeKey(clientMsgID string, TypeKey string) error {
|
|
|
|
|
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
|
|
|
|
return d.RDB.Del(context.Background(), key).Err()
|
|
|
|
|
return r.rdb.Del(context.Background(), key).Err()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|