GetPubList 获取已发布帖子列表缓存
(ctx context.Context, page int, size int)
| 271 | |
| 272 | // GetPubList 获取已发布帖子列表缓存 |
| 273 | func (c *postCache) GetPubList(ctx context.Context, page int, size int) ([]domain.Post, error) { |
| 274 | if page <= 0 || size <= 0 { |
| 275 | return nil, fmt.Errorf("无效的分页参数: page=%d, size=%d", page, size) |
| 276 | } |
| 277 | |
| 278 | key := fmt.Sprintf("%d_%d", page, size) |
| 279 | data, err := c.client.Get(ctx, c.pubListKey(key)).Bytes() |
| 280 | if err != nil { |
| 281 | if err == redis.Nil { |
| 282 | return nil, fmt.Errorf("缓存中未找到已发布帖子列表: %s", key) |
| 283 | } |
| 284 | return nil, fmt.Errorf("从缓存获取已发布帖子列表失败: %v", err) |
| 285 | } |
| 286 | |
| 287 | var posts []domain.Post |
| 288 | if err = json.Unmarshal(data, &posts); err != nil { |
| 289 | // 删除可能损坏的缓存数据 |
| 290 | _ = c.client.Del(ctx, c.pubListKey(key)) |
| 291 | return nil, fmt.Errorf("反序列化已发布帖子列表数据失败: %v", err) |
| 292 | } |
| 293 | |
| 294 | return posts, nil |
| 295 | } |
| 296 | |
| 297 | // SetPubList 设置已发布帖子列表缓存 |
| 298 | func (c *postCache) SetPubList(ctx context.Context, page int, size int, posts []domain.Post) error { |
nothing calls this directly
no test coverage detected