-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdable.v
162 lines (139 loc) · 3.62 KB
/
cmdable.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
module redict
import arrays
import time
import x.json2 as json
fn use_precise(duration time.Duration) bool {
return duration < time.second || duration % time.second != 0
}
fn format_ms(duration time.Duration) i64 {
if duration > 0 && duration < time.millisecond {
return 1
}
return i64(duration / time.millisecond)
}
fn format_sec(duration time.Duration) i64 {
if duration > 0 && duration < time.second {
return 1
}
return i64(duration / time.second)
}
/*
*
*
* Cmdable
*
*
*/
struct Cmdable {
mut:
cmdable_function fn (cmd &Cmder) ! = unsafe { nil }
}
pub fn (c Cmdable) ping() !&StatusCmd {
cmd := new_status_cmd('ping')
c.cmdable_function(cmd)!
return cmd
}
pub fn (c Cmdable) del(keys ...string) !&IntCmd {
mut args := []json.Any{}
args << 'del'
for key in keys {
args << key
}
cmd := new_int_cmd(...args)
c.cmdable_function(cmd)!
return cmd
}
pub fn (c Cmdable) expire(key string, expiration time.Duration) !&BoolCmd {
return c.private_expire(key, expiration, '')
}
pub fn (c Cmdable) expire_nx(key string, expiration time.Duration) !&BoolCmd {
return c.private_expire(key, expiration, 'NX')
}
pub fn (c Cmdable) expire_xx(key string, expiration time.Duration) !&BoolCmd {
return c.private_expire(key, expiration, 'XX')
}
pub fn (c Cmdable) expire_gt(key string, expiration time.Duration) !&BoolCmd {
return c.private_expire(key, expiration, 'GT')
}
pub fn (c Cmdable) expire_lt(key string, expiration time.Duration) !&BoolCmd {
return c.private_expire(key, expiration, 'LT')
}
fn (c Cmdable) private_expire(key string, expiration time.Duration, mode string) !&BoolCmd {
mut args := []json.Any{}
args << 'expire'
args << key
args << format_sec(expiration)
if mode != '' {
args << mode
}
cmd := new_bool_cmd(...args)
c.cmdable_function(cmd)!
return cmd
}
pub fn (c Cmdable) get(key string) !&StringCmd {
cmd := new_string_cmd('get', key)
c.cmdable_function(cmd)!
return cmd
}
// set issues a `SET key value [expiration]` command.
// Zero expiration means the key has no expiration time.
pub fn (c Cmdable) set(key string, value string, expiration time.Duration) !&StatusCmd {
mut args := []json.Any{}
args << 'set'
args << key
args << value
if expiration > 0 {
if use_precise(expiration) {
args = arrays.concat(args, 'px', format_ms(expiration))
} else {
args = arrays.concat(args, 'ex', format_sec(expiration))
}
}
cmd := new_status_cmd(...args)
c.cmdable_function(cmd)!
return cmd
}
/*
*
*
* CmdableStateful
*
*
*/
struct CmdableStateful {
mut:
cmdable_stateful_function fn (cmd &Cmder) ! = unsafe { nil }
}
pub fn (c CmdableStateful) auth(password string) !&StatusCmd {
cmd := new_status_cmd('auth', password)
c.cmdable_stateful_function(cmd)!
return cmd
}
pub fn (c CmdableStateful) auth_acl(username string, password string) !&StatusCmd {
cmd := new_status_cmd('auth', username, password)
c.cmdable_stateful_function(cmd)!
return cmd
}
pub fn (c CmdableStateful) hello(protover int, username string, password string, client_name string) !&MapStringInterfaceCmd {
mut args := []json.Any{}
args = arrays.concat(args, 'hello', protover)
if password != '' {
if username != '' {
args = arrays.concat(args, 'auth', username, password)
} else {
args = arrays.concat(args, 'auth', 'default', password)
}
}
if client_name != '' {
args = arrays.concat(args, 'setname', client_name)
}
cmd := new_map_string_interface_cmd(...args)
c.cmdable_stateful_function(cmd)!
return cmd
}
// It is called `select_db` because `select` is a reserved keyword.
pub fn (c CmdableStateful) select_db(index int) !&StatusCmd {
cmd := new_status_cmd('select', index)
c.cmdable_stateful_function(cmd)!
return cmd
}