@@ -18,6 +18,11 @@ type (
18
18
cacheKey string
19
19
)
20
20
21
+ func (c cacheKey ) Prefix () cacheKeyPrefix {
22
+ prefix , _ , _ := strings .Cut (string (c ), "/" )
23
+ return cacheKeyPrefix (prefix )
24
+ }
25
+
21
26
const (
22
27
cacheKeyPrefixBatchRequest cacheKeyPrefix = "batch"
23
28
cacheKeyPrefixRequest cacheKeyPrefix = "request"
@@ -36,25 +41,26 @@ func (s *Server) getCacheKeyWithPrefix(p cacheKeyPrefix, key string) cacheKey {
36
41
return cacheKey (fmt .Sprintf ("%s/%s" , p , key ))
37
42
}
38
43
44
+ func getCacheMetricsCtx (ctx context.Context , k cacheKey ) context.Context {
45
+ ctx , _ = tag .New (ctx , tag .Upsert (metrics .TagCacheKey , string (k )), tag .Upsert (metrics .TagCacheKeyPrefix , string (k .Prefix ())))
46
+ return ctx
47
+ }
48
+
39
49
func (s * Server ) getFromCache (ctx context.Context , k cacheKey ) (any , bool ) {
40
- strKey := string (k )
41
- val , ok := s .cache .Get (strKey )
50
+ val , ok := s .cache .Get (string (k ))
42
51
if ok {
43
- ctx , _ = tag .New (ctx , tag .Upsert (metrics .TagCacheKey , strKey ))
44
- stats .Record (ctx , metrics .CounterCacheHit .M (1 ))
52
+ stats .Record (getCacheMetricsCtx (ctx , k ), metrics .CounterCacheHit .M (1 ))
45
53
}
46
54
return val , ok
47
55
}
48
56
49
57
func (s * Server ) setInCache (ctx context.Context , k cacheKey , v any , expiration ... time.Duration ) {
50
- strKey := string (k )
51
- ctx , _ = tag .New (ctx , tag .Upsert (metrics .TagCacheKey , strKey ))
52
- stats .Record (ctx , metrics .CounterCacheMiss .M (1 ))
58
+ stats .Record (getCacheMetricsCtx (ctx , k ), metrics .CounterCacheMiss .M (1 ))
53
59
exp := cache .DefaultExpiration
54
60
if len (expiration ) > 0 {
55
61
exp = expiration [0 ]
56
62
}
57
- s .cache .Set (strKey , v , exp )
63
+ s .cache .Set (string ( k ) , v , exp )
58
64
}
59
65
60
66
func (s * Server ) invalidateByPrefix (prefix cacheKey ) int {
0 commit comments