-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
106 lines (90 loc) · 1.76 KB
/
options.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package kvstore
import "time"
type (
InitOption func(o *InitOptionContext)
GetOption func(o *GetOptionContext)
SetOption func(o *SetOptionContext)
IncrOption func(o *IncrOptionContext)
)
type InitOptionContext struct{}
type GetOptionContext struct {
TTL time.Duration
ExAt time.Time
Del bool
}
type SetOptionContext struct {
TTL time.Duration
ExAt time.Time
Mode string
Get bool
KeepTTL bool
}
type IncrOptionContext struct {
TTL time.Duration
ExAt time.Time
KeepTTL bool
}
func WithGetTTL(ttl time.Duration) GetOption {
return func(o *GetOptionContext) {
o.TTL = ttl
o.ExAt = time.Time{}
o.Del = false
}
}
func WithGetExpireAt(eat time.Time) GetOption {
return func(o *GetOptionContext) {
o.ExAt = eat
o.TTL = 0
o.Del = false
}
}
func WithDel() GetOption {
return func(o *GetOptionContext) {
o.Del = true
o.TTL = 0
o.ExAt = time.Time{}
}
}
func WithSetOnlyIfNotExists() SetOption {
return func(o *SetOptionContext) {
o.Mode = "NX"
}
}
func WithSetOnlyIfAlreadyExists() SetOption {
return func(o *SetOptionContext) {
o.Mode = "XX"
}
}
func WithRetrieveDisabled() SetOption {
return func(o *SetOptionContext) {
o.Get = false
}
}
func WithSetTTL(ttl time.Duration) SetOption {
return func(o *SetOptionContext) {
o.TTL = ttl
o.ExAt = time.Time{}
o.KeepTTL = false
}
}
func WithSetExpireAt(eat time.Time) SetOption {
return func(o *SetOptionContext) {
o.ExAt = eat
o.TTL = 0
o.KeepTTL = false
}
}
func WithIncrTTL(ttl time.Duration, keepTTL bool) IncrOption {
return func(o *IncrOptionContext) {
o.TTL = ttl
o.KeepTTL = keepTTL
o.ExAt = time.Time{}
}
}
func WithIncrExpireAt(eat time.Time) IncrOption {
return func(o *IncrOptionContext) {
o.ExAt = eat
o.TTL = 0
o.KeepTTL = false
}
}