-
Notifications
You must be signed in to change notification settings - Fork 0
/
low_lru_option.go
45 lines (39 loc) · 922 Bytes
/
low_lru_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 defaultLowLRUOptions = lowLRUOptions{
expiry: 0,
capacity: 1000,
}
type lowLRUOptions struct {
expiry time.Duration
capacity int
}
type LowLRUOption interface {
apply(*lowLRUOptions)
}
type funcLowLRUOption struct {
f func(*lowLRUOptions)
}
func (fdo *funcLowLRUOption) apply(do *lowLRUOptions) {
fdo.f(do)
}
func newFuncLowLRUOption(f func(*lowLRUOptions)) *funcLowLRUOption {
return &funcLowLRUOption{
f: f,
}
}
// WithLowLRUExpiry if <=0, it will not expire due to time
func WithLowLRUExpiry(expiry time.Duration) LowLRUOption {
return newFuncLowLRUOption(func(o *lowLRUOptions) {
o.expiry = expiry
})
}
// WithLRUCapacity set the maximum amount of data to be cached
func WithLowLRUCapacity(capacity int) LowLRUOption {
return newFuncLowLRUOption(func(o *lowLRUOptions) {
if capacity < 1 {
panic(`lru capacity must > 0`)
}
o.capacity = capacity
})
}