redis replace go redis

This commit is contained in:
Gordon 2022-05-31 15:13:59 +08:00 committed by Xinwei Xiong(cubxxw-openim)
parent 85b9a2cc39
commit b9597a139b
2 changed files with 56 additions and 0 deletions

View File

@ -4,10 +4,13 @@ import (
"Open_IM/pkg/common/config"
log2 "Open_IM/pkg/common/log"
pbChat "Open_IM/pkg/proto/chat"
pbCommon "Open_IM/pkg/proto/sdk_ws"
"Open_IM/pkg/utils"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/golang/protobuf/proto"
"strconv"
"time"
)
@ -19,6 +22,46 @@ import (
// d.rdb.Publish(context.Background(),channel,msg)
//}
func (d *DataBases) NewGetMessageListBySeq(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.HGetAll(context.Background(), key).Result()
if err != nil {
errResult = err
failedSeqList = append(failedSeqList, v)
log2.NewWarn(operationID, "redis get message error:", err.Error(), v)
} else {
msg, err := Map2Pb(result)
//msg := pbCommon.MsgData{}
//err = jsonpb.UnmarshalString(result, &msg)
if err != nil {
errResult = err
failedSeqList = append(failedSeqList, v)
log2.NewWarn(operationID, "Unmarshal err", result, err.Error())
} else {
log2.NewDebug(operationID, "redis get msg is ", msg.String())
seqMsg = append(seqMsg, msg)
}
}
}
return seqMsg, failedSeqList, errResult
}
func Map2Pb(m map[string]string) (*pbCommon.MsgData, error) {
var data pbCommon.MsgData
b, err := json.Marshal(m)
if err != nil {
return nil, err
}
err = proto.Unmarshal(b, &data)
if err != nil {
return nil, err
}
return &data, nil
}
func (d *DataBases) NewSetMessageToCache(msgList []*pbChat.MsgDataToMQ, uid string, operationID string) error {
ctx := context.Background()
var failedList []pbChat.MsgDataToMQ

View File

@ -57,3 +57,16 @@ func Test_NewSetMessageToCache(t *testing.T) {
assert.Nil(t, err)
}
func Test_NewGetMessageListBySeq(t *testing.T) {
var msg pbChat.MsgDataToMQ
var data server_api_params.MsgData
uid := "test_uid"
data.Seq = 11
data.ClientMsgID = "23jwhjsdf"
msg.MsgData = &data
seqMsg, failedSeqList, err := DB.NewGetMessageListBySeq(uid, []uint32{11}, "cacheTest")
assert.Nil(t, err)
fmt.Println(seqMsg, failedSeqList)
}