diff --git a/arc_test.go b/arc_test.go index 753bceb..1ccc6f2 100644 --- a/arc_test.go +++ b/arc_test.go @@ -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) { diff --git a/cache.go b/cache.go index 1e923d3..b56d9d1 100644 --- a/cache.go +++ b/cache.go @@ -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, @@ -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() } diff --git a/lfu_test.go b/lfu_test.go index f9c65f8..d8c9b99 100644 --- a/lfu_test.go +++ b/lfu_test.go @@ -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 { diff --git a/lru_test.go b/lru_test.go index 7dffbf4..9292d8c 100644 --- a/lru_test.go +++ b/lru_test.go @@ -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 { diff --git a/simple.go b/simple.go index 532d2e0..995204c 100644 --- a/simple.go +++ b/simple.go @@ -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 @@ -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{ diff --git a/simple_test.go b/simple_test.go index f68ced2..22793b0 100644 --- a/simple_test.go +++ b/simple_test.go @@ -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) { @@ -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) }