-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.v
130 lines (106 loc) · 2.83 KB
/
string.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
module vredis
@[inline]
pub fn (mut r Redis) incrby(key string, increment int) !int {
return r.send('INCRBY', key, increment)!.int()
}
@[inline]
pub fn (mut r Redis) incr(key string) !int {
return r.incrby(key, 1)!
}
@[inline]
pub fn (mut r Redis) decr(key string) !int {
return r.incrby(key, -1)!
}
@[inline]
pub fn (mut r Redis) decrby(key string, decrement int) !int {
return r.incrby(key, -decrement)!
}
@[inline]
pub fn (mut r Redis) incrbyfloat(key string, increment f64) !f64 {
return r.send('INCRBYFLOAT', key, increment)!.f64()
}
@[inline]
pub fn (mut r Redis) append(key string, value string) !int {
return r.send('APPEND', key, value)!.int()
}
@[inline]
pub fn (mut r Redis) strlen(key string) !int {
return r.send('STRLEN', key)!.int()
}
@[inline]
pub fn (mut r Redis) get(key string) !string {
return r.send('GET', key)!.bytestr()
}
@[inline]
pub fn (mut r Redis) getset(key string, value string) !string {
return r.send('GETSET', key, value)!.bytestr()
}
@[inline]
pub fn (mut r Redis) getrange(key string, start int, end int) !string {
return r.send('GETRANGE', key, start, end)!.bytestr()
}
pub fn (mut r Redis) setnx(key string, value string) !bool {
return r.set_opts(key, value, SetOpts{
nx: true
})!
}
pub fn (mut r Redis) setrange(key string, offset int, value string) !int {
return r.send('SETRANGE', key, offset, value)!.int()
}
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) set(key string, value string) !bool {
return r.send('SET', key, value)!.ok()
}
pub fn (mut r Redis) setbit(key string, offset int, value int) !bool {
return r.send('SETBIT', key, offset, value)!.@is(1)
}
pub fn (mut r Redis) getbit(key string, offset int) !int {
return r.send('GETBIT', key, offset)!.int()
}
pub fn (mut r Redis) mget(key string, keys ...string) !map[string]string {
mut args := [CmdArg(key)]
for it in keys {
args << it
}
mut data := map[string]string{}
vals := r.send('MGET', ...args)!.strings()
for i := 0; i < args.len; i++ {
data[args[i] as string] = vals[i]
}
return data
}
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' }
mut args := [CmdArg(value)]
args << ex
args << nx
args << keep_ttl
return r.send('SET', ...args)!.ok()
}
@[inline]
pub fn (mut r Redis) keys(pattern string) ![]string {
return r.send('KEYS', pattern)!.strings()
}
pub fn (mut r Redis) psetex(key string, millis int, value string) !bool {
return r.set_opts(key, value, SetOpts{
px: millis
})
}