Skip to content

Commit

Permalink
feature: add cap for 2q and fix bug on arc.Cap
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuliquan committed Aug 2, 2023
1 parent d9a133c commit 60d4838
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions 2q.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ func (c *TwoQueueCache[K, V]) Len() int {
return c.recent.Len() + c.frequent.Len()
}

// Cap returns the capacity of the cache
func (c *TwoQueueCache[K, V]) Cap() int {
return c.size
}

// Keys returns a slice of the keys in the cache.
// The frequently used keys are first in the returned slice.
func (c *TwoQueueCache[K, V]) Keys() []K {
Expand Down
3 changes: 3 additions & 0 deletions 2q_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ func Test2Q(t *testing.T) {
if l.Len() != 128 {
t.Fatalf("bad len: %v", l.Len())
}
if l.Cap() != 128 {
t.Fatalf("expect %d, but %d", 128, l.Cap())
}

for i, k := range l.Keys() {
if v, ok := l.Get(k); !ok || v != k || v != i+128 {
Expand Down
2 changes: 1 addition & 1 deletion arc/arc.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (c *ARCCache[K, V]) Len() int {

// Cap returns the capacity of cache
func (c *ARCCache[K, V]) Cap() int {
return c.Cap()
return c.size
}

// Keys returns all the cached keys
Expand Down

0 comments on commit 60d4838

Please sign in to comment.