1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| func (h *CommentServer) QueryComment(ctx context.Context, req *comment.QueryCommentREQ, rsp *comment.QueryCommentRSP) error { var comments []model.Comment var ack []*comment.CommentContent info, err := redisCli.GetRedisCli().Get(ctx, "comment_id_"+strconv.Itoa(int(req.CommentId))).Bytes() if err == nil { fmt.Println("use redis!") err = json.Unmarshal(info, &ack) rsp.Code = uint32(common.StatusRPCOK) rsp.Comment = ack return nil } err = db.QueryComment(req.CommentId, req.Type, &comments) if err != nil { rsp.Code = uint32(common.ErrRPCCode) rsp.Err = err.Error() return errors.Wrap(err, "db query comment err") } for _, v := range comments { rspUser, err := client.GetIDUser(v.UserID) if err != nil { rsp.Code = uint32(common.ErrRPCCode) rsp.Err = err.Error() return errors.Wrap(err, "db get id user err") } ack = append(ack, &comment.CommentContent{ Id: v.ID, CommentId: v.CommentedID, Type: uint32(v.Type), Content: v.Content, Time: v.Time, UserId: v.UserID, UserAvatar: oss.DownloadUrl(rspUser.Avatar), UserName: rspUser.Name, Love: int64(v.Love), }) } fmt.Println("user mysql!") rsp.Code = uint32(common.StatusRPCOK) rsp.Comment = ack data, _ := json.Marshal(ack) err = redisCli.GetRedisCli().Set(ctx, "comment_id_"+strconv.Itoa(int(req.CommentId)), string(data), time.Second*5).Err() if err != nil { rsp.Err = "redis add comment err" } return nil }
|