SetCacheList 将审核记录列表写入缓存
(ctx context.Context, key string, checks []domain.Check, ttl time.Duration)
| 124 | |
| 125 | // SetCacheList 将审核记录列表写入缓存 |
| 126 | func (c *checkCache) SetCacheList(ctx context.Context, key string, checks []domain.Check, ttl time.Duration) error { |
| 127 | // 将[]domain.Check对象序列化为JSON格式 |
| 128 | data, err := json.Marshal(checks) |
| 129 | if err != nil { |
| 130 | return err |
| 131 | } |
| 132 | |
| 133 | // 设置随机化的TTL,防止缓存雪崩 |
| 134 | randomTTL := ttl + time.Duration(randomInt(0, 300))*time.Second |
| 135 | |
| 136 | // 将数据写入Redis |
| 137 | return c.client.Set(ctx, key, data, randomTTL).Err() |
| 138 | } |
| 139 | |
| 140 | // GetCountCache 从缓存中获取审核记录的数量 |
| 141 | func (c *checkCache) GetCountCache(ctx context.Context, key string) (int64, error) { |