redis add get message

pull/232/head
Gordon 3 years ago
parent d6742812c8
commit 79ff36d49a

@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/golang/protobuf/jsonpb"
"strconv"
)
@ -269,18 +270,14 @@ func (d *DataBases) GetMessageListBySeq(userID string, seqList []uint32, operati
//MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
key := messageCache + userID + "_" + strconv.Itoa(int(v))
result, err := redis.Values(d.Exec("HGETALL", key))
result, err := redis.String(d.Exec("GET", key))
if err != nil {
errResult = err
failedSeqList = append(failedSeqList, v)
log2.NewWarn(operationID, "redis get message error:", err.Error(), v)
} else {
var a []byte
for _, v := range result {
a = append(a, v.(byte))
}
msg := pbCommon.MsgData{}
err = json.Unmarshal(a, &msg)
err = jsonpb.UnmarshalString(result, &msg)
if err != nil {
errResult = err
failedSeqList = append(failedSeqList, v)
@ -299,15 +296,15 @@ func (d *DataBases) SetMessageToCache(msgList []*pbChat.MsgDataToMQ, uid string,
var failedList []pbChat.MsgDataToMQ
for _, msg := range msgList {
key := messageCache + uid + "_" + strconv.Itoa(int(msg.MsgData.Seq))
m, err := utils.Pb2Map(msg.MsgData)
s, err := utils.Pb2String(msg.MsgData)
if err != nil {
log2.NewWarn(operationID, utils.GetSelfFuncName(), "Pb2Map failed", msg.MsgData.String(), uid, err.Error())
log2.NewWarn(operationID, utils.GetSelfFuncName(), "Pb2String failed", msg.MsgData.String(), uid, err.Error())
continue
}
log2.NewDebug(operationID, "convert map is ", m)
_, err = d.Exec("hmset", key, redis.Args{}.Add("TIMEOUT", config.Config.MsgCacheTimeout).AddFlat(m)...)
log2.NewDebug(operationID, "convert string is ", s)
_, err = d.Exec("SET", key, s, "ex", config.Config.MsgCacheTimeout)
if err != nil {
log2.NewWarn(operationID, utils.GetSelfFuncName(), "redis failed", "args:", key, *msg, uid, m)
log2.NewWarn(operationID, utils.GetSelfFuncName(), "redis failed", "args:", key, *msg, uid, s)
failedList = append(failedList, *msg)
}
}

@ -1,7 +1,6 @@
package utils
import (
"bytes"
"encoding/json"
"github.com/gogo/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
@ -136,16 +135,23 @@ func RemoveRepeatedStringInList(slc []string) []string {
return result
}
func Pb2Map(pb proto.Message) (map[string]interface{}, error) {
_buffer := bytes.Buffer{}
jsonbMarshaller := &jsonpb.Marshaler{
func Pb2String(pb proto.Message) (string, error) {
marshaler := jsonpb.Marshaler{
OrigName: true,
EnumsAsInts: true,
EmitDefaults: true,
EnumsAsInts: false,
EmitDefaults: false,
}
_ = jsonbMarshaller.Marshal(&_buffer, pb)
jsonCnt := _buffer.Bytes()
var out map[string]interface{}
err := json.Unmarshal(jsonCnt, &out)
return out, err
return marshaler.MarshalToString(pb)
}
func Map2Pb(m map[string]interface{}) (pb proto.Message, err error) {
b, err := json.Marshal(m)
if err != nil {
return nil, err
}
err = proto.Unmarshal(b, pb)
if err != nil {
return nil, err
}
return pb, nil
}

Loading…
Cancel
Save