MCPcopy Create free account
hub / github.com/GoSimplicity/LinkMe / SetEmptyCache

Method SetEmptyCache

pkg/cachep/bloom/cachebloom.go:98–121  ·  view source on GitHub ↗

SetEmptyCache 缓存空对象,防止缓存穿透

(ctx context.Context, key string, ttl time.Duration)

Source from the content-addressed store, hash-verified

96
97// SetEmptyCache 缓存空对象,防止缓存穿透
98func (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// 导出该方法以便外部调用

Callers

nothing calls this directly

Calls 2

SetMethod · 0.65
ExecMethod · 0.65

Tested by

no test coverage detected