@@ -347,9 +347,9 @@ func RemoveCacheFor(key []byte) {
347347type Cache struct {
348348 data * ristretto.Cache [[]byte , * CachePL ]
349349
350- numCacheRead int64
351- numCacheReadFails int64
352- numCacheSave int64
350+ numCacheRead atomic. Int64
351+ numCacheReadFails atomic. Int64
352+ numCacheSave atomic. Int64
353353}
354354
355355func (c * Cache ) wait () {
@@ -365,23 +365,22 @@ func (c *Cache) get(key []byte) (*CachePL, bool) {
365365 }
366366 val , ok := c .data .Get (key )
367367 if ! ok {
368- atomic . AddInt64 ( & c .numCacheReadFails , 1 )
368+ c .numCacheReadFails . Add ( 1 )
369369 return val , ok
370370 }
371371 if val .list == nil {
372- atomic . AddInt64 ( & c .numCacheReadFails , 1 )
372+ c .numCacheReadFails . Add ( 1 )
373373 return nil , false
374374 }
375- atomic .AddInt64 (& c .numCacheRead , 1 )
376- c .numCacheRead += 1
375+ c .numCacheRead .Add (1 )
377376 return val , true
378377}
379378
380379func (c * Cache ) set (key []byte , i * CachePL ) {
381380 if c == nil {
382381 return
383382 }
384- c .numCacheSave += 1
383+ c .numCacheSave . Add ( 1 )
385384 c .data .Set (key , i , 1 )
386385}
387386
@@ -407,8 +406,7 @@ type MemoryLayer struct {
407406 cache * Cache
408407
409408 // metrics
410- statsHolder * StatsHolder
411- numDisksRead int64
409+ statsHolder * StatsHolder
412410}
413411
414412func (ml * MemoryLayer ) clear () {
@@ -448,6 +446,15 @@ func initMemoryLayer(cacheSize int64, deleteOnUpdates bool) *MemoryLayer {
448446 for range ticker .C {
449447 // Record the posting list cache hit ratio
450448 ostats .Record (context .Background (), x .PLCacheHitRatio .M (m .Ratio ()))
449+
450+ x .NumPostingListCacheSave .M (ml .cache .numCacheRead .Load ())
451+ ml .cache .numCacheSave .Store (0 )
452+
453+ x .NumPostingListCacheRead .M (ml .cache .numCacheRead .Load ())
454+ ml .cache .numCacheRead .Store (0 )
455+
456+ x .NumPostingListCacheReadFail .M (ml .cache .numCacheReadFails .Load ())
457+ ml .cache .numCacheReadFails .Store (0 )
451458 }
452459 }()
453460
@@ -657,7 +664,6 @@ func (ml *MemoryLayer) readFromCache(key []byte, readTs uint64) *List {
657664}
658665
659666func (ml * MemoryLayer ) readFromDisk (key []byte , pstore * badger.DB , readTs uint64 ) (* List , error ) {
660- atomic .AddInt64 (& ml .numDisksRead , 1 )
661667 txn := pstore .NewTransactionAt (readTs , false )
662668 defer txn .Discard ()
663669
0 commit comments