Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ type Cache[K comparable, V Value] interface {

// Len returns number of elements in the cache.
Len() int

// Size returns the total size of all elements in the cache. It uses
// the same units produced by V.Size().
Size() uint64
}
9 changes: 9 additions & 0 deletions cache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,12 @@ func (c *Cache[K, V]) RangeFIFO(visitor func(K, V) bool) {
}
}
}

// Size returns the total size of all elements in the cache. It uses
// the same units produced by V.Size().
func (c *Cache[K, V]) Size() uint64 {
c.mtx.RLock()
defer c.mtx.RUnlock()

return c.size
}
4 changes: 2 additions & 2 deletions cache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestElementSizeCapacityEvictsEverything(t *testing.T) {
c.Put(1, &sizeable{value: 1, size: 1})
c.Put(2, &sizeable{value: 2, size: 2})
c.Put(3, &sizeable{value: 3, size: 3})
require.Equal(t, c.size, uint64(6))
require.Equal(t, c.Size(), uint64(6))

// Insert element with size=capacity of cache.
c.Put(4, &sizeable{value: 4, size: 6})
Expand Down Expand Up @@ -326,7 +326,7 @@ func TestLoadAndDelete(t *testing.T) {

// The length should be 0.
require.Zero(t, c.Len())
require.Zero(t, c.size)
require.Zero(t, c.Size())
}

// TestRangeIteration checks that the `Range` method works as expected.
Expand Down