Skip to content

Commit 584e98a

Browse files
committed
Make capacity a required param
1 parent d5857d5 commit 584e98a

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

cache/cache.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,6 @@ type cache struct {
5454
// CacheOption configures a cache.
5555
type CacheOption func(*cache)
5656

57-
// Capacity sets the fixed capacity bound on the cache, in bytes.
58-
// If not provided, default is 10MB.
59-
func Capacity(cap uint64) CacheOption {
60-
return func(c *cache) {
61-
c.cap = cap
62-
}
63-
}
64-
6557
// Policy is a cache eviction policy for use with the EvictionPolicy CacheOption.
6658
type Policy uint8
6759

@@ -90,9 +82,9 @@ func EvictionPolicy(policy Policy) CacheOption {
9082
// New returns a cache with the requested options configured.
9183
// The cache consumes memory bounded by a fixed capacity,
9284
// plus tracking overhead linear in the number of items.
93-
func New(options ...CacheOption) Cache {
85+
func New(capacity uint64, options ...CacheOption) Cache {
9486
c := &cache{
95-
cap: 10 * 1024 * 1024, // Default capacity of 10MB
87+
cap: capacity,
9688
keyList: list.New(),
9789
items: map[string]*cached{},
9890
}

cache/cache_test.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ import (
55
"testing"
66
)
77

8-
func TestCapacity(t *testing.T) {
9-
c := &cache{}
10-
Capacity(52)(c)
11-
if c.cap != 52 {
12-
t.Errorf("Capacity failed to set cap")
13-
}
14-
}
15-
168
func TestEvictionPolicy(t *testing.T) {
179
c := &cache{keyList: list.New()}
1810
EvictionPolicy(LeastRecentlyUsed)(c)
@@ -33,10 +25,10 @@ func TestNew(t *testing.T) {
3325
optionApplied = true
3426
}
3527

36-
c := New(option).(*cache)
28+
c := New(314159, option).(*cache)
3729

38-
if c.cap != 10*1024*1024 {
39-
t.Errorf("Expected default cache capacity of %d", 10*1024*1024)
30+
if c.cap != 314159 {
31+
t.Errorf("Expected cache capacity of %d", 314159)
4032
}
4133
if c.size != 0 {
4234
t.Errorf("Expected initial size of zero")
@@ -71,15 +63,15 @@ func TestPutGetRemoveSize(t *testing.T) {
7163
expectedItems []Item
7264
}{{
7365
label: "Items added, key doesn't exist",
74-
cache: New(),
66+
cache: New(10000),
7567
useCache: func(c Cache) {
7668
c.Put("foo", testItem(1))
7769
},
7870
expectedSize: 1,
7971
expectedItems: []Item{testItem(1), nil, nil},
8072
}, {
8173
label: "Items added, key exists",
82-
cache: New(),
74+
cache: New(10000),
8375
useCache: func(c Cache) {
8476
c.Put("foo", testItem(1))
8577
c.Put("foo", testItem(10))
@@ -88,7 +80,7 @@ func TestPutGetRemoveSize(t *testing.T) {
8880
expectedItems: []Item{testItem(10), nil, nil},
8981
}, {
9082
label: "Items added, LRA eviction",
91-
cache: New(Capacity(2), EvictionPolicy(LeastRecentlyAdded)),
83+
cache: New(2, EvictionPolicy(LeastRecentlyAdded)),
9284
useCache: func(c Cache) {
9385
c.Put("foo", testItem(1))
9486
c.Put("bar", testItem(1))
@@ -99,7 +91,7 @@ func TestPutGetRemoveSize(t *testing.T) {
9991
expectedItems: []Item{nil, testItem(1), testItem(1)},
10092
}, {
10193
label: "Items added, LRU eviction",
102-
cache: New(Capacity(2), EvictionPolicy(LeastRecentlyUsed)),
94+
cache: New(2, EvictionPolicy(LeastRecentlyUsed)),
10395
useCache: func(c Cache) {
10496
c.Put("foo", testItem(1))
10597
c.Put("bar", testItem(1))
@@ -110,7 +102,7 @@ func TestPutGetRemoveSize(t *testing.T) {
110102
expectedItems: []Item{testItem(1), nil, testItem(1)},
111103
}, {
112104
label: "Items removed, key doesn't exist",
113-
cache: New(),
105+
cache: New(10000),
114106
useCache: func(c Cache) {
115107
c.Put("foo", testItem(1))
116108
c.Remove("baz")
@@ -119,7 +111,7 @@ func TestPutGetRemoveSize(t *testing.T) {
119111
expectedItems: []Item{testItem(1), nil, nil},
120112
}, {
121113
label: "Items removed, key exists",
122-
cache: New(),
114+
cache: New(10000),
123115
useCache: func(c Cache) {
124116
c.Put("foo", testItem(1))
125117
c.Remove("foo")

0 commit comments

Comments
 (0)