diff --git a/galaxycache.go b/galaxycache.go index b8a2046e..a81460c5 100644 --- a/galaxycache.go +++ b/galaxycache.go @@ -704,7 +704,7 @@ func (c *cache) stats() CacheStats { c.mu.Lock() defer c.mu.Unlock() return CacheStats{ - Bytes: c.nbytes, + Bytes: c.nbytes.Get(), Items: c.itemsLocked(), Gets: c.nget, Hits: c.nhit, @@ -732,7 +732,7 @@ func (c *cache) add(key string, value valWithStat) { c.mu.Lock() defer c.mu.Unlock() c.lru.Add(key, value) - c.nbytes += int64(len(key)) + value.size() + c.nbytes.Add(int64(len(key)) + value.size()) } func (c *cache) removeOldest() { @@ -745,9 +745,7 @@ func (c *cache) removeOldest() { } func (c *cache) bytes() int64 { - c.mu.Lock() - defer c.mu.Unlock() - return c.nbytes + return c.nbytes.Get() } func (c *cache) items() int64 { diff --git a/galaxycache_test.go b/galaxycache_test.go index 33c0b5af..0445606e 100644 --- a/galaxycache_test.go +++ b/galaxycache_test.go @@ -388,8 +388,8 @@ func TestNoDedup(t *testing.T) { testKStats := keyStats{dQPS: windowedAvgQPS{}} testvws := g.newValWithStat([]byte(testval), &testKStats) wantBytes := int64(len(testkey)) + testvws.size() - if g.mainCache.nbytes != wantBytes { - t.Errorf("cache has %d bytes, want %d", g.mainCache.nbytes, wantBytes) + if g.mainCache.nbytes.Get() != wantBytes { + t.Errorf("cache has %d bytes, want %d", g.mainCache.nbytes.Get(), wantBytes) } } diff --git a/typed_caches.go b/typed_caches.go index 9b1edc2f..2ba6acaa 100644 --- a/typed_caches.go +++ b/typed_caches.go @@ -53,7 +53,7 @@ func (c *candidateCache) get(key string) (*keyStats, bool) { type cache struct { mu sync.Mutex lru *lru.TypedCache[string, valWithStat] - nbytes int64 // of all keys and values + nbytes AtomicInt // of all keys and values nhit, nget int64 nevict int64 // number of evictions ctype CacheType @@ -69,7 +69,7 @@ func newCache(kind CacheType) cache { func (c *cache) setLRUOnEvicted(f func(key string, kStats *keyStats)) { c.lru.OnEvicted = func(key string, value valWithStat) { val := value - c.nbytes -= int64(len(key)) + val.size() + c.nbytes.Add(-(int64(len(key)) + val.size())) c.nevict++ if f != nil { f(key, val.stats) diff --git a/untyped_caches.go b/untyped_caches.go index 430bb59a..59b64e02 100644 --- a/untyped_caches.go +++ b/untyped_caches.go @@ -57,7 +57,7 @@ func (c *candidateCache) get(key string) (*keyStats, bool) { type cache struct { mu sync.Mutex lru *lru.Cache - nbytes int64 // of all keys and values + nbytes AtomicInt // of all keys and values nhit, nget int64 nevict int64 // number of evictions ctype CacheType @@ -73,7 +73,7 @@ func newCache(kind CacheType) cache { func (c *cache) setLRUOnEvicted(f func(key string, kStats *keyStats)) { c.lru.OnEvicted = func(key lru.Key, value interface{}) { val := value.(valWithStat) - c.nbytes -= int64(len(key.(string))) + val.size() + c.nbytes.Add(-(int64(len(key.(string))) + val.size())) c.nevict++ if f != nil { f(key.(string), val.stats)