forked from electric-sheep-co/arduino-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redis.cpp
263 lines (212 loc) · 6.06 KB
/
Redis.cpp
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
#include "Redis.h"
#include "RedisInternal.h"
RedisReturnValue Redis::authenticate(const char *password)
{
if (conn.connected())
{
int passwordLength = strlen(password);
if (passwordLength > 0)
{
auto cmdRet = RedisCommand("AUTH", ArgList{password}).issue(conn);
return cmdRet->type() == RedisObject::Type::SimpleString && (String)*cmdRet == "OK"
? RedisSuccess
: RedisAuthFailure;
}
return RedisSuccess;
}
return RedisNotConnectedFailure;
}
#define TRCMD(t, c, ...) return RedisCommand(c, ArgList{__VA_ARGS__}).issue_typed<t>(conn)
#define TRCMD_EXPECTOK(c, ...) return (bool)(((String)*RedisCommand(c, ArgList{__VA_ARGS__}).issue(conn)).indexOf("OK") != -1)
bool Redis::set(const char *key, const char *value)
{
TRCMD_EXPECTOK("SET", key, value);
}
String Redis::get(const char *key)
{
TRCMD(String, "GET", key);
}
bool Redis::del(const char *key)
{
TRCMD(bool, "DEL", key);
}
int Redis::append(const char *key, const char *value)
{
TRCMD(int, "APPEND", key, value);
}
int Redis::publish(const char *channel, const char *message)
{
TRCMD(int, "PUBLISH", channel, message);
}
bool Redis::exists(const char *key)
{
TRCMD(bool, "EXISTS", key);
}
bool Redis::_expire_(const char *key, int arg, const char *cmd_var)
{
TRCMD(bool, cmd_var, key, String(arg));
}
bool Redis::persist(const char *key)
{
TRCMD(bool, "PERSIST", key);
}
int Redis::_ttl_(const char *key, const char *cmd_var)
{
TRCMD(int, cmd_var, key);
}
bool Redis::_hset_(const char *key, const char *field, const char *value, const char *cmd_var)
{
TRCMD(int, cmd_var, key, field, value);
}
String Redis::hget(const char *key, const char *field)
{
TRCMD(String, "HGET", key, field);
}
bool Redis::hdel(const char *key, const char *field)
{
TRCMD(bool, "HDEL", key, field);
}
int Redis::hlen(const char *key)
{
TRCMD(int, "HLEN", key);
}
int Redis::hstrlen(const char *key, const char *field)
{
TRCMD(int, "HSTRLEN", key, field);
}
bool Redis::hexists(const char *key, const char *field)
{
TRCMD(bool, "HEXISTS", key, field);
}
std::vector<String> Redis::lrange(const char *key, int start, int stop)
{
auto rv = RedisCommand("LRANGE", ArgList{key, String(start), String(stop)}).issue(conn);
return rv->type() == RedisObject::Type::Array
? (std::vector<String>)*((RedisArray *)rv.get())
: std::vector<String>();
}
String Redis::lindex(const char *key, int index)
{
TRCMD(String, "LINDEX", key, String(index));
}
int Redis::llen(const char *key)
{
TRCMD(int, "LLEN", key);
}
String Redis::lpop(const char *key)
{
TRCMD(String, "LPOP", key);
}
int Redis::lpos(const char *key, const char *element)
{
TRCMD(int, "LPOS", key, element);
}
int Redis::lpush(const char *key, const char *value, bool exclusive)
{
TRCMD(int, (exclusive ? "LPUSHX" : "LPUSH"), key, value);
}
int Redis::lrem(const char *key, int count, const char *element)
{
TRCMD(int, "LREM", key, String(count), element);
}
bool Redis::lset(const char *key, int index, const char *element)
{
TRCMD_EXPECTOK("LSET", key, String(index), element);
}
bool Redis::ltrim(const char *key, int start, int stop)
{
TRCMD_EXPECTOK("LTRIM", key, String(start), String(stop));
}
String Redis::info(const char *section)
{
TRCMD(String, "INFO", (section ? section : ""));
}
String Redis::rpop(const char *key)
{
TRCMD(String, "RPOP", key);
}
int Redis::rpush(const char *key, const char *value, bool exclusive)
{
TRCMD(int, (exclusive ? "RPUSHX" : "RPUSH"), key, value);
}
bool Redis::_subscribe(SubscribeSpec spec)
{
if (!subscriberMode)
{
subSpec.push_back(spec);
return true;
}
const char *cmdName = spec.pattern ? "PSUBSCRIBE" : "SUBSCRIBE";
auto rv = RedisCommand(cmdName, ArgList{spec.spec}).issue(conn);
return rv->type() == RedisObject::Type::Array;
}
bool Redis::unsubscribe(const char *channelOrPattern)
{
auto rv = RedisCommand("UNSUBSCRIBE", ArgList{channelOrPattern}).issue(conn);
if (rv->type() == RedisObject::Type::Array)
{
auto vec = (std::vector<String>)*((RedisArray *)rv.get());
return vec.size() == 3 && vec[1] == String(channelOrPattern);
}
return false;
}
RedisSubscribeResult Redis::startSubscribing(RedisMsgCallback messageCallback, RedisMsgErrorCallback errCallback)
{
if (!messageCallback)
{
return RedisSubscribeBadCallback;
}
bool success = true;
subscriberMode = true;
if (subSpec.size())
{
for (auto spec : subSpec)
{
success = _subscribe(spec) && success;
}
}
if (!success)
{
return RedisSubscribeSetupFailure;
}
auto emitErr = [=](RedisMessageError errCode) -> void {
if (errCallback)
{
errCallback(this, errCode);
}
};
subLoopRun = true;
while (subLoopRun)
{
auto msg = RedisObject::parseType(conn);
if (msg->type() == RedisObject::Type::InternalError)
{
auto errPtr = (RedisInternalError *)msg.get();
if (errPtr->code() == RedisInternalError::Disconnected)
{
return RedisSubscribeServerDisconnected;
}
return RedisSubscribeOtherError;
}
if (msg->type() != RedisObject::Type::Array)
{
emitErr(RedisMessageBadResponseType);
continue;
}
auto msgVec = (std::vector<String>)*((RedisArray *)msg.get());
if (msgVec.size() < 3)
{
emitErr(RedisMessageTruncatedResponse);
continue;
}
if (msgVec[0] != "message" && msgVec[0] != "pmessage")
{
emitErr(RedisMessageUnknownType);
continue;
}
// pmessage payloads have an extra paramter at index 1 that specifies the matched pattern; we ignore it here
auto pMsgAdd = msgVec[0] == "pmessage" ? 1 : 0;
messageCallback(this, msgVec[1 + pMsgAdd], msgVec[2 + pMsgAdd]);
}
return RedisSubscribeSuccess;
}