-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathredis.v
352 lines (303 loc) · 8.02 KB
/
redis.v
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
module redis
import net
import strconv
pub struct ConnOpts {
port int = 6379
host string = '127.0.0.1'
}
pub struct Redis {
mut:
socket net.TcpConn
}
pub struct SetOpts {
ex int = -4
px int = -4
nx bool
xx bool
keep_ttl bool
}
pub enum KeyType {
t_none
t_string
t_list
t_set
t_zset
t_hash
t_stream
t_unknown
}
fn (mut r Redis) redis_transaction(message string) !string {
r.socket.write_string(message)!
return r.socket.read_line()
}
// https://github.com/v-community/learn_v_in_y_minutes/blob/master/learnv.v
// https://github.com/vlang/v/blob/master/vlib/net/socket_test.v
// https://redis.io/topics/protocol
pub fn connect(opts ConnOpts) !Redis {
mut socket := net.dial_tcp('${opts.host}:${opts.port}')!
return Redis{
socket: socket
}
}
pub fn (mut r Redis) disconnect() {
r.socket.close() or {}
}
pub fn (mut r Redis) auth(username string, password string) bool {
res := r.redis_transaction('AUTH ${username} "${password}"\r\n') or { return false }
return res.starts_with('+OK')
}
pub fn (mut r Redis) set(key string, value string) bool {
res := r.redis_transaction('SET "${key}" "${value}"\r\n') or { return false }
return res.starts_with('+OK')
}
pub fn (mut r Redis) set_opts(key string, value string, opts SetOpts) bool {
ex := if opts.ex == -4 && opts.px == -4 {
''
} else if opts.ex != -4 {
' EX ${opts.ex}'
} else {
' PX ${opts.px}'
}
nx := if opts.nx == false && opts.xx == false {
''
} else if opts.nx == true {
' NX'
} else {
' XX'
}
keep_ttl := if opts.keep_ttl == false { '' } else { ' KEEPTTL' }
res := r.redis_transaction('SET "${key}" "${value}"${ex}${nx}${keep_ttl}\r\n') or {
return false
}
return res.starts_with('+OK')
}
pub fn (mut r Redis) setex(key string, seconds int, value string) bool {
return r.set_opts(key, value, SetOpts{
ex: seconds
})
}
pub fn (mut r Redis) psetex(key string, millis int, value string) bool {
return r.set_opts(key, value, SetOpts{
px: millis
})
}
pub fn (mut r Redis) setnx(key string, value string) int {
res := r.set_opts(key, value, SetOpts{
nx: true
})
return if res == true { 1 } else { 0 }
}
pub fn (mut r Redis) incrby(key string, increment int) !int {
res := r.redis_transaction('INCRBY "${key}" ${increment}\r\n')!
rerr := parse_err(res)
if rerr != '' {
return error(rerr)
}
return parse_int(res)
}
pub fn (mut r Redis) incr(key string) !int {
res := r.incrby(key, 1)!
return res
}
pub fn (mut r Redis) decr(key string) !int {
res := r.incrby(key, -1)!
return res
}
pub fn (mut r Redis) decrby(key string, decrement int) !int {
res := r.incrby(key, -decrement)!
return res
}
pub fn (mut r Redis) incrbyfloat(key string, increment f64) !f64 {
mut res := r.redis_transaction('INCRBYFLOAT "${key}" ${increment}\r\n')!
rerr := parse_err(res)
if rerr != '' {
return error(rerr)
}
res = r.socket.read_line()
return parse_float(res)
}
pub fn (mut r Redis) append(key string, value string) !int {
res := r.redis_transaction('APPEND "${key}" "${value}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) setrange(key string, offset int, value string) !int {
res := r.redis_transaction('SETRANGE "${key}" ${offset} "${value}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) lpush(key string, element string) !int {
res := r.redis_transaction('LPUSH "${key}" "${element}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) rpush(key string, element string) !int {
res := r.redis_transaction('RPUSH "${key}" "${element}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) expire(key string, seconds int) !int {
res := r.redis_transaction('EXPIRE "${key}" ${seconds}\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) pexpire(key string, millis int) !int {
res := r.redis_transaction('PEXPIRE "${key}" ${millis}\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) expireat(key string, timestamp int) !int {
res := r.redis_transaction('EXPIREAT "${key}" ${timestamp}\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) pexpireat(key string, millistimestamp i64) !int {
res := r.redis_transaction('PEXPIREAT "${key}" ${millistimestamp}\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) persist(key string) !int {
res := r.redis_transaction('PERSIST "${key}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) get(key string) !string {
res := r.redis_transaction('GET "${key}"\r\n')!
len := parse_int(res)
if len == -1 {
return error('key not found')
}
return r.socket.read_line()[0..len]
}
pub fn (mut r Redis) getset(key string, value string) !string {
res := r.redis_transaction('GETSET "${key}" ${value}\r\n')!
len := parse_int(res)
if len == -1 {
return ''
}
return r.socket.read_line()[0..len]
}
pub fn (mut r Redis) getrange(key string, start int, end int) !string {
res := r.redis_transaction('GETRANGE "${key}" ${start} ${end}\r\n')!
len := parse_int(res)
if len == 0 {
r.socket.read_line()
return ''
}
return r.socket.read_line()[0..len]
}
pub fn (mut r Redis) randomkey() !string {
res := r.redis_transaction('RANDOMKEY\r\n')!
len := parse_int(res)
if len == -1 {
return error('database is empty')
}
return r.socket.read_line()[0..len]
}
pub fn (mut r Redis) strlen(key string) !int {
res := r.redis_transaction('STRLEN "${key}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) lpop(key string) !string {
res := r.redis_transaction('LPOP "${key}"\r\n')!
len := parse_int(res)
if len == -1 {
return error('key not found')
}
return r.socket.read_line()[0..len]
}
pub fn (mut r Redis) rpop(key string) !string {
res := r.redis_transaction('RPOP "${key}"\r\n')!
len := parse_int(res)
if len == -1 {
return error('key not found')
}
return r.socket.read_line()[0..len]
}
pub fn (mut r Redis) llen(key string) !int {
res := r.redis_transaction('LLEN "${key}"\r\n')!
rerr := parse_err(res)
if rerr != '' {
return error(rerr)
}
return parse_int(res)
}
pub fn (mut r Redis) ttl(key string) !int {
res := r.redis_transaction('TTL "${key}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) pttl(key string) !int {
res := r.redis_transaction('PTTL "${key}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) exists(key string) !int {
res := r.redis_transaction('EXISTS "${key}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) type_of(key string) !KeyType {
res := r.redis_transaction('TYPE "${key}"\r\n')!
if res.len > 6 {
return match res#[1..res.len - 2] {
'none' {
KeyType.t_none
}
'string' {
KeyType.t_string
}
'list' {
KeyType.t_list
}
'set' {
KeyType.t_set
}
'zset' {
KeyType.t_zset
}
'hash' {
KeyType.t_hash
}
'stream' {
KeyType.t_stream
}
else {
KeyType.t_unknown
}
}
} else {
return KeyType.t_unknown
}
}
pub fn (mut r Redis) del(key string) !int {
res := r.redis_transaction('DEL "${key}"\r\n')!
return parse_int(res)
}
pub fn (mut r Redis) rename(key string, newkey string) bool {
res := r.redis_transaction('RENAME "${key}" "${newkey}"\r\n') or { return false }
return res.starts_with('+OK')
}
pub fn (mut r Redis) renamenx(key string, newkey string) !int {
res := r.redis_transaction('RENAMENX "${key}" "${newkey}"\r\n')!
rerr := parse_err(res)
if rerr != '' {
return error(rerr)
}
return parse_int(res)
}
pub fn (mut r Redis) flushall() bool {
res := r.redis_transaction('FLUSHALL\r\n') or { return false }
return res.starts_with('+OK')
}
fn parse_int(res string) int {
return if res.len > 1 { res[1..].int() } else { 0 }
}
fn parse_float(res string) f64 {
return strconv.atof64(res) or { 0 }
}
fn parse_err(res string) string {
if res.len >= 5 && res.starts_with('-ERR') {
return res[5..res.len - 2]
} else if res.len >= 11 && res[0..10] == '-WRONGTYPE' {
return res[11..res.len - 2]
}
return ''
}
pub fn (mut r Redis) ping() bool {
res := r.redis_transaction('PING\r\n') or { return false }
return res.starts_with('+PONG')
}
// select_db executes a select statement. Note: `select` is a reserved keyword in V.
pub fn (mut r Redis) select_db(db_number int) bool {
res := r.redis_transaction('SELECT ${db_number}\r\n') or { return false }
return res.starts_with('+OK')
}