AOSharedServiceLibrary
redis_interface.h
1 /*
2 MIT License Block
3 
4 Copyright (c) 2016 Alex Barry
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24 
25 #ifndef AOSSL_REDIS_INCLUDE_REDIS_INTERFACE_H_
26 #define AOSSL_REDIS_INCLUDE_REDIS_INTERFACE_H_
27 
28 #include <string.h>
29 #include <string>
30 #include <vector>
31 #include <exception>
32 
33 #include "aossl/core/include/buffers.h"
34 #include "aossl/core/include/slot_pool.h"
35 
37 struct RedisConnectionException: public std::exception {
39  std::string int_msg;
40  const char * what_str;
41 
43  inline RedisConnectionException(std::string msg) {
44  int_msg = "Error Connecting to Redis: " + msg;
45  what_str = int_msg.c_str();
46  }
47 
49  inline RedisConnectionException(const char * msg_cstr) {
50  std::string msg(msg_cstr);
51  int_msg = msg;
52  }
53 
55  ~RedisConnectionException() throw() {}
56 
58  const char * what() const throw() {
59  std::string what_str = "Error Connecting to Redis: " + int_msg;
60  return what_str.c_str();
61  }
62 };
63 
66 struct RedisOperationException: public std::exception {
68  std::string int_msg;
69 
71  RedisOperationException(std::string msg) {int_msg = msg;}
72 
74  inline RedisOperationException(const char * msg_cstr) {
75  std::string msg(msg_cstr);
76  int_msg = msg;
77  }
78 
80  ~RedisOperationException() throw() {}
81 
83  const char * what() const throw() {
84  std::string what_str = "Error Performing Redis Operation: " + int_msg;
85  return what_str.c_str();
86  }
87 };
88 
92  std::string ip;
93 
95  int port;
96 
98  std::string password;
99 
102 
104  int timeout;
105 
107  int role;
108 };
109 
112 
114 
118  public:
119  virtual ~RedisInterface() {}
120 
122 
124  virtual std::string load(std::string key) = 0;
125 
127  virtual AOSSL::StringBuffer* load_safe(std::string key) = 0;
128 
130  virtual bool save(std::string key, std::string msg) = 0;
131 
133  virtual bool setnx(std::string key, std::string msg) = 0;
134 
136  virtual bool exists(std::string key) = 0;
137 
139  virtual bool del(std::string key) = 0;
140 
142  virtual bool expire(std::string key, unsigned int second) = 0;
143 
145  virtual bool persist(std::string key) = 0;
146 
148  virtual bool setex(std::string key, std::string val, unsigned int second) = 0;
149 
151  virtual int append(std::string key, std::string val) = 0;
152 
154  virtual int len(std::string key) = 0;
155 
157  virtual int incr(std::string key) = 0;
158 
160  virtual int incr(std::string key, int incr_amt) = 0;
161 
163  virtual int decr(std::string key) = 0;
164 
166  virtual int decr(std::string key, int decr_amt) = 0;
167 
169  virtual int lpush(std::string key, std::string val) = 0;
170 
172  virtual int rpush(std::string key, std::string val) = 0;
173 
175 
177  virtual std::string lpop(std::string key) = 0;
178 
180  virtual AOSSL::StringBuffer* lpop_safe(std::string key) = 0;
181 
183 
185  virtual std::string rpop(std::string key) = 0;
186 
188  virtual AOSSL::StringBuffer* rpop_safe(std::string key) = 0;
189 
191  virtual bool lset(std::string key, std::string val, int index) = 0;
192 
194  virtual int linsert(std::string key, std::string val, std::string pivot, \
195  bool before_pivot) = 0;
196 
198 
200  virtual std::string lindex(std::string key, int index) = 0;
201 
203  virtual AOSSL::StringBuffer* lindex_safe(std::string key, int index) = 0;
204 
206  virtual int llen(std::string key) = 0;
207 
209  virtual bool ltrim(std::string key, int start_index, int end_index) = 0;
210 
212  virtual std::vector<std::string> mget(std::vector<std::string> keys) = 0;
213 
215  virtual bool mset(std::vector<RedisKvPair> save_sets) = 0;
216 
218  virtual bool msetnx(std::vector<RedisKvPair> save_sets) = 0;
219 };
220 
221 #endif // AOSSL_REDIS_INCLUDE_REDIS_INTERFACE_H_
RedisOperationException(std::string msg)
Create a Redis Operation Exception, and store the given error message.
Definition: redis_interface.h:71
int timeout
The timeout on the Redis Node.
Definition: redis_interface.h:104
A Structure for storing Redis Connection Information.
Definition: redis_interface.h:90
The Redis Admin.
Definition: redis_interface.h:117
int pool_size
The pool size on the Redis Node.
Definition: redis_interface.h:101
A Structure for holding a single value.
Definition: buffers.h:42
const char * what() const
Show the error message in readable format.
Definition: redis_interface.h:58
std::string ip
The IP of the Redis Node.
Definition: redis_interface.h:92
RedisConnectionException(const char *msg_cstr)
Create a Redis Connection Exception, and store the given error message.
Definition: redis_interface.h:49
A Structure for storing a Key-Value pair.
Definition: buffers.h:48
int port
The port of the Redis Node.
Definition: redis_interface.h:95
An Implementation of std::exception that denotes a connection error in Redis.
Definition: redis_interface.h:37
int role
The Role on the Redis Node.
Definition: redis_interface.h:107
const char * what() const
Show the error message in readable format.
Definition: redis_interface.h:83
Definition: redis_interface.h:66
std::string int_msg
An error message passed on initialization.
Definition: redis_interface.h:39
std::string password
The password of the Redis Node.
Definition: redis_interface.h:98
RedisOperationException(const char *msg_cstr)
Create a Redis Operation Exception, and store the given error message.
Definition: redis_interface.h:74
std::string int_msg
An error message passed on initialization.
Definition: redis_interface.h:68
RedisConnectionException(std::string msg)
Create a Redis Connection Exception, and store the given error message.
Definition: redis_interface.h:43