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

Fix confusing error msg. during 2Q creation #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion 2q.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCa
}
recentEvict, err := simplelru.NewLRU(evictSize, nil)
if err != nil {
return nil, err
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
Expand Down
13 changes: 11 additions & 2 deletions simplelru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand All @@ -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,
Expand Down