Skip to content

Commit

Permalink
Add Stats.MaxBytesSize for determining cache capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Dec 2, 2021
1 parent f766eeb commit 656ee2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions fastcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type Stats struct {
// BytesSize is the current size of the cache in bytes.
BytesSize uint64

// MaxBytesSize is the maximum allowed size of the cache in bytes (aka capacity).
MaxBytesSize uint64

// BigStats contains stats for GetBig/SetBig methods.
BigStats
}
Expand Down Expand Up @@ -296,6 +299,7 @@ func (b *bucket) UpdateStats(s *Stats) {
for _, chunk := range b.chunks {
s.BytesSize += uint64(cap(chunk))
}
s.MaxBytesSize += uint64(len(b.chunks))*chunkSize
b.mu.RUnlock()
}

Expand Down
5 changes: 4 additions & 1 deletion fastcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ func TestCacheWrap(t *testing.T) {
t.Fatalf("unexpected number of items; got %d; cannot be smaller than %d", s.EntriesCount, calls/5)
}
if s.BytesSize < 1024 {
t.Fatalf("unexpected number of bytesSize; got %d; cannot be smaller than %d", s.BytesSize, 1024)
t.Fatalf("unexpected BytesSize; got %d; cannot be smaller than %d", s.BytesSize, 1024)
}
if s.MaxBytesSize < 32*1024*1024 {
t.Fatalf("unexpected MaxBytesSize; got %d; cannot be smaller than %d", s.MaxBytesSize, 32*1024*1024)
}
}

Expand Down

0 comments on commit 656ee2f

Please sign in to comment.