GetPub 获取已发布帖子缓存
(ctx context.Context, key string)
| 373 | |
| 374 | // GetPub 获取已发布帖子缓存 |
| 375 | func (c *postCache) GetPub(ctx context.Context, key string) (domain.Post, error) { |
| 376 | if key == "" { |
| 377 | return domain.Post{}, fmt.Errorf("无效的帖子ID: %s", key) |
| 378 | } |
| 379 | |
| 380 | // 先检查是否是空对象 |
| 381 | isEmpty, err := c.IsEmpty(ctx, key) |
| 382 | if err == nil && isEmpty { |
| 383 | return domain.Post{}, fmt.Errorf("帖子不存在 %s", key) |
| 384 | } |
| 385 | |
| 386 | data, err := c.client.Get(ctx, c.pubKey(key)).Bytes() |
| 387 | if err != nil { |
| 388 | if err == redis.Nil { |
| 389 | return domain.Post{}, fmt.Errorf("缓存中未找到已发布帖子: %s", key) |
| 390 | } |
| 391 | return domain.Post{}, fmt.Errorf("从缓存获取已发布帖子失败: %v", err) |
| 392 | } |
| 393 | |
| 394 | var post domain.Post |
| 395 | if err = json.Unmarshal(data, &post); err != nil { |
| 396 | return domain.Post{}, fmt.Errorf("反序列化已发布帖子数据失败: %v", err) |
| 397 | } |
| 398 | |
| 399 | return post, nil |
| 400 | } |
| 401 | |
| 402 | // SetPub 设置已发布帖子缓存 |
| 403 | func (c *postCache) SetPub(ctx context.Context, key string, post domain.Post) error { |