Skip to content

Commit

Permalink
feature: add Cap interface{}
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuliquan committed Jul 28, 2023
1 parent f4cd393 commit d9a133c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions arc/arc.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ func (c *ARCCache[K, V]) Len() int {
return c.t1.Len() + c.t2.Len()
}

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

// Keys returns all the cached keys
func (c *ARCCache[K, V]) Keys() []K {
c.lock.RLock()
Expand Down
9 changes: 9 additions & 0 deletions arc/arc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ func TestARC(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 Expand Up @@ -340,6 +343,9 @@ func TestARC(t *testing.T) {
t.Fatalf("should be deleted")
}
}
if l.Cap() != 128 {
t.Fatalf("expect %d, but %d", 128, l.Cap())
}

l.Purge()
if l.Len() != 0 {
Expand All @@ -348,6 +354,9 @@ func TestARC(t *testing.T) {
if _, ok := l.Get(200); ok {
t.Fatalf("should contain nothing")
}
if l.Cap() != 128 {
t.Fatalf("expect %d, but %d", 128, l.Cap())
}
}

// Test that Contains doesn't update recent-ness
Expand Down
5 changes: 5 additions & 0 deletions lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,8 @@ func (c *Cache[K, V]) Len() int {
c.lock.RUnlock()
return length
}

// Cap returns the capacity of the cache
func (c *Cache[K, V]) Cap() int {
return c.lru.Cap()
}
3 changes: 3 additions & 0 deletions lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func TestLRU(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())
}

if evictCounter != 128 {
t.Fatalf("bad evict count: %v", evictCounter)
Expand Down
5 changes: 5 additions & 0 deletions simplelru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func (c *LRU[K, V]) Len() int {
return c.evictList.length()
}

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

// Resize changes the cache size.
func (c *LRU[K, V]) Resize(size int) (evicted int) {
diff := c.Len() - size
Expand Down
3 changes: 3 additions & 0 deletions simplelru/lru_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type LRUCache[K comparable, V any] interface {
// Returns the number of items in the cache.
Len() int

// Returns the capacity of the cache.
Cap() int

// Clears all cache entries.
Purge()

Expand Down
3 changes: 3 additions & 0 deletions simplelru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func TestLRU(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())
}

if evictCounter != 128 {
t.Fatalf("bad evict count: %v", evictCounter)
Expand Down

0 comments on commit d9a133c

Please sign in to comment.