Arduino-Redis
A Redis library for Arduino.
Redis.h
1 
21 #ifndef REDIS_H
22 #define REDIS_H
23 
24 #include "Arduino.h"
25 #include "Client.h"
26 
27 #include <vector>
28 
29 typedef enum {
30  RedisSuccess = 0,
31  RedisNotConnectedFailure = 1,
32  RedisAuthFailure = 2,
33 } RedisReturnValue;
34 
43 class Redis {
44 public:
50  Redis(Client& client) : conn(client) {}
51 
52  ~Redis() {}
53  Redis(const Redis&) = delete;
54  Redis& operator=(const Redis&) = delete;
55 
61  RedisReturnValue authenticate(const char* password);
62 
72  bool set(const char* key, const char* value);
73 
79  String get(const char* key);
80 
86  bool del(const char* key);
87 
93  bool exists(const char* key);
94 
101  int append(const char* key, const char* value);
102 
109  int publish(const char* channel, const char* message);
110 
117  bool expire(const char* key, int seconds) { return _expire_(key, seconds, "EXPIRE"); }
118 
126  bool expire_at(const char* key, int timestamp) { return _expire_(key, timestamp, "EXPIREAT"); }
127 
134  bool pexpire(const char* key, int ms) { return _expire_(key, ms, "PEXPIRE"); }
135 
143  bool pexpire_at(const char* key, int timestamp) { return _expire_(key, timestamp, "PEXPIREAT"); }
144 
150  bool persist(const char* key);
151 
158  int ttl(const char* key) { return _ttl_(key, "TTL"); }
159 
166  int pttl(const char* key) { return _ttl_(key, "PTTL"); }
167 
175  bool hset(const char* key, const char* field, const char* value) { return _hset_(key, field, value, "HSET"); }
176 
184  bool hsetnx(const char* key, const char* field, const char* value) { return _hset_(key, field, value, "HSETNX"); }
185 
192  String hget(const char* key, const char* field);
199  bool hdel(const char* key, const char* field);
200 
206  int hlen(const char* key);
207 
214  int hstrlen(const char* key, const char* field);
215 
222  bool hexists(const char* key, const char* field);
223 
230  std::vector<String> lrange(const char* key, int start, int stop);
231 
232 #if ARDUINO_REDIS_TEST
233  typedef struct {
234  int total;
235  int passed;
236  } TestResults;
237 
238  TestResults runTests(bool logToSerial = false,
239  String prefix = "__com.arduino-redis.test",
240  bool retainData = false);
241 #endif // ARDUINO_REDIS_TEST
242 
243 private:
244  Client& conn;
245 
246  bool _expire_(const char*, int, const char*);
247  int _ttl_(const char*, const char*);
248  bool _hset_(const char*, const char*, const char*, const char*);
249 };
250 
251 #if ARDUINO_REDIS_TEST
252 #include <map>
253 
254 /* tests are not expected to pass if retainData = true */
255 Redis::TestResults Redis::runTests(bool logToSerial, String prefix, bool retainData)
256 {
257  /* These are NOT executed in definition order! */
258  std::map<String, std::function<bool(const char*)>> g_Tests
259  {
260  { "set", [this](const char* k) { return set(k, "!"); } },
261  { "setget", [this](const char* k) { return set(k, "!") == 1 && get(k) == "!"; } },
262  { "expire", [this](const char* k) {
263  return set(k, "E") && expire(k, 5) && ttl(k) > 4;
264  } },
265  { "pexpire", [this](const char* k) {
266  return set(k, "PE") && pexpire(k, 5000) && pttl(k) > 4500;
267  } },
268  { "dlyexp", [this](const char* k) {
269  auto sr = set(k, "DE");
270  expire(k, 10);
271  delay(4000);
272  auto t = ttl(k);
273  return sr && t > 5 && t < 7;
274  } },
275  { "hset", [this](const char* k) { return hset(k, "TF", "H!") && hexists(k, "TF"); } },
276  { "hsetget", [this](const char* k) {
277  return hset(k, "TF", "HH") && hget(k, "TF") == "HH";
278  } },
279  { "hlen", [this](const char* k) {
280  for (int i = 0; i < 10; i++) {
281  auto fv = String(i);
282  hset(k, String("field-" + fv).c_str(), fv.c_str());
283  }
284  return hlen(k) == 10;
285  } },
286  { "hstrlen", [this](const char* k) {
287  return hset(k, "hsr", k) && hstrlen(k, "hsr") == strlen(k);
288  } },
289  { "append", [this](const char* k) { return append(k, "foo") == 3; } },
290  { "exists", [this](const char* k) { return set(k, k) && exists(k); } }
291  };
292 
293  if (logToSerial) {
294  Serial.begin(115200);
295  Serial.printf("Redis::run_tests(\"%s\") starting...\n", prefix.c_str());
296  }
297 
298  auto pFunc = [&prefix](const String& n) { return String(prefix + "." + n); };
299 
300  int total = 0, pass = 0;
301  for (auto& kv : g_Tests)
302  {
303  auto tName = kv.first;
304  auto tFunc = kv.second;
305  auto pName = pFunc(tName);
306  auto tRes = tFunc(pName.c_str());
307  if (logToSerial)
308  Serial.printf("\tTest \"%s\":\t%s\n", tName.c_str(), tRes ? "pass" : "FAIL");
309  total++;
310  pass += tRes ? 1 : 0;
311  }
312 
313  if (logToSerial)
314  Serial.printf("Result: %d passed / %d total\n", pass, total);
315 
316  if (!retainData)
317  for (auto& kv : g_Tests)
318  (void)del(pFunc(kv.first).c_str());
319 
320  return { .total = total, .passed = pass };
321 }
322 #endif
323 
324 #endif
String hget(const char *key, const char *field)
Definition: Redis.cpp:76
bool del(const char *key)
Definition: Redis.cpp:36
bool pexpire_at(const char *key, int timestamp)
Definition: Redis.h:143
int hlen(const char *key)
Definition: Redis.cpp:86
bool expire_at(const char *key, int timestamp)
Definition: Redis.h:126
int ttl(const char *key)
Definition: Redis.h:158
int publish(const char *channel, const char *message)
Definition: Redis.cpp:46
int pttl(const char *key)
Definition: Redis.h:166
bool persist(const char *key)
Definition: Redis.cpp:61
std::vector< String > lrange(const char *key, int start, int stop)
Definition: Redis.cpp:101
RedisReturnValue authenticate(const char *password)
Definition: Redis.cpp:6
bool pexpire(const char *key, int ms)
Definition: Redis.h:134
int hstrlen(const char *key, const char *field)
Definition: Redis.cpp:91
Definition: Redis.h:43
bool hsetnx(const char *key, const char *field, const char *value)
Definition: Redis.h:184
Redis(Client &client)
Definition: Redis.h:50
bool exists(const char *key)
Definition: Redis.cpp:51
bool expire(const char *key, int seconds)
Definition: Redis.h:117
int append(const char *key, const char *value)
Definition: Redis.cpp:41
bool hdel(const char *key, const char *field)
Definition: Redis.cpp:81
bool hset(const char *key, const char *field, const char *value)
Definition: Redis.h:175
bool hexists(const char *key, const char *field)
Definition: Redis.cpp:96