-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifo_option.go
54 lines (47 loc) · 1.11 KB
/
fifo_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
46
47
48
49
50
51
52
53
54
package gcache
import "time"
var defaultFIFOOptions = fifoOptions{
expiry: 0,
capacity: 1000,
clear: time.Minute * 10,
}
type fifoOptions struct {
expiry time.Duration
capacity int
clear time.Duration
}
type FIFOOption interface {
apply(*fifoOptions)
}
type funcFIFOOption struct {
f func(*fifoOptions)
}
func (fdo *funcFIFOOption) apply(do *fifoOptions) {
fdo.f(do)
}
func newFuncFIFOOption(f func(*fifoOptions)) *funcFIFOOption {
return &funcFIFOOption{
f: f,
}
}
// WithFIFOExpiry if <=0, it will not expire due to time
func WithFIFOExpiry(expiry time.Duration) FIFOOption {
return newFuncFIFOOption(func(o *fifoOptions) {
o.expiry = expiry
})
}
// WithFIFOCapacity set the maximum amount of data to be cached
func WithFIFOCapacity(capacity int) FIFOOption {
return newFuncFIFOOption(func(o *fifoOptions) {
if capacity < 1 {
panic(`fifo capacity must > 0`)
}
o.capacity = capacity
})
}
// WithFIFOClear timer clear expired cache, if <=0 not start timer.
func WithFIFOClear(duration time.Duration) FIFOOption {
return newFuncFIFOOption(func(po *fifoOptions) {
po.clear = duration
})
}