Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend SimpleCache to allow for unbounded growth #38

Merged
merged 5 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -66,9 +66,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 @@ -146,6 +143,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