-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
lru.go
49 lines (41 loc) · 1.16 KB
/
lru.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package config
import (
"errors"
"fmt"
"github.com/qdm12/gosettings"
"github.com/qdm12/gotree"
)
type CacheLRU struct {
// MaxEntries is the number of maximum entries
// to keep in the LRU cache. It defaults to 10e4
// if letf unset.
// Note its type is int instead of uint* since it is
// used to compare against an int length so its maximum
// is math.MaxInt which depends on the platform.
MaxEntries int
}
func (c *CacheLRU) setDefaults() {
const defaultMaxEntries = 10e4
c.MaxEntries = gosettings.DefaultComparable(c.MaxEntries, defaultMaxEntries)
}
var (
ErrCacheLRUMaxEntriesNegative = errors.New("max entries is negative")
ErrCacheLRUMaxEntriesZero = errors.New("max entries is zero")
)
func (c *CacheLRU) validate() error {
switch {
case c.MaxEntries < 0:
return fmt.Errorf("%w: %d", ErrCacheLRUMaxEntriesNegative, c.MaxEntries)
case c.MaxEntries == 0:
return fmt.Errorf("%w", ErrCacheLRUMaxEntriesZero)
}
return nil
}
func (c *CacheLRU) String() string {
return c.ToLinesNode().String()
}
func (c *CacheLRU) ToLinesNode() (node *gotree.Node) {
node = gotree.New("LRU cache:")
node.Appendf("Max entries: %d", c.MaxEntries)
return node
}