Arduino-Redis
A Redis library for Arduino.
Redis.h
Go to the documentation of this file.
1 
21 #ifndef REDIS_H
22 #define REDIS_H
23 
24 #include "Arduino.h"
25 #include "Client.h"
26 
27 #include <vector>
28 
30 typedef enum
31 {
38 
40 typedef enum
41 {
52 
54 typedef enum
55 {
63 
72 class Redis
73 {
74 public:
76  typedef void (*RedisMsgCallback)(Redis *, String channel, String message);
79 
85  Redis(Client &client) : conn(client) {}
86 
87  ~Redis() {}
88  Redis(const Redis &) = delete;
89  Redis &operator=(const Redis &) = delete;
90  Redis(const Redis &&) = delete;
91  Redis &operator=(const Redis &&) = delete;
92 
98  RedisReturnValue authenticate(const char *password);
99 
109  bool set(const char *key, const char *value);
110 
116  String get(const char *key);
117 
123  bool del(const char *key);
124 
130  bool exists(const char *key);
131 
138  int append(const char *key, const char *value);
139 
146  int publish(const char *channel, const char *message);
147 
154  bool expire(const char *key, int seconds) { return _expire_(key, seconds, "EXPIRE"); }
155 
163  bool expire_at(const char *key, int timestamp) { return _expire_(key, timestamp, "EXPIREAT"); }
164 
171  bool pexpire(const char *key, int ms) { return _expire_(key, ms, "PEXPIRE"); }
172 
180  bool pexpire_at(const char *key, int timestamp) { return _expire_(key, timestamp, "PEXPIREAT"); }
181 
187  bool persist(const char *key);
188 
195  int pttl(const char *key) { return _ttl_(key, "PTTL"); }
196 
203  int ttl(const char *key) { return _ttl_(key, "TTL"); }
204 
212  bool hset(const char *key, const char *field, const char *value) { return _hset_(key, field, value, "HSET"); }
213 
221  bool hsetnx(const char *key, const char *field, const char *value) { return _hset_(key, field, value, "HSETNX"); }
222 
229  String hget(const char *key, const char *field);
230 
237  bool hdel(const char *key, const char *field);
238 
244  int hlen(const char *key);
245 
252  int hstrlen(const char *key, const char *field);
253 
260  bool hexists(const char *key, const char *field);
261 
268  String lindex(const char *key, int index);
269 
276  int llen(const char *key);
277 
282  String lpop(const char *key);
283 
289  int lpos(const char *key, const char *element);
290 
297  int lpush(const char *key, const char *value, bool exclusive = false);
298 
306  std::vector<String> lrange(const char *key, int start, int stop);
307 
314  int lrem(const char *key, int count, const char *element);
315 
322  bool lset(const char *key, int index, const char *element);
323 
330  bool ltrim(const char *key, int start, int stop);
331 
336  String rpop(const char *key);
337 
344  int rpush(const char *key, const char *value, bool exclusive = false);
345 
349  bool subscribe(const char *channel) { return _subscribe(SubscribeSpec{false, String(channel)}); }
350 
354  bool psubscribe(const char *pattern) { return _subscribe(SubscribeSpec{true, String(pattern)}); }
355 
359  bool unsubscribe(const char *channelOrPattern);
360 
371  RedisSubscribeResult startSubscribing(RedisMsgCallback messageCallback, RedisMsgErrorCallback errorCallback = nullptr);
372 
376  void stopSubscribing() { subLoopRun = false; }
377 
378  String info(const char *section);
379 
380  // The following are (obstensibly) for library testing purposes only
381  void setTestContext(const void *context) { _test_context = context; }
382  const void *getTestContext() { return _test_context; }
383 
384 private:
385  typedef struct
386  {
387  bool pattern;
388  String spec;
389  } SubscribeSpec;
390 
391  bool _subscribe(SubscribeSpec spec);
392 
393  Client &conn;
394  std::vector<SubscribeSpec> subSpec;
395  bool subscriberMode = false;
396  bool subLoopRun = false;
397 
398  bool _expire_(const char *, int, const char *);
399  int _ttl_(const char *, const char *);
400  bool _hset_(const char *, const char *, const char *, const char *);
401 
402  const void *_test_context;
403 };
404 
405 #endif
Redis::lset
bool lset(const char *key, int index, const char *element)
Definition: Redis.cpp:140
RedisMessageError
RedisMessageError
Definition: Redis.h:54
Redis::ttl
int ttl(const char *key)
Definition: Redis.h:203
Redis::hget
String hget(const char *key, const char *field)
Definition: Redis.cpp:77
RedisMessageBadResponseType
@ RedisMessageBadResponseType
The underlying Redis type detected in the message is not of the type expected.
Definition: Redis.h:57
Redis::~Redis
~Redis()
Definition: Redis.h:87
Redis::pexpire_at
bool pexpire_at(const char *key, int timestamp)
Definition: Redis.h:180
Redis
Definition: Redis.h:72
Redis::stopSubscribing
void stopSubscribing()
Definition: Redis.h:376
Redis::RedisMsgErrorCallback
void(* RedisMsgErrorCallback)(Redis *, RedisMessageError)
Definition: Redis.h:78
Redis::subscribe
bool subscribe(const char *channel)
Definition: Redis.h:349
Redis::del
bool del(const char *key)
Definition: Redis.cpp:37
RedisAuthFailure
@ RedisAuthFailure
The authentication credentials used are not valid.
Definition: Redis.h:36
Redis::lpush
int lpush(const char *key, const char *value, bool exclusive=false)
Definition: Redis.cpp:130
Redis::get
String get(const char *key)
Definition: Redis.cpp:32
RedisMessageUnknownType
@ RedisMessageUnknownType
An unknown error occurred.
Definition: Redis.h:61
RedisSuccess
@ RedisSuccess
Definition: Redis.h:32
Redis::startSubscribing
RedisSubscribeResult startSubscribing(RedisMsgCallback messageCallback, RedisMsgErrorCallback errorCallback=nullptr)
Definition: Redis.cpp:191
Redis::RedisMsgCallback
void(* RedisMsgCallback)(Redis *, String channel, String message)
Definition: Redis.h:76
Redis::unsubscribe
bool unsubscribe(const char *channelOrPattern)
Definition: Redis.cpp:178
Redis::expire_at
bool expire_at(const char *key, int timestamp)
Definition: Redis.h:163
RedisSubscribeSetupFailure
@ RedisSubscribeSetupFailure
Setting up for subscription mode failed.
Definition: Redis.h:45
RedisSubscribeSuccess
@ RedisSubscribeSuccess
Definition: Redis.h:50
Redis::pexpire
bool pexpire(const char *key, int ms)
Definition: Redis.h:171
RedisSubscribeResult
RedisSubscribeResult
Definition: Redis.h:40
Redis::operator=
Redis & operator=(const Redis &)=delete
Redis::publish
int publish(const char *channel, const char *message)
Definition: Redis.cpp:47
Redis::pttl
int pttl(const char *key)
Definition: Redis.h:195
RedisMessageTruncatedResponse
@ RedisMessageTruncatedResponse
The message response was truncated early.
Definition: Redis.h:59
Redis::persist
bool persist(const char *key)
Definition: Redis.cpp:62
Redis::lrange
std::vector< String > lrange(const char *key, int start, int stop)
Definition: Redis.cpp:102
Redis::setTestContext
void setTestContext(const void *context)
Definition: Redis.h:381
Redis::rpush
int rpush(const char *key, const char *value, bool exclusive=false)
Definition: Redis.cpp:160
Redis::authenticate
RedisReturnValue authenticate(const char *password)
Definition: Redis.cpp:4
Redis::lpop
String lpop(const char *key)
Definition: Redis.cpp:120
Redis::lpos
int lpos(const char *key, const char *element)
Definition: Redis.cpp:125
RedisSubscribeOtherError
@ RedisSubscribeOtherError
An unknown error occurred.
Definition: Redis.h:49
RedisSubscribeBadCallback
@ RedisSubscribeBadCallback
One of the callback parameters given is invalid.
Definition: Redis.h:43
Redis::hsetnx
bool hsetnx(const char *key, const char *field, const char *value)
Definition: Redis.h:221
Redis::exists
bool exists(const char *key)
Definition: Redis.cpp:52
RedisNotConnectedFailure
@ RedisNotConnectedFailure
Authenticate attempted before the connection has been established.
Definition: Redis.h:34
Redis::lindex
String lindex(const char *key, int index)
Definition: Redis.cpp:110
Redis::hstrlen
int hstrlen(const char *key, const char *field)
Definition: Redis.cpp:92
Redis::hdel
bool hdel(const char *key, const char *field)
Definition: Redis.cpp:82
RedisSubscribeServerDisconnected
@ RedisSubscribeServerDisconnected
The remote end disconnected, retry may be available.
Definition: Redis.h:47
Redis::llen
int llen(const char *key)
Definition: Redis.cpp:115
Redis::hexists
bool hexists(const char *key, const char *field)
Definition: Redis.cpp:97
Redis::Redis
Redis(Client &client)
Definition: Redis.h:85
RedisReturnValue
RedisReturnValue
Definition: Redis.h:30
Redis::append
int append(const char *key, const char *value)
Definition: Redis.cpp:42
Redis::hset
bool hset(const char *key, const char *field, const char *value)
Definition: Redis.h:212
Redis::psubscribe
bool psubscribe(const char *pattern)
Definition: Redis.h:354
Redis::set
bool set(const char *key, const char *value)
Definition: Redis.cpp:27
Redis::rpop
String rpop(const char *key)
Definition: Redis.cpp:155
Redis::getTestContext
const void * getTestContext()
Definition: Redis.h:382
Redis::lrem
int lrem(const char *key, int count, const char *element)
Definition: Redis.cpp:135
Redis::info
String info(const char *section)
Definition: Redis.cpp:150
Redis::hlen
int hlen(const char *key)
Definition: Redis.cpp:87
Redis::ltrim
bool ltrim(const char *key, int start, int stop)
Definition: Redis.cpp:145
Redis::expire
bool expire(const char *key, int seconds)
Definition: Redis.h:154