Skip to content

Commit 3c279a8

Browse files
committed
Implement simplelru.InvalidSize error
1 parent ae46393 commit 3c279a8

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

2q.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ func New2QParams(size int, recentRatio float64, ghostRatio float64) (*TwoQueueCa
7070
}
7171
recentEvict, err := simplelru.NewLRU(evictSize, nil)
7272
if err != nil {
73-
return nil, fmt.Errorf("Failed to create Evict cache, please choose a higher 'ghostRatio' or increase cache 'size'")
73+
if _, ok := err.(*simplelru.InvalidSize); ok {
74+
return nil, fmt.Errorf("Failed to create Evict cache, please choose a higher 'ghostRatio' or increase cache 'size'")
75+
}
7476
}
7577

7678
// Initialize the cache

simplelru/lru.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ package simplelru
22

33
import (
44
"container/list"
5-
"errors"
65
)
76

7+
// InvalidSize error is returned when a size <= 0 is given while creting simplelru
8+
type InvalidSize struct {
9+
errMsg string
10+
}
11+
12+
// Error implements the error interface for type InvalidSize
13+
func (e *InvalidSize) Error() string {
14+
return "Must provide a positive size"
15+
}
16+
817
// EvictCallback is used to get a callback when a cache entry is evicted
918
type EvictCallback func(key interface{}, value interface{})
1019

@@ -25,7 +34,7 @@ type entry struct {
2534
// NewLRU constructs an LRU of the given size
2635
func NewLRU(size int, onEvict EvictCallback) (*LRU, error) {
2736
if size <= 0 {
28-
return nil, errors.New("Must provide a positive size")
37+
return nil, &InvalidSize{}
2938
}
3039
c := &LRU{
3140
size: size,

0 commit comments

Comments
 (0)