Skip to content

Commit

Permalink
feature: add Cap interface{} (#145)
Browse files Browse the repository at this point in the history
* feature: add Cap interface{}
* feature: add Cap for 2q and fix bug on arc.Cap
* feature: add Cap for expireable_lru
* test: add Cap for expire lru
  • Loading branch information
zhuliquan committed Jan 10, 2024
1 parent 53739fc commit d582c45
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 2q.go
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
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
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
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
Expand Up @@ -339,3 +339,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
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
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
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
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
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
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

0 comments on commit d582c45

Please sign in to comment.