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

Function QueryData

pkg/cachep/bloom/cachebloom.go:27–68  ·  view source on GitHub ↗

QueryData 查询数据,如果缓存和布隆过滤器都没有命中,则存储并返回传递的数据

(cb *CacheBloom, ctx context.Context, key string, data T, ttl time.Duration)

Source from the content-addressed store, hash-verified

25
26// QueryData 查询数据,如果缓存和布隆过滤器都没有命中,则存储并返回传递的数据
27func QueryData[T any](cb *CacheBloom, ctx context.Context, key string, data T, ttl time.Duration) (T, error) {
28 if cb == nil {
29 var zeroValue T
30 return zeroValue, errors.New("CacheBloom实例不能为空")
31 }
32
33 if key == "" {
34 var zeroValue T
35 return zeroValue, errors.New("键不能为空")
36 }
37
38 // 检查布隆过滤器
39 if !cb.bf.TestString(key) {
40 if !isNil(data) {
41 return CacheData(cb, ctx, key, data, ttl)
42 }
43 var zeroValue T
44 return zeroValue, nil
45 }
46
47 // 检查缓存
48 cachedData, err := cb.client.Get(ctx, key).Result()
49 if err != nil {
50 if errors.Is(err, redis.Nil) {
51 if !isNil(data) {
52 return CacheData(cb, ctx, key, data, ttl)
53 }
54 var zeroValue T
55 return zeroValue, nil
56 }
57 var zeroValue T
58 return zeroValue, err
59 }
60
61 var result T
62 if err := json.Unmarshal([]byte(cachedData), &result); err != nil {
63 var zeroValue T
64 return zeroValue, err
65 }
66
67 return result, nil
68}
69
70// CacheData 序列化数据并存储到 Redis 和布隆过滤器中
71func CacheData[T any](cb *CacheBloom, ctx context.Context, key string, data T, ttl time.Duration) (T, error) {

Callers

nothing calls this directly

Calls 3

isNilFunction · 0.85
CacheDataFunction · 0.85
GetMethod · 0.65

Tested by

no test coverage detected