-
Notifications
You must be signed in to change notification settings - Fork 0
/
low_lfu_option.go
45 lines (39 loc) · 925 Bytes
/
low_lfu_option.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
package gcache
import "time"
var defaultLowLFUOptions = lowLFUOptions{
expiry: 0,
capacity: 1000,
}
type lowLFUOptions struct {
expiry time.Duration
capacity int
}
type LowLFUOption interface {
apply(*lowLFUOptions)
}
type funcLowLFUOption struct {
f func(*lowLFUOptions)
}
func (fdo *funcLowLFUOption) apply(do *lowLFUOptions) {
fdo.f(do)
}
func newFuncLowLFUOption(f func(*lowLFUOptions)) *funcLowLFUOption {
return &funcLowLFUOption{
f: f,
}
}
// WithLowLFUExpiry if <=0, it will not expire due to time
func WithLowLFUExpiry(expiry time.Duration) LowLFUOption {
return newFuncLowLFUOption(func(o *lowLFUOptions) {
o.expiry = expiry
})
}
// WithLowLFUCapacity set the maximum amount of data to be cached
func WithLowLFUCapacity(capacity int) LowLFUOption {
return newFuncLowLFUOption(func(o *lowLFUOptions) {
if capacity < 1 {
panic(`lfu capacity must > 0`)
}
o.capacity = capacity
})
}