-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
185 lines (162 loc) · 3.98 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package redis
import (
"context"
"math/rand"
"time"
"github.com/redis/go-redis/v9"
"github.com/charlesbases/library/codec"
"github.com/charlesbases/library/codec/json"
)
var (
defaultContext = context.Background()
defaultTimeout = 3 * time.Second
defaultRetries = 0
defaultMarshaler = json.Marshaler
defaultMutexTTL = 5 * time.Second
defaultMutexInterval = 50 * time.Millisecond
// RandomExpiry 0-24h 随机过期时间
RandomExpiry = func() time.Duration {
return time.Duration(rand.Intn(24 * int(time.Hour)))
}
)
// RedisClient redis client
var RedisClient Cmdable = func(o *Options) (redis.Cmdable, func() error) {
client := redis.NewClient(&redis.Options{
Addr: o.Addrs[0],
Username: o.Username,
Password: o.Password,
MaxRetries: o.MaxRetries,
DialTimeout: o.Timeout,
ReadTimeout: o.Timeout,
WriteTimeout: o.Timeout,
// PoolSize: 4 * runtime.NumCPU(), // 连接池最大 socket 连接数。(default: 4 * runtime.NumCPU())
// MinIdleConns: 16, // 空闲连接数
// TLSConfig: &tls.Config{
// InsecureSkipVerify: true,
// },
})
return client, client.Close
}
// RedisCluster redis cluster
var RedisCluster Cmdable = func(o *Options) (redis.Cmdable, func() error) {
cluster := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: o.Addrs,
Username: o.Username,
Password: o.Password,
MaxRetries: o.MaxRetries,
DialTimeout: o.Timeout,
ReadTimeout: o.Timeout,
WriteTimeout: o.Timeout,
// PoolSize: 4 * runtime.NumCPU(), // 连接池最大 socket 连接数。(default: 4 * runtime.NumCPU())
// MinIdleConns: 16, // 空闲连接数
// TLSConfig: &tls.Config{
// InsecureSkipVerify: true,
// },
})
return cluster, cluster.Close
}
// Cmdable redis.Cmdable
type Cmdable func(o *Options) (redis.Cmdable, func() error)
// Options .
type Options struct {
Context context.Context
Addrs []string
Username string
Password string
// Timeout second
Timeout time.Duration
// MaxRetries 命令执行失败时的重试次数
MaxRetries int
// Cmdable redis redis.Cmdable
Cmdable Cmdable
}
// SetOptions .
type SetOptions struct {
Context context.Context
// TTL key 过期时间
TTL time.Duration
// Expiry key 过期时间。TTL 和 Expiry 同时设置时,以 Expiry 为准
Expiry time.Time
// Marshaler value 编码方式
Marshaler codec.Marshaler
}
// setoptions .
func setoptions(opts ...func(o *SetOptions)) *SetOptions {
o := &SetOptions{
Context: defaultContext,
Marshaler: defaultMarshaler,
}
for _, opt := range opts {
opt(o)
}
return o
}
// GetOptions .
type GetOptions struct {
Context context.Context
// Marshaler value 编码方式
Marshaler codec.Marshaler
}
// getoptions .
func getoptions(opts ...func(o *GetOptions)) *GetOptions {
o := &GetOptions{
Context: defaultContext,
Marshaler: defaultMarshaler,
}
for _, opt := range opts {
opt(o)
}
return o
}
// DelOptions .
type DelOptions struct {
Context context.Context
}
// deloptions .
func deloptions(opts ...func(o *DelOptions)) *DelOptions {
o := &DelOptions{
Context: defaultContext,
}
for _, opt := range opts {
opt(o)
}
return o
}
// MutexOptions .
type MutexOptions struct {
Context context.Context
// Interval 尝试获取锁的时间间隔
Interval time.Duration
// TTL 加锁后,超时自动删除锁
TTL time.Duration
}
// mutexoptions .
func mutexoptions(opts ...func(o *MutexOptions)) *MutexOptions {
o := &MutexOptions{
Context: defaultContext,
Interval: defaultMutexInterval,
TTL: defaultMutexTTL,
}
for _, opt := range opts {
opt(o)
}
return o
}
// ExpireOptions .
type ExpireOptions struct {
Context context.Context
// TTL key 过期时间
TTL time.Duration
// Expiry key 过期时间。TTL 和 Expiry 同时设置时,以 Expiry 为准
Expiry time.Time
}
// expireoptions .
func expireoptions(opts ...func(o *ExpireOptions)) *ExpireOptions {
o := &ExpireOptions{
Context: defaultContext,
}
for _, opt := range opts {
opt(o)
}
return o
}