Skip to content

Commit

Permalink
add EntriesCount to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Mar 19, 2024
1 parent 7d80b4d commit 8152e22
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
15 changes: 9 additions & 6 deletions lru_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,15 @@ func (c *LRUCache[K, V]) AppendKeys(keys []K) []K {
// Stats returns cache stats.
func (c *LRUCache[K, V]) Stats() (stats Stats) {
for i := uint32(0); i <= c.mask; i++ {
c.shards[i].mu.Lock()
s := c.shards[i].stats
c.shards[i].mu.Unlock()
stats.GetCalls += s.getcalls
stats.SetCalls += s.setcalls
stats.Misses += s.misses
s := &c.shards[i]
s.mu.Lock()
sl := s.table.length
ss := s.stats
s.mu.Unlock()
stats.EntriesCount += uint64(sl)
stats.GetCalls += ss.getcalls
stats.SetCalls += ss.setcalls
stats.Misses += ss.misses
}
return
}
6 changes: 6 additions & 0 deletions lru_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ func TestLRUCacheStats(t *testing.T) {
cache.Set("d", 3)

stats := cache.Stats()
if got, want := stats.EntriesCount, uint64(4); got != want {
t.Fatalf("cache entries should be %v: %v", want, got)
}
if got, want := stats.GetCalls, uint64(0); got != want {
t.Fatalf("cache get calls should be %v: %v", want, got)
}
Expand All @@ -316,6 +319,9 @@ func TestLRUCacheStats(t *testing.T) {
cache.Set("c", 13)

stats = cache.Stats()
if got, want := stats.EntriesCount, uint64(4); got != want {
t.Fatalf("cache entries should be %v: %v", want, got)
}
if got, want := stats.GetCalls, uint64(5); got != want {
t.Fatalf("cache get calls should be %v: %v", want, got)
}
Expand Down
3 changes: 3 additions & 0 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ type Stats struct {

// Misses is the number of cache misses.
Misses uint64

// EntriesCount is the current number of entries in the cache.
EntriesCount uint64
}
15 changes: 9 additions & 6 deletions ttl_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,15 @@ func (c *TTLCache[K, V]) AppendKeys(keys []K) []K {
// Stats returns cache stats.
func (c *TTLCache[K, V]) Stats() (stats Stats) {
for i := uint32(0); i <= c.mask; i++ {
c.shards[i].mu.Lock()
s := c.shards[i].stats
c.shards[i].mu.Unlock()
stats.GetCalls += s.getcalls
stats.SetCalls += s.setcalls
stats.Misses += s.misses
s := &c.shards[i]
s.mu.Lock()
sl := s.table.length
ss := s.stats
s.mu.Unlock()
stats.EntriesCount += uint64(sl)
stats.GetCalls += ss.getcalls
stats.SetCalls += ss.setcalls
stats.Misses += ss.misses
}
return
}
6 changes: 6 additions & 0 deletions ttl_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ func TestTTLCacheStats(t *testing.T) {
cache.Set("d", 3, 2*time.Second)

stats := cache.Stats()
if got, want := stats.EntriesCount, uint64(4); got != want {
t.Fatalf("cache entries should be %v: %v", want, got)
}
if got, want := stats.GetCalls, uint64(0); got != want {
t.Fatalf("cache get calls should be %v: %v", want, got)
}
Expand All @@ -370,6 +373,9 @@ func TestTTLCacheStats(t *testing.T) {
cache.Set("c", 13, 3*time.Second)

stats = cache.Stats()
if got, want := stats.EntriesCount, uint64(4); got != want {
t.Fatalf("cache entries should be %v: %v", want, got)
}
if got, want := stats.GetCalls, uint64(5); got != want {
t.Fatalf("cache get calls should be %v: %v", want, got)
}
Expand Down

0 comments on commit 8152e22

Please sign in to comment.