Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add Cap interface{} #145

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in an infinite recursive call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in an infinite recursive call.

Thank for your reviewing, I have fix it

}

// 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