From ae46393b1ee3eb13fdad75eaced1d91eee20ad4d Mon Sep 17 00:00:00 2001 From: Ravi Shekhar Jethani Date: Sun, 1 Sep 2019 21:56:18 +0200 Subject: [PATCH 1/2] Fix confusing error msg. during 2Q creation For cases where the combination of 2Q size and ghostRatio result in evict cache of size 0, the error message displayed to the user is rather confusing. This commit proposes to fix the code and give a suitable error message to the user. Addresses: Issue #51 --- 2q.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2q.go b/2q.go index e474cd0..fdc3cae 100644 --- a/2q.go +++ b/2q.go @@ -70,7 +70,7 @@ func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCa } recentEvict, err := simplelru.NewLRU(evictSize, nil) if err != nil { - return nil, err + return nil, fmt.Errorf("Failed to create Evict cache, please choose a higher 'ghostRatio' or increase cache 'size'") } // Initialize the cache From a8fae61c87679dbd18dd54522adb7960685fe6f4 Mon Sep 17 00:00:00 2001 From: Ravi Shekhar Jethani Date: Fri, 6 Sep 2019 00:39:16 +0200 Subject: [PATCH 2/2] Implement simplelru.InvalidSize error --- 2q.go | 4 +++- simplelru/lru.go | 13 +++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/2q.go b/2q.go index fdc3cae..44169f4 100644 --- a/2q.go +++ b/2q.go @@ -70,7 +70,9 @@ func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCa } recentEvict, err := simplelru.NewLRU(evictSize, nil) if err != nil { - return nil, fmt.Errorf("Failed to create Evict cache, please choose a higher 'ghostRatio' or increase cache 'size'") + if _, ok := err.(*simplelru.InvalidSize); ok { + return nil, fmt.Errorf("Failed to create Evict cache, please choose a higher 'ghostRatio' or increase cache 'size'") + } } // Initialize the cache diff --git a/simplelru/lru.go b/simplelru/lru.go index a86c853..7a99080 100644 --- a/simplelru/lru.go +++ b/simplelru/lru.go @@ -2,9 +2,18 @@ package simplelru import ( "container/list" - "errors" ) +// InvalidSize error is returned when a size <= 0 is given while creating simplelru +type InvalidSize struct { + errMsg string +} + +// Error implements the error interface for type InvalidSize +func (e *InvalidSize) Error() string { + return "Must provide a positive size" +} + // EvictCallback is used to get a callback when a cache entry is evicted type EvictCallback func(key interface{}, value interface{}) @@ -25,7 +34,7 @@ type entry struct { // NewLRU constructs an LRU of the given size func NewLRU(size int, onEvict EvictCallback) (*LRU, error) { if size <= 0 { - return nil, errors.New("Must provide a positive size") + return nil, &InvalidSize{} } c := &LRU{ size: size,