-
Notifications
You must be signed in to change notification settings - Fork 3
/
redis.go
200 lines (173 loc) · 5.35 KB
/
redis.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package fridge
import (
"crypto/tls"
"github.com/shomali11/xredis"
"time"
)
// RedisOption an option for a redis option
type RedisOption func(*RedisSettings)
// WithHost sets redis host
func WithHost(host string) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.Host = host
}
}
// WithPort sets redis port
func WithPort(port int) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.Port = port
}
}
// WithPassword sets redis password
func WithPassword(password string) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.Password = password
}
}
// WithDatabase sets redis database
func WithDatabase(database int) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.Database = database
}
}
// WithNetwork sets redis network
func WithNetwork(network string) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.Network = network
}
}
// WithConnectTimeout sets redis connect timeout
func WithConnectTimeout(connectTimeout time.Duration) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.ConnectTimeout = connectTimeout
}
}
// WithWriteTimeout sets redis write timeout
func WithWriteTimeout(writeTimeout time.Duration) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.WriteTimeout = writeTimeout
}
}
// WithReadTimeout sets redis read timeout
func WithReadTimeout(readTimeout time.Duration) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.ReadTimeout = readTimeout
}
}
// WithConnectionIdleTimeout sets redis connection idle timeout
func WithConnectionIdleTimeout(connectionIdleTimeout time.Duration) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.ConnectionIdleTimeout = connectionIdleTimeout
}
}
// WithConnectionMaxIdle sets redis connection max idle
func WithConnectionMaxIdle(connectionMaxIdle int) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.ConnectionMaxIdle = connectionMaxIdle
}
}
// WithConnectionMaxActive sets redis connection max active
func WithConnectionMaxActive(connectionMaxActive int) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.ConnectionMaxActive = connectionMaxActive
}
}
// WithConnectionWait sets redis connection wait
func WithConnectionWait(connectionWait bool) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.ConnectionWait = connectionWait
}
}
// WithTlsConfig sets redis tls config
func WithTlsConfig(tlsConfig *tls.Config) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.TlsConfig = tlsConfig
}
}
// WithTlsSkipVerify sets redis tls skip verification
func WithTlsSkipVerify(tlsSkipVerify bool) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.TlsSkipVerify = tlsSkipVerify
}
}
// WithTestOnBorrowPeriod sets redis test on borrow period
func WithTestOnBorrowPeriod(testOnBorrowPeriod time.Duration) RedisOption {
return func(redisSettings *RedisSettings) {
redisSettings.TestOnBorrowPeriod = testOnBorrowPeriod
}
}
// RedisSettings contains redis settings
type RedisSettings struct {
Host string
Port int
Password string
Database int
Network string
ConnectTimeout time.Duration
WriteTimeout time.Duration
ReadTimeout time.Duration
ConnectionIdleTimeout time.Duration
ConnectionMaxIdle int
ConnectionMaxActive int
ConnectionWait bool
TlsConfig *tls.Config
TlsSkipVerify bool
TestOnBorrowPeriod time.Duration
}
// NewRedisCache creates a new redis client
func NewRedisCache(options ...RedisOption) *RedisCache {
settings := &RedisSettings{}
for _, option := range options {
option(settings)
}
redisOptions := &xredis.Options{
Host: settings.Host,
Port: settings.Port,
Password: settings.Password,
Database: settings.Database,
Network: settings.Network,
ConnectTimeout: settings.ConnectTimeout,
WriteTimeout: settings.WriteTimeout,
ReadTimeout: settings.ReadTimeout,
ConnectionIdleTimeout: settings.ConnectionIdleTimeout,
ConnectionMaxIdle: settings.ConnectionMaxIdle,
ConnectionMaxActive: settings.ConnectionMaxActive,
ConnectionWait: settings.ConnectionWait,
TlsConfig: settings.TlsConfig,
TlsSkipVerify: settings.TlsSkipVerify,
TestOnBorrowPeriod: settings.TestOnBorrowPeriod,
}
return &RedisCache{client: xredis.SetupClient(redisOptions)}
}
// RedisCache contains redis client
type RedisCache struct {
client *xredis.Client
}
// Get a value by key
func (c *RedisCache) Get(key string) (string, bool, error) {
return c.client.Get(key)
}
// Set a key value pair
func (c *RedisCache) Set(key string, value string, timeout time.Duration) error {
seconds := int64(timeout.Seconds())
if seconds == 0 {
_, err := c.client.Set(key, value)
return err
}
_, err := c.client.SetEx(key, value, seconds)
return err
}
// Remove a key
func (c *RedisCache) Remove(key string) error {
_, err := c.client.Del(key)
return err
}
// Ping to test connectivity
func (c *RedisCache) Ping() error {
_, err := c.client.Ping()
return err
}
// Close to close resources
func (c *RedisCache) Close() error {
return c.client.Close()
}