/
option.go
55 lines (45 loc) · 1015 Bytes
/
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
55
package redis
import "time"
// Option struct
type Option struct {
ExpireDuration *time.Duration
Encoder *string // JSON, GOB
}
// EncoderJSON json encoder
const EncoderJSON = "json"
// EncoderGOB gob encoder
const EncoderGOB = "gob"
// WithExpireTime with expire time
func WithExpireTime(duration time.Duration) Option {
return Option{
ExpireDuration: &duration,
}
}
// with encoder
func withEncoder(str string) Option {
return Option{Encoder: &str}
}
// WithDefaultEncoder with default encoder
func WithDefaultEncoder() Option {
return withEncoder(EncoderGOB)
}
// WithJSONEncoder with json encoder
func WithJSONEncoder() Option {
return withEncoder(EncoderJSON)
}
// WithGOBEncoder with gob encoder
func WithGOBEncoder() Option {
return withEncoder(EncoderGOB)
}
func getOption(opts ...Option) Option {
o := Option{}
for _, v := range opts {
if v.ExpireDuration != nil {
o.ExpireDuration = v.ExpireDuration
}
if v.Encoder != nil {
o.Encoder = v.Encoder
}
}
return o
}