Skip to content

Commit d9a133c

Browse files
committed
feature: add Cap interface{}
1 parent f4cd393 commit d9a133c

File tree

7 files changed

+33
-0
lines changed

7 files changed

+33
-0
lines changed

arc/arc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ func (c *ARCCache[K, V]) Len() int {
202202
return c.t1.Len() + c.t2.Len()
203203
}
204204

205+
// Cap returns the capacity of cache
206+
func (c *ARCCache[K, V]) Cap() int {
207+
return c.Cap()
208+
}
209+
205210
// Keys returns all the cached keys
206211
func (c *ARCCache[K, V]) Keys() []K {
207212
c.lock.RLock()

arc/arc_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ func TestARC(t *testing.T) {
313313
if l.Len() != 128 {
314314
t.Fatalf("bad len: %v", l.Len())
315315
}
316+
if l.Cap() != 128 {
317+
t.Fatalf("expect %d, but %d", 128, l.Cap())
318+
}
316319

317320
for i, k := range l.Keys() {
318321
if v, ok := l.Get(k); !ok || v != k || v != i+128 {
@@ -340,6 +343,9 @@ func TestARC(t *testing.T) {
340343
t.Fatalf("should be deleted")
341344
}
342345
}
346+
if l.Cap() != 128 {
347+
t.Fatalf("expect %d, but %d", 128, l.Cap())
348+
}
343349

344350
l.Purge()
345351
if l.Len() != 0 {
@@ -348,6 +354,9 @@ func TestARC(t *testing.T) {
348354
if _, ok := l.Get(200); ok {
349355
t.Fatalf("should contain nothing")
350356
}
357+
if l.Cap() != 128 {
358+
t.Fatalf("expect %d, but %d", 128, l.Cap())
359+
}
351360
}
352361

353362
// Test that Contains doesn't update recent-ness

lru.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,8 @@ func (c *Cache[K, V]) Len() int {
248248
c.lock.RUnlock()
249249
return length
250250
}
251+
252+
// Cap returns the capacity of the cache
253+
func (c *Cache[K, V]) Cap() int {
254+
return c.lru.Cap()
255+
}

lru_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func TestLRU(t *testing.T) {
8585
if l.Len() != 128 {
8686
t.Fatalf("bad len: %v", l.Len())
8787
}
88+
if l.Cap() != 128 {
89+
t.Fatalf("expect %d, but %d", 128, l.Cap())
90+
}
8891

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

simplelru/lru.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ func (c *LRU[K, V]) Len() int {
148148
return c.evictList.length()
149149
}
150150

151+
// Cap returns the capacity of the cache
152+
func (c *LRU[K, V]) Cap() int {
153+
return c.size
154+
}
155+
151156
// Resize changes the cache size.
152157
func (c *LRU[K, V]) Resize(size int) (evicted int) {
153158
diff := c.Len() - size

simplelru/lru_interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ type LRUCache[K comparable, V any] interface {
3838
// Returns the number of items in the cache.
3939
Len() int
4040

41+
// Returns the capacity of the cache.
42+
Cap() int
43+
4144
// Clears all cache entries.
4245
Purge()
4346

simplelru/lru_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ func TestLRU(t *testing.T) {
2424
if l.Len() != 128 {
2525
t.Fatalf("bad len: %v", l.Len())
2626
}
27+
if l.Cap() != 128 {
28+
t.Fatalf("expect %d, but %d", 128, l.Cap())
29+
}
2730

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

0 commit comments

Comments
 (0)