-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloom_cache.go
More file actions
51 lines (45 loc) · 1.15 KB
/
bloom_cache.go
File metadata and controls
51 lines (45 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cache
import (
"context"
"errors"
"fmt"
)
type BloomFilterCache struct {
ReadThroughCache
}
func NewBloomFilterCache(cache Cache, bf BloomFilter,
loadFunc func(ctx context.Context, key string) (any, error)) *BloomFilterCache {
return &BloomFilterCache{
ReadThroughCache: ReadThroughCache{
Cache: cache,
LoadFunc: func(ctx context.Context, key string) (any, error) {
// 如果布隆过滤器不存在就不查询数据库
if !bf.HasKey(ctx, key) {
return nil, errKeyNotFound
}
return loadFunc(ctx, key)
},
},
}
}
type BloomFilterCacheV1 struct {
ReadThroughCache
Bf BloomFilter
}
func (r *BloomFilterCacheV1) Get(ctx context.Context, key string) (any, error) {
val, err := r.Cache.Get(ctx, key)
if errors.Is(err, errKeyNotFound) && r.Bf.HasKey(ctx, key) {
val, err = r.LoadFunc(ctx, key)
if err == nil {
er := r.Cache.Set(ctx, key, val, r.Expiration)
if er != nil {
return val, fmt.Errorf("%w, 原因:%s", ErrFailedToRefreshCache, er.Error())
}
}
}
return val, err
}
// BloomFilter 需提供布隆过滤器接口实现
type BloomFilter interface {
HasKey(ctx context.Context, key string) bool
}