-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.v
125 lines (100 loc) · 2.87 KB
/
set.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
module vredis
@[params]
pub struct ScanOpts {
pattern string
count i64
cursor u64
}
pub struct ScanReply {
cursor u64
result []string
}
pub fn (mut r Redis) sadd(key string, member1 string, member2 ...string) !int {
mut args := [CmdArg(key), CmdArg(member1)]
for it in member2 {
args << it
}
return r.send('SADD', ...args)!.int()
}
pub fn (mut r Redis) scard(key string) !int {
return r.send('SCARD', key)!.int()
}
pub fn (mut r Redis) sismember(key string, value string) !bool {
return r.send('SISMEMBER', key, value)!.int() == 1
}
pub fn (mut r Redis) spop(key string) !string {
return r.send('SPOP', key)!.bytestr()
}
pub fn (mut r Redis) smove(source string, destination string, member string) !bool {
return r.send('SMOVE', source, destination, member)!.int() == 1
}
pub fn (mut r Redis) srandmember(key string, cnt ...int) ![]string {
count := if cnt.len > 0 { cnt[0] } else { 1 }
ret := r.send('SRANDMEMBER', key, count)!.data().bytestr()
return if ret.len > 0 {
ret.split(crlf)
} else {
[]string{}
}
}
pub fn (mut r Redis) srem(key string, member1 string, member2 ...string) !int {
mut args := [CmdArg(key)]
args << member1
for it in member2 {
args << it
}
return r.send('SREM', ...args)!.int()
}
fn (mut r Redis) multi_keys_handle(cmd string, key string, keys []string) ![]string {
mut args := [CmdArg(key)]
for it in keys {
args << it
}
return r.send('${cmd}', ...args)!.strings()
}
fn (mut r Redis) multi_keys_store_handle(cmd string, key string, keys []string) !int {
mut args := [CmdArg(key)]
for it in keys {
args << it
}
return r.send(cmd, ...args)!.int()
}
pub fn (mut r Redis) sunion(key string, keys ...string) ![]string {
return r.multi_keys_handle('SUNION', key, keys)!
}
pub fn (mut r Redis) sunionstore(key string, keys ...string) !int {
return r.multi_keys_store_handle('SUNIONSTORE', key, keys)
}
pub fn (mut r Redis) sdiff(key string, keys ...string) ![]string {
return r.multi_keys_handle('SDIFF', key, keys)!
}
pub fn (mut r Redis) sdiffstore(key string, keys ...string) !int {
return r.multi_keys_store_handle('SDIFFSTORE', key, keys)
}
pub fn (mut r Redis) sinter(key string, keys ...string) ![]string {
return r.multi_keys_handle('SINTER', key, keys)!
}
pub fn (mut r Redis) smembers(key string) ![]string {
return r.send('SMEMBERS', key)!.strings()
}
pub fn (mut r Redis) sinterstore(key string, keys ...string) !int {
return r.multi_keys_store_handle('SINTERSTORE', key, keys)
}
pub fn (mut r Redis) sscan(key string, opts ScanOpts) !ScanReply {
mut args := [CmdArg(key), CmdArg(opts.cursor)]
if opts.pattern.len > 0 {
args << 'MATCH'
args << opts.pattern
}
if opts.count > 0 {
args << 'COUNT'
args << opts.count
}
next_cursor, members := r.send('SSCAN', ...args)!.data().bytestr().split_once(crlf) or {
return error('error msg')
}
return ScanReply{
cursor: next_cursor.u64()
result: members.split(crlf)
}
}