ListPosts 获取作者帖子的列表
(ctx context.Context, pagination domain.Pagination)
| 163 | |
| 164 | // ListPosts 获取作者帖子的列表 |
| 165 | func (p *postRepository) ListPosts(ctx context.Context, pagination domain.Pagination) ([]domain.Post, error) { |
| 166 | // 先从缓存中获取 |
| 167 | posts, err := p.cache.GetList(ctx, pagination.Page, int(*pagination.Size)) |
| 168 | if err == nil && len(posts) > 0 { |
| 169 | return posts, nil |
| 170 | } |
| 171 | |
| 172 | // 缓存未命中,从数据库获取 |
| 173 | pub, err := p.dao.List(ctx, pagination) |
| 174 | if err != nil { |
| 175 | p.l.Error("获取帖子列表失败", |
| 176 | zap.Error(err), |
| 177 | zap.Int("page", pagination.Page)) |
| 178 | return nil, fmt.Errorf("获取帖子列表失败: %w", err) |
| 179 | } |
| 180 | |
| 181 | // 转换数据 |
| 182 | result := change.FromDomainSlicePost(pub) |
| 183 | |
| 184 | // 异步写入缓存 |
| 185 | go func() { |
| 186 | if err := p.cache.SetList(ctx, pagination.Page, int(*pagination.Size), result); err != nil { |
| 187 | p.l.Error("缓存帖子列表失败", |
| 188 | zap.Error(err), |
| 189 | zap.Int("page", pagination.Page)) |
| 190 | } |
| 191 | }() |
| 192 | |
| 193 | return result, nil |
| 194 | } |
| 195 | |
| 196 | // ListPublishPosts 获取已发布的帖子列表 |
| 197 | func (p *postRepository) ListPublishPosts(ctx context.Context, pagination domain.Pagination, biz ...int) ([]domain.Post, error) { |
nothing calls this directly
no test coverage detected