@@ -18,6 +18,11 @@ type (
1818 cacheKey string
1919)
2020
21+ func (c cacheKey ) Prefix () cacheKeyPrefix {
22+ prefix , _ , _ := strings .Cut (string (c ), "/" )
23+ return cacheKeyPrefix (prefix )
24+ }
25+
2126const (
2227 cacheKeyPrefixBatchRequest cacheKeyPrefix = "batch"
2328 cacheKeyPrefixRequest cacheKeyPrefix = "request"
@@ -36,25 +41,26 @@ func (s *Server) getCacheKeyWithPrefix(p cacheKeyPrefix, key string) cacheKey {
3641 return cacheKey (fmt .Sprintf ("%s/%s" , p , key ))
3742}
3843
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+
3949func (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 ))
4251 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 ))
4553 }
4654 return val , ok
4755}
4856
4957func (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 ))
5359 exp := cache .DefaultExpiration
5460 if len (expiration ) > 0 {
5561 exp = expiration [0 ]
5662 }
57- s .cache .Set (strKey , v , exp )
63+ s .cache .Set (string ( k ) , v , exp )
5864}
5965
6066func (s * Server ) invalidateByPrefix (prefix cacheKey ) int {
0 commit comments