Skip to content

Commit

Permalink
Extend SimpleCache to allow for unbounded growth (#38)
Browse files Browse the repository at this point in the history
* Allow SimpleCache to be of unbounded size

* Only panic if incorrect size for LRU/LFU/ARC caches

* Change unbounded flag value to c.size = 0

* Fix typo in evictedFunc test case

* Add test case for simple unbounded cache
  • Loading branch information
aaronwinter authored and bluele committed Oct 10, 2017
1 parent e73ab21 commit 4726142
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion arc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func buildLoadingARCacheWithExpiration(size int, ep time.Duration) Cache {
}

func evictedFuncForARC(key, value interface{}) {
fmt.Printf("[ARC] Key:%v Value:%v will evicted.\n", key, value)
fmt.Printf("[ARC] Key:%v Value:%v will be evicted.\n", key, value)
}

func TestARCGet(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ type CacheBuilder struct {
}

func New(size int) *CacheBuilder {
if size <= 0 {
panic("gcache: size <= 0")
}
return &CacheBuilder{
clock: NewRealClock(),
tp: TYPE_SIMPLE,
Expand Down Expand Up @@ -155,6 +152,10 @@ func (cb *CacheBuilder) Expiration(expiration time.Duration) *CacheBuilder {
}

func (cb *CacheBuilder) Build() Cache {
if cb.size <= 0 && cb.tp != TYPE_SIMPLE {
panic("gcache: Cache size <= 0")
}

return cb.build()
}

Expand Down
2 changes: 1 addition & 1 deletion lfu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func evictedFuncForLFU(key, value interface{}) {
fmt.Printf("[LFU] Key:%v Value:%v will evicted.\n", key, value)
fmt.Printf("[LFU] Key:%v Value:%v will be evicted.\n", key, value)
}

func buildLFUCache(size int) Cache {
Expand Down
2 changes: 1 addition & 1 deletion lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func evictedFuncForLRU(key, value interface{}) {
fmt.Printf("[LRU] Key:%v Value:%v will evicted.\n", key, value)
fmt.Printf("[LRU] Key:%v Value:%v will be evicted.\n", key, value)
}

func buildLRUCache(size int) Cache {
Expand Down
8 changes: 6 additions & 2 deletions simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func newSimpleCache(cb *CacheBuilder) *SimpleCache {
}

func (c *SimpleCache) init() {
c.items = make(map[interface{}]*simpleItem, c.size)
if c.size <= 0 {
c.items = make(map[interface{}]*simpleItem)
} else {
c.items = make(map[interface{}]*simpleItem, c.size)
}
}

// Set a new key-value pair
Expand Down Expand Up @@ -58,7 +62,7 @@ func (c *SimpleCache) set(key, value interface{}) (interface{}, error) {
item.value = value
} else {
// Verify size not exceeded
if len(c.items) >= c.size {
if (len(c.items) >= c.size) && c.size > 0 {
c.evict(1)
}
item = &simpleItem{
Expand Down
22 changes: 21 additions & 1 deletion simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func buildLoadingSimpleCache(size int, loader LoaderFunc) Cache {
}

func evictedFuncForSimple(key, value interface{}) {
fmt.Printf("[Simple] Key:%v Value:%v will evicted.\n", key, value)
fmt.Printf("[Simple] Key:%v Value:%v will be evicted.\n", key, value)
}

func TestSimpleGet(t *testing.T) {
Expand Down Expand Up @@ -61,6 +61,26 @@ func TestSimpleEvictItem(t *testing.T) {
}
}

func TestSimpleUnboundedNoEviction(t *testing.T) {
numbers := 1000
size_tracker := 0
gcu := buildLoadingSimpleCache(0, loader)

for i := 0; i < numbers; i++ {
current_size := gcu.Len()
if current_size != size_tracker {
t.Errorf("Excepted cache size is %v not %v", current_size, size_tracker)
}

_, err := gcu.Get(fmt.Sprintf("Key-%d", i))
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

size_tracker++
}
}

func TestSimpleGetIFPresent(t *testing.T) {
testGetIFPresent(t, TYPE_SIMPLE)
}
Expand Down

0 comments on commit 4726142

Please sign in to comment.