Arduino-Redis
A Redis library for Arduino.
Redis.cpp
1 #include "Redis.h"
2 #include "RedisInternal.h"
3 
4 #pragma mark Redis class implemenation
5 
6 RedisReturnValue Redis::authenticate(const char* password)
7 {
8  if(conn.connected())
9  {
10  int passwordLength = strlen(password);
11  if (passwordLength > 0)
12  {
13  auto cmdRet = RedisCommand("AUTH", ArgList{password}).issue(conn);
14  return cmdRet->type() == RedisObject::Type::SimpleString && (String)*cmdRet == "OK"
15  ? RedisSuccess : RedisAuthFailure;
16  }
17 
18  return RedisSuccess;
19  }
20 
21  return RedisNotConnectedFailure;
22 }
23 
24 bool Redis::set(const char* key, const char* value)
25 {
26  return ((String)*RedisCommand("SET", ArgList{key, value}).issue(conn)).indexOf("OK") != -1;
27 }
28 
29 #define TRCMD(t, c, ...) return RedisCommand(c, ArgList{__VA_ARGS__}).issue_typed<t>(conn)
30 
31 String Redis::get(const char* key)
32 {
33  TRCMD(String, "GET", key);
34 }
35 
36 bool Redis::del(const char* key)
37 {
38  TRCMD(bool, "DEL", key);
39 }
40 
41 int Redis::append(const char* key, const char* value)
42 {
43  TRCMD(int, "APPEND", key, value);
44 }
45 
46 int Redis::publish(const char* channel, const char* message)
47 {
48  TRCMD(int, "PUBLISH", channel, message);
49 }
50 
51 bool Redis::exists(const char* key)
52 {
53  TRCMD(bool, "EXISTS", key);
54 }
55 
56 bool Redis::_expire_(const char* key, int arg, const char* cmd_var)
57 {
58  TRCMD(bool, cmd_var, key, String(arg));
59 }
60 
61 bool Redis::persist(const char* key)
62 {
63  TRCMD(bool, "PERSIST", key);
64 }
65 
66 int Redis::_ttl_(const char* key, const char* cmd_var)
67 {
68  TRCMD(int, cmd_var, key);
69 }
70 
71 bool Redis::_hset_(const char* key, const char* field, const char* value, const char* cmd_var)
72 {
73  TRCMD(int, cmd_var, key, field, value);
74 }
75 
76 String Redis::hget(const char* key, const char* field)
77 {
78  TRCMD(String, "HGET", key, field);
79 }
80 
81 bool Redis::hdel(const char* key, const char* field)
82 {
83  TRCMD(bool, "HDEL", key, field);
84 }
85 
86 int Redis::hlen(const char* key)
87 {
88  TRCMD(int, "HLEN", key);
89 }
90 
91 int Redis::hstrlen(const char* key, const char* field)
92 {
93  TRCMD(int, "HSTRLEN", key, field);
94 }
95 
96 bool Redis::hexists(const char* key, const char* field)
97 {
98  TRCMD(bool, "HEXISTS", key, field);
99 }
100 
101 std::vector<String> Redis::lrange(const char* key, int start, int stop)
102 {
103  auto rv = RedisCommand("LRANGE", ArgList{key, String(start), String(stop)}).issue(conn);
104  return rv->type() == RedisObject::Type::Array
105  ? (std::vector<String>)*((RedisArray*)rv.get())
106  : std::vector<String>();
107 }
String hget(const char *key, const char *field)
Definition: Redis.cpp:76
bool del(const char *key)
Definition: Redis.cpp:36
bool set(const char *key, const char *value)
Definition: Redis.cpp:24
int hlen(const char *key)
Definition: Redis.cpp:86
String get(const char *key)
Definition: Redis.cpp:31
int publish(const char *channel, const char *message)
Definition: Redis.cpp:46
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
int hstrlen(const char *key, const char *field)
Definition: Redis.cpp:91
bool exists(const char *key)
Definition: Redis.cpp:51
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 hexists(const char *key, const char *field)
Definition: Redis.cpp:96