Skip to content

Commit d582c45

Browse files
authored
feature: add Cap interface{} (#145)
* 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
1 parent 53739fc commit d582c45

11 files changed

+50
-0
lines changed

2q.go

+5
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ func (c *TwoQueueCache[K, V]) Len() int {
175175
return c.recent.Len() + c.frequent.Len()
176176
}
177177

178+
// Cap returns the capacity of the cache
179+
func (c *TwoQueueCache[K, V]) Cap() int {
180+
return c.size
181+
}
182+
178183
// Resize changes the cache size.
179184
func (c *TwoQueueCache[K, V]) Resize(size int) (evicted int) {
180185
c.lock.Lock()

2q_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ func Test2Q(t *testing.T) {
299299
if l.Len() != 128 {
300300
t.Fatalf("bad len: %v", l.Len())
301301
}
302+
if l.Cap() != 128 {
303+
t.Fatalf("expect %d, but %d", 128, l.Cap())
304+
}
302305

303306
for i, k := range l.Keys() {
304307
if v, ok := l.Get(k); !ok || v != k || v != i+128 {

arc/arc.go

+5
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 the cache
206+
func (c *ARCCache[K, V]) Cap() int {
207+
return c.size
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

+9
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

expirable/expirable_lru.go

+5
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,8 @@ func (c *LRU[K, V]) addToBucket(e *internal.Entry[K, V]) {
339339
func (c *LRU[K, V]) removeFromBucket(e *internal.Entry[K, V]) {
340340
delete(c.buckets[e.ExpireBucket].entries, e.Key)
341341
}
342+
343+
// Cap returns the capacity of the cache
344+
func (c *LRU[K, V]) Cap() int {
345+
return c.size
346+
}

expirable/expirable_lru_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,10 @@ func TestLoadingExpired(t *testing.T) {
425425
func TestLRURemoveOldest(t *testing.T) {
426426
lc := NewLRU[string, string](2, nil, 0)
427427

428+
if lc.Cap() != 2 {
429+
t.Fatalf("expect cap is 2")
430+
}
431+
428432
k, v, ok := lc.RemoveOldest()
429433
if k != "" {
430434
t.Fatalf("should be empty")

lru.go

+5
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

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func TestLRU(t *testing.T) {
8686
if l.Len() != 128 {
8787
t.Fatalf("bad len: %v", l.Len())
8888
}
89+
if l.Cap() != 128 {
90+
t.Fatalf("expect %d, but %d", 128, l.Cap())
91+
}
8992

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

simplelru/lru.go

+5
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ func (c *LRU[K, V]) Len() int {
147147
return c.evictList.Length()
148148
}
149149

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

simplelru/lru_interface.go

+3
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

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func TestLRU(t *testing.T) {
2727
if l.Len() != 128 {
2828
t.Fatalf("bad len: %v", l.Len())
2929
}
30+
if l.Cap() != 128 {
31+
t.Fatalf("expect %d, but %d", 128, l.Cap())
32+
}
3033

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

0 commit comments

Comments
 (0)