SetList 设置帖子列表缓存
(ctx context.Context, page int, size int, posts []domain.Post)
| 215 | |
| 216 | // SetList 设置帖子列表缓存 |
| 217 | func (c *postCache) SetList(ctx context.Context, page int, size int, posts []domain.Post) error { |
| 218 | if page <= 0 || size <= 0 { |
| 219 | return fmt.Errorf("无效的分页参数: page=%d, size=%d", page, size) |
| 220 | } |
| 221 | |
| 222 | data, err := json.Marshal(posts) |
| 223 | if err != nil { |
| 224 | return fmt.Errorf("序列化帖子列表数据失败: %v", err) |
| 225 | } |
| 226 | |
| 227 | // 列表缓存使用随机过期时间 |
| 228 | randomExpiration := c.expiration + time.Duration(rand.Int63n(300))*time.Second |
| 229 | key := fmt.Sprintf("%d_%d", page, size) |
| 230 | return c.client.Set(ctx, c.listKey(key), data, randomExpiration).Err() |
| 231 | } |
| 232 | |
| 233 | // DelList 删除帖子列表缓存 |
| 234 | func (c *postCache) DelList(ctx context.Context, key string) error { |