SetCache 将单个审核记录写入缓存
(ctx context.Context, key string, check domain.Check, ttl time.Duration)
| 57 | |
| 58 | // SetCache 将单个审核记录写入缓存 |
| 59 | func (c *checkCache) SetCache(ctx context.Context, key string, check domain.Check, ttl time.Duration) error { |
| 60 | // 将domain.Check对象序列化为JSON格式 |
| 61 | data, err := json.Marshal(check) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | // 设置随机化的TTL,防止缓存雪崩 |
| 67 | randomTTL := ttl + time.Duration(randomInt(0, 300))*time.Second |
| 68 | |
| 69 | // 将数据写入Redis |
| 70 | return c.client.Set(ctx, key, data, randomTTL).Err() |
| 71 | } |
| 72 | |
| 73 | // ClearCache 清除缓存中的指定键 |
| 74 | func (c *checkCache) ClearCache(ctx context.Context, key string) error { |