| 67 | } |
| 68 | |
| 69 | func (r *rankingCache) Get(ctx context.Context) ([]domain.Post, error) { |
| 70 | // 获取缓存数据 |
| 71 | val, err := r.client.Get(ctx, r.key).Bytes() |
| 72 | if err != nil { |
| 73 | if errors.Is(err, redis.Nil) { |
| 74 | r.logger.Info("缓存未命中", zap.String("key", r.key)) |
| 75 | return nil, nil |
| 76 | } |
| 77 | r.logger.Error("获取缓存失败", |
| 78 | zap.String("key", r.key), |
| 79 | zap.Error(err)) |
| 80 | return nil, ErrGetCache |
| 81 | } |
| 82 | |
| 83 | // 反序列化帖子数据 |
| 84 | var posts []domain.Post |
| 85 | if err := json.Unmarshal(val, &posts); err != nil { |
| 86 | r.logger.Error("反序列化帖子失败", zap.Error(err)) |
| 87 | return nil, ErrUnmarshalPost |
| 88 | } |
| 89 | |
| 90 | r.logger.Info("缓存获取成功", |
| 91 | zap.String("key", r.key), |
| 92 | zap.Int("post_count", len(posts))) |
| 93 | |
| 94 | return posts, nil |
| 95 | } |