-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtempdb.go
197 lines (169 loc) · 4.46 KB
/
tempdb.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
package tempdb
import (
"crypto/tls"
"errors"
"fmt"
"net"
"time"
"gopkg.in/redis.v5"
)
const (
prefix = "tempdb:"
)
var (
// ErrKeyRequired is returned when key is not passed as parameter in tempdb.Config.
ErrKeyRequired = errors.New("creating a new tempdb requires a non-empty key")
// ErrValueRequired is returned when value is not passed as parameter in tempdb.Config.
ErrValueRequired = errors.New("creating a new tempdb requires a non-empty value")
)
type (
// Tempdb stores an expiring (or non-expiring) key/value pair in Redis.
Tempdb struct {
*redis.Client
}
// Options carries the different variables to tune a newly started redis client,
// it exposes the same configuration available from https://godoc.org/gopkg.in/redis.v5#Options go client.
Options struct {
Network string
Addr string
Dialer func() (net.Conn, error)
Password string
DB int
MaxRetries int
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
PoolSize int
PoolTimeout time.Duration
IdleTimeout time.Duration
IdleCheckFrequency time.Duration
ReadOnly bool
TLSConfig *tls.Config
}
)
// New returns a new temp configured with the
// variables from the options parameter, or returning an non-nil err
// if an error occurred while creating redis client.
func New(o Options) (*Tempdb, error) {
opts := newOptions(o)
client := redis.NewClient(opts)
return &Tempdb{client}, nil
}
// Insert a key/value pair with an optional expiration time.
func (t *Tempdb) Insert(key, value string, expires time.Duration) error {
if len(key) == 0 {
return ErrKeyRequired
}
if len(value) == 0 {
return ErrValueRequired
}
k := fmt.Sprint(prefix, key)
return t.Set(k, value, expires).Err()
}
// Find the value associated with the key.
func (t *Tempdb) Find(key string) (string, error) {
if len(key) == 0 {
return "", ErrKeyRequired
}
k := fmt.Sprint(prefix, key)
value, err := t.Get(k).Result()
if err != nil {
return "", err
}
return value, t.Del(k).Err()
}
func newOptions(o Options) *redis.Options {
opts := &redis.Options{}
setAddr(opts, o.Addr)
setPassword(opts, o.Password)
setDB(opts, o.DB)
setNetwork(opts, o.Network)
setDialer(opts, o.Dialer)
setMaxRetries(opts, o.MaxRetries)
setDialTimeout(opts, o.DialTimeout)
setReadTimeout(opts, o.ReadTimeout)
setWriteTimeout(opts, o.WriteTimeout)
setPoolSize(opts, o.PoolSize)
setPoolTimeout(opts, o.PoolTimeout)
setIdleTimeout(opts, o.IdleTimeout)
setIdleCheckFrequency(opts, o.IdleCheckFrequency)
setReadOnly(opts, o.ReadOnly)
setTLSConfig(opts, o.TLSConfig)
return opts
}
func setAddr(opts *redis.Options, addr string) {
if len(addr) == 0 {
addr = "localhost:6379"
}
opts.Addr = addr
}
func setPassword(opts *redis.Options, password string) {
if len(password) != 0 {
opts.Password = password
}
}
func setDB(opts *redis.Options, db int) {
if db != 0 {
opts.DB = db
}
}
func setNetwork(opts *redis.Options, network string) {
if len(network) != 0 {
opts.Network = network
}
}
func setDialer(opts *redis.Options, dialer func() (net.Conn, error)) {
if dialer != nil {
opts.Dialer = dialer
}
}
func setMaxRetries(opts *redis.Options, maxRetries int) {
if maxRetries != 0 {
opts.MaxRetries = maxRetries
}
}
func setDialTimeout(opts *redis.Options, dialTimeout time.Duration) {
if dialTimeout != 0 {
opts.DialTimeout = dialTimeout
}
}
func setReadTimeout(opts *redis.Options, readTimeout time.Duration) {
if readTimeout != 0 {
opts.ReadTimeout = readTimeout
}
}
func setWriteTimeout(opts *redis.Options, writeTimeout time.Duration) {
if writeTimeout != 0 {
opts.WriteTimeout = writeTimeout
}
}
func setPoolSize(opts *redis.Options, poolSize int) {
if poolSize != 0 {
opts.PoolSize = poolSize
}
}
func setPoolTimeout(opts *redis.Options, poolTimeout time.Duration) {
if poolTimeout != 0 {
opts.PoolTimeout = poolTimeout
}
}
func setIdleTimeout(opts *redis.Options, idleTimeout time.Duration) {
if idleTimeout != 0 {
opts.IdleTimeout = idleTimeout
}
}
func setIdleCheckFrequency(opts *redis.Options, idleCheckFrequency time.Duration) {
if idleCheckFrequency != 0 {
opts.IdleCheckFrequency = idleCheckFrequency
}
}
func setReadOnly(opts *redis.Options, readOnly bool) {
if readOnly {
opts.ReadOnly = readOnly
}
}
func setTLSConfig(opts *redis.Options, tlsConfig *tls.Config) {
if tlsConfig != nil {
opts.TLSConfig = tlsConfig
}
}