GetPublishPostById 获取已发布的帖子详细信息
(ctx context.Context, postId uint)
| 134 | |
| 135 | // GetPublishPostById 获取已发布的帖子详细信息 |
| 136 | func (p *postRepository) GetPublishPostById(ctx context.Context, postId uint) (domain.Post, error) { |
| 137 | // 先从缓存中获取 |
| 138 | post, err := p.cache.GetPub(ctx, strconv.Itoa(int(postId))) |
| 139 | if err == nil { |
| 140 | return post, nil |
| 141 | } |
| 142 | |
| 143 | dp, err := p.dao.GetPubById(ctx, postId) |
| 144 | if err != nil { |
| 145 | p.l.Error("获取已发布帖子失败", zap.Error(err), zap.Uint("post_id", postId)) |
| 146 | // 缓存空对象 |
| 147 | if err := p.cache.SetEmpty(ctx, fmt.Sprint(postId)); err != nil { |
| 148 | p.l.Error("设置空对象缓存失败", zap.Error(err)) |
| 149 | } |
| 150 | return domain.Post{}, err |
| 151 | } |
| 152 | |
| 153 | result := change.ToDomainPubPost(dp) |
| 154 | |
| 155 | go func() { |
| 156 | if err := p.cache.SetPub(ctx, strconv.Itoa(int(postId)), result); err != nil { |
| 157 | p.l.Error("设置已发布帖子缓存失败", zap.Error(err), zap.Uint("post_id", postId)) |
| 158 | } |
| 159 | }() |
| 160 | |
| 161 | return result, nil |
| 162 | } |
| 163 | |
| 164 | // ListPosts 获取作者帖子的列表 |
| 165 | func (p *postRepository) ListPosts(ctx context.Context, pagination domain.Pagination) ([]domain.Post, error) { |
nothing calls this directly
no test coverage detected