MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / ListPublishPosts

Method ListPublishPosts

internal/repository/post.go:197–238  ·  view source on GitHub ↗

ListPublishPosts 获取已发布的帖子列表

(ctx context.Context, pagination domain.Pagination, biz ...int)

Source from the content-addressed store, hash-verified

195
196// ListPublishPosts 获取已发布的帖子列表
197func (p *postRepository) ListPublishPosts(ctx context.Context, pagination domain.Pagination, biz ...int) ([]domain.Post, error) {
198 // 先从缓存中获取
199 posts, err := p.cache.GetPubList(ctx, pagination.Page, int(*pagination.Size))
200 if err == nil && len(posts) > 0 {
201 return posts, nil
202 }
203
204 // 缓存未命中,从数据库获取
205 pub, err := p.dao.ListPub(ctx, pagination)
206 if err != nil {
207 p.l.Error("从数据库获取已发布帖子列表失败", zap.Error(err))
208 return nil, fmt.Errorf("从数据库获取已发布帖子列表失败: %w", err)
209 }
210
211 if len(pub) == 0 && len(biz) == 0 {
212 p.l.Info("没有找到已发布的帖子")
213 // 缓存空对象
214 if err := p.cache.SetEmpty(ctx, fmt.Sprint(pagination.Page)); err != nil {
215 p.l.Error("设置空对象缓存失败", zap.Error(err))
216 }
217 return []domain.Post{}, nil
218 }
219
220 result := change.FromDomainSlicePubPostList(pub)
221
222 go func() {
223 // 如果是前三页
224 if pagination.Page <= 3 {
225 if err := p.cache.PreHeat(ctx, result); err != nil {
226 p.l.Error("预热已发布帖子列表缓存失败", zap.Error(err))
227 }
228 } else {
229 if err := p.cache.SetPubList(ctx, pagination.Page, int(*pagination.Size), result); err != nil {
230 p.l.Error("缓存已发布帖子列表失败",
231 zap.Error(err),
232 zap.Int("page", pagination.Page))
233 }
234 }
235 }()
236
237 return result, nil
238}
239
240// Delete 删除帖子
241func (p *postRepository) Delete(ctx context.Context, postId uint, uid int64) error {

Callers

nothing calls this directly

Calls 6

GetPubListMethod · 0.65
ListPubMethod · 0.65
SetEmptyMethod · 0.65
PreHeatMethod · 0.65
SetPubListMethod · 0.65

Tested by

no test coverage detected