GetTopCommentsReply 加载顶级评论
(ctx context.Context, postId int64)
| 70 | |
| 71 | // GetTopCommentsReply 加载顶级评论 |
| 72 | func (c *commentRepository) GetTopCommentsReply(ctx context.Context, postId int64) (domain.Comment, error) { |
| 73 | // 优先从缓存获取 |
| 74 | if comments, err := c.cache.Get(ctx, postId); err == nil { |
| 75 | return comments, nil |
| 76 | } else if err != redis.Nil { |
| 77 | return domain.Comment{}, fmt.Errorf("获取缓存失败: %w", err) |
| 78 | } |
| 79 | |
| 80 | // 缓存未命中,从数据库加载 |
| 81 | daoComments, err := c.dao.FindTopCommentsByPostId(ctx, postId) |
| 82 | if err != nil { |
| 83 | return domain.Comment{}, fmt.Errorf("从数据库加载评论失败: %w", err) |
| 84 | } |
| 85 | |
| 86 | // 转换并写入缓存 |
| 87 | domainComment := c.toDomainComment(daoComments) |
| 88 | if err := c.cache.Set(ctx, domainComment); err != nil { |
| 89 | return domain.Comment{}, fmt.Errorf("写入缓存失败: %w", err) |
| 90 | } |
| 91 | |
| 92 | return domainComment, nil |
| 93 | } |
| 94 | |
| 95 | // ListComments 列出评论 |
| 96 | func (c *commentRepository) ListComments(ctx context.Context, postId int64, minId, limit int64) ([]domain.Comment, error) { |
nothing calls this directly
no test coverage detected