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 all commits
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 2q.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,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
}

// Resize changes the cache size.
func (c *TwoQueueCache[K, V]) Resize(size int) (evicted int) {
c.lock.Lock()
Expand Down
3 changes: 3 additions & 0 deletions 2q_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,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
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 the cache
func (c *ARCCache[K, V]) Cap() int {
return c.size
}

// 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 expirable/expirable_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,8 @@ func (c *LRU[K, V]) addToBucket(e *internal.Entry[K, V]) {
func (c *LRU[K, V]) removeFromBucket(e *internal.Entry[K, V]) {
delete(c.buckets[e.ExpireBucket].entries, e.Key)
}

// Cap returns the capacity of the cache
func (c *LRU[K, V]) Cap() int {
return c.size
}
4 changes: 4 additions & 0 deletions expirable/expirable_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ func TestLoadingExpired(t *testing.T) {
func TestLRURemoveOldest(t *testing.T) {
lc := NewLRU[string, string](2, nil, 0)

if lc.Cap() != 2 {
t.Fatalf("expect cap is 2")
}

k, v, ok := lc.RemoveOldest()
if k != "" {
t.Fatalf("should be empty")
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 @@ -86,6 +86,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 @@ -147,6 +147,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 @@ -27,6 +27,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