From b9597a139be0c326ded314e9f6b9ccd6bded6175 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Tue, 31 May 2022 15:13:59 +0800 Subject: [PATCH] redis replace go redis --- pkg/common/db/newRedisModel.go | 43 ++++++++++++++++++++++++++++++++ pkg/common/db/redisModel_test.go | 13 ++++++++++ 2 files changed, 56 insertions(+) diff --git a/pkg/common/db/newRedisModel.go b/pkg/common/db/newRedisModel.go index 812d773f1..993ef2147 100644 --- a/pkg/common/db/newRedisModel.go +++ b/pkg/common/db/newRedisModel.go @@ -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 diff --git a/pkg/common/db/redisModel_test.go b/pkg/common/db/redisModel_test.go index a11f96bcd..967b75ac1 100644 --- a/pkg/common/db/redisModel_test.go +++ b/pkg/common/db/redisModel_test.go @@ -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) + +}