SetEmptyCache 缓存空对象,防止缓存穿透
(ctx context.Context, key string, ttl time.Duration)
| 96 | |
| 97 | // SetEmptyCache 缓存空对象,防止缓存穿透 |
| 98 | func (cb *CacheBloom) SetEmptyCache(ctx context.Context, key string, ttl time.Duration) error { |
| 99 | if key == "" { |
| 100 | return errors.New("键不能为空") |
| 101 | } |
| 102 | |
| 103 | if ttl <= 0 { |
| 104 | return errors.New("过期时间必须为正数") |
| 105 | } |
| 106 | |
| 107 | emptyValue, err := json.Marshal(struct{}{}) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | |
| 112 | pipe := cb.client.Pipeline() |
| 113 | pipe.Set(ctx, key, emptyValue, ttl) |
| 114 | _, err = pipe.Exec(ctx) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | |
| 119 | cb.bf.AddString(key) |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | // RebuildBloomFilter 重建布隆过滤器 |
| 124 | // 导出该方法以便外部调用 |