xRedis API  1.1
The C++ Client API for Redis server
xRedisClient.h
1 /*
2  * ----------------------------------------------------------------------------
3  * Copyright (c) 2013-2014, xSky <guozhw at gmail dot com>
4  * All rights reserved.
5  * Distributed under GPL license.
6  * ----------------------------------------------------------------------------
7  */
8 
9 #ifndef _XREDIS_CLIENT_H_
10 #define _XREDIS_CLIENT_H_
11 
12 #include <stdint.h>
13 #include <string.h>
14 #include <string>
15 #include <vector>
16 #include <set>
17 #include <map>
18 #include <algorithm>
19 #include <sstream>
20 
21 using namespace std;
22 
23 #define REDIS_REPLY_STRING 1
24 #define REDIS_REPLY_ARRAY 2
25 #define REDIS_REPLY_INTEGER 3
26 #define REDIS_REPLY_NIL 4
27 #define REDIS_REPLY_STATUS 5
28 #define REDIS_REPLY_ERROR 6
29 
30 
31 #define MAX_ERR_STR_LEN 128
32 
33 typedef std::string KEY;
34 typedef std::string VALUE;
35 typedef std::vector<KEY> KEYS;
36 typedef KEYS FILEDS;
37 typedef std::vector<VALUE> VALUES;
38 typedef std::vector<string> VDATA;
39 
40 typedef std::set<string> SETDATA;
41 
42 typedef struct _REDIS_NODE_{
43  unsigned int dbindex;
44  const char *host;
45  unsigned int port;
46  const char *passwd;
47  unsigned int poolsize;
48  unsigned int timeout;
49  unsigned int role;
50 }RedisNode;
51 
52 typedef unsigned int (*HASHFUN)(const char *);
53 
54 class RedisPool;
55 class xRedisClient;
56 
57 class RedisDBIdx {
58 public:
59  RedisDBIdx();
60  RedisDBIdx(xRedisClient *xredisclient);
61  ~RedisDBIdx();
62 
63  bool CreateDBIndex(const char *key, HASHFUN fun, const unsigned int type);
64  bool CreateDBIndex(const int64_t id, const unsigned int type);
65  char *GetErrInfo() {return mStrerr;}
66  void SetIOType();
67 
68 private:
69  bool SetErrInfo(const char *info, int len);
70  void IOtype(unsigned int type);
71  friend class xRedisClient;
72 
73 private:
74  unsigned int mType;
75  unsigned int mIndex;
76  char *mStrerr;
77  xRedisClient *mClient;
78  unsigned int mIOtype;
79  bool mIOFlag;
80 };
81 
82 typedef struct _DATA_ITEM_{
83  int type;
84  string str;
85 
86  _DATA_ITEM_ & operator=(const _DATA_ITEM_ &data) {
87  type = data.type;
88  str = data.str;
89  return *this;
90  }
91 }DataItem;
92 typedef std::vector<DataItem> ReplyData;
93 typedef ReplyData ArrayReply;
94 typedef std::map<string, double> ZSETDATA;
95 typedef std::vector<RedisDBIdx> DBIArray;
96 
97 
98 typedef enum _BIT_OP_{
99  AND = 0,
100  OR = 1,
101  XOR = 2,
102  NOT = 3
103 }BITOP;
104 
105 typedef enum _LIST_MODEL_{
106  BEFORE = 0,
107  AFTER = 1
108 }LMODEL;
109 
110 
111 typedef enum _SORT_ORDER_{
112  ASC = 0,
113  DESC = 1
114 }SORTODER;
115 
116 typedef struct _SORT_LIMIT_
117 {
118  int offset;
119  int count;
120 }LIMIT;
121 
122 template<class T>
123 string toString(const T &t) {
124  ostringstream oss;
125  oss << t;
126  return oss.str();
127 }
128 
129 typedef enum _REDIS_ROLE_{
130  MASTER = 0,
131  SLAVE = 1
132 }ROLE;
133 
134 #define SETDEFAULTIOTYPE(type) if (!dbi.mIOFlag) {SetIOtype(dbi, type);}
135 
137 public:
138  xRedisClient();
139  ~xRedisClient();
140 
141  bool Init(unsigned int maxtype);
142  void release();
143  void KeepAlive();
144  inline RedisPool *GetRedisPool();
145  bool ConnectRedisCache( const RedisNode *redisnodelist, unsigned int hashbase, unsigned int cachetype);
146 
147 public:
148 
149  // connection
150  /* AUTH */ bool auth(const RedisDBIdx& dbi, const string& pass);
151  /* ECHO */
152  /* PING */
153  /* QUIT */
154  /* SELECT */
155 
156  // Commands operating on string values
157  /* APPEND */ bool append(const RedisDBIdx& dbi, const string& key, const string& value);
158  /* BITCOUNT */ bool bitcount(const RedisDBIdx& dbi,const string& key, int& count, const int start=0, const int end=0);
159  /* BITOP */ bool bitop(const RedisDBIdx& dbi, const BITOP operation, const string& destkey, const KEYS& keys, int& lenght);
160  /* BITPOS */ bool bitpos(const RedisDBIdx& dbi, const string& key, const int bit, int64_t& pos, const int start=0, const int end=0);
161  /* DECR */ bool decr(const RedisDBIdx& dbi, const string& key, int64_t& result);
162  /* DECRBY */ bool decrby(const RedisDBIdx& dbi, const string& key, const int by, int64_t& result);
163  /* GET */ bool get(const RedisDBIdx& dbi, const string& key, string& value);
164  /* GETBIT */ bool getbit(const RedisDBIdx& dbi, const string& key, const int& offset, int& bit);
165  /* GETRANGE */ bool getrange(const RedisDBIdx& dbi,const string& key, const int start, const int end, string& out);
166  /* GETSET */ bool getset(const RedisDBIdx& dbi, const string& key, const string& newValue, string& oldValue);
167  /* INCR */ bool incr(const RedisDBIdx& dbi, const string& key, int64_t& result);
168  /* INCRBY */ bool incrby(const RedisDBIdx& dbi, const string& key, const int by, int64_t& result);
169  /* INCRBYFLOAT */
170  /* MGET */ bool mget(const DBIArray& dbi, const KEYS & keys, ReplyData& vDdata);
171  /* MSET */ bool mset(const DBIArray& dbi, const VDATA& data);
172  /* MSETNX */
173  /* PSETEX */ bool psetex(const RedisDBIdx& dbi, const string& key, const int milliseconds, const string& value);
174  /* SET */ bool set(const RedisDBIdx& dbi, const string& key, const string& value);
175  /* SET */ bool set(const RedisDBIdx& dbi, const string& key, const char *value, int len, const int second=0);
176  /* SETBIT */ bool setbit(const RedisDBIdx& dbi, const string& key, const int offset, const int64_t newbitValue, int64_t oldbitValue);
177  /* SETEX */ bool setex(const RedisDBIdx& dbi, const string& key, const int seconds, const string& value);
178  /* SETNX */ bool setnx(const RedisDBIdx& dbi, const string& key, const string& value);
179  /* SETRANGE */ bool setrange(const RedisDBIdx& dbi,const string& key, const int offset, const string& value, int& length);
180  /* STRLEN */ bool strlen(const RedisDBIdx& dbi, const string& key, int& length);
181 
182 
183  /* DEL */ bool del(const RedisDBIdx& dbi, const string& key);
184  bool del(const DBIArray& dbi, const KEYS & vkey, int64_t& count);
185  /* DUMP */
186  /* EXISTS */ bool exists(const RedisDBIdx& dbi, const string& key);
187  /* EXPIRE */ bool expire(const RedisDBIdx& dbi, const string& key, const unsigned int second);
188  /* EXPIREAT */ bool expireat(const RedisDBIdx& dbi, const string& key, const unsigned int timestamp);
189  /* KEYS */
190  /* MIGRATE */
191  /* MOVE */
192  /* OBJECT */
193  /* PERSIST */ bool persist(const RedisDBIdx& dbi, const string& key);
194  /* PEXPIRE */ bool pexpire(const RedisDBIdx& dbi, const string& key, const unsigned int milliseconds);
195  /* PEXPIREAT */ bool pexpireat(const RedisDBIdx& dbi, const string& key, const unsigned int millisecondstimestamp);
196  /* PTTL */ bool pttl(const RedisDBIdx& dbi, const string& key, int64_t &milliseconds);
197  /* RANDOMKEY */ bool randomkey(const RedisDBIdx& dbi, KEY& key);
198  /* RENAME */
199  /* RENAMENX */
200  /* RESTORE */
201  /* SCAN */
202 
203 
204  /* SORT */ bool sort(const RedisDBIdx& dbi, ArrayReply& array, const string& key, const char* by = NULL,
205  LIMIT *limit = NULL, bool alpha = false, const FILEDS* get = NULL,
206  const SORTODER order = ASC, const char* destination = NULL);
207 
208  /* TTL */ bool ttl(const RedisDBIdx& dbi, const string& key, int64_t& seconds);
209  /* TYPE */
210 
211 
212  /* HDEL */ bool hdel(const RedisDBIdx& dbi, const string& key, const string& filed, int64_t& num);
213  bool hdel(const RedisDBIdx& dbi, const string& key, const KEYS& vfiled, int64_t& num);
214  /* HEXISTS */ bool hexist(const RedisDBIdx& dbi, const string& key, const string& filed);
215  /* HGET */ bool hget(const RedisDBIdx& dbi, const string& key, const string& filed, string& value);
216  /* HGETALL */ bool hgetall(const RedisDBIdx& dbi, const string& key, ArrayReply& array);
217  /* HINCRBY */ bool hincrby(const RedisDBIdx& dbi, const string& key, const string& filed, const int64_t increment ,int64_t& value);
218  /* HINCRBYFLOAT */ bool hincrbyfloat(const RedisDBIdx& dbi, const string& key, const string& filed, const float increment, float& value);
219  /* HKEYS */ bool hkeys(const RedisDBIdx& dbi, const string& key, KEYS& keys);
220  /* HLEN */ bool hlen(const RedisDBIdx& dbi, const string& key, int64_t& count);
221  /* HMGET */ bool hmget(const RedisDBIdx& dbi, const string& key, const KEYS& filed, ArrayReply& array);
222  /* HMSET */ bool hmset(const RedisDBIdx& dbi, const string& key, const VDATA& vData);
223  /* HSCAN */
224  /* HSET */ bool hset(const RedisDBIdx& dbi, const string& key, const string& filed, const string& value, int64_t& retval);
225  /* HSETNX */ bool hsetnx(const RedisDBIdx& dbi, const string& key, const string& filed, const string& value);
226  /* HVALS */ bool hvals(const RedisDBIdx& dbi, const string& key, VALUES& values);
227 
228  /* BLPOP */
229  /* BRPOP */
230  /* BRPOPLPUSH */
231  /* LINDEX */ bool lindex(const RedisDBIdx& dbi, const string& key, const int64_t index, VALUE& value);
232  /* LINSERT */ bool linsert(const RedisDBIdx& dbi, const string& key, const LMODEL mod, const string& pivot, const string& value, int64_t& retval);
233  /* LLEN */ bool llen(const RedisDBIdx& dbi, const string& key, int64_t& len);
234  /* LPOP */ bool lpop(const RedisDBIdx& dbi, const string& key, string& value);
235  /* LPUSH */ bool lpush(const RedisDBIdx& dbi, const string& key, const VALUES& vValue, int64_t& length);
236  /* LPUSHX */ bool lpushx(const RedisDBIdx& dbi, const string& key, const string& value, int64_t& length);
237  /* LRANGE */ bool lrange(const RedisDBIdx& dbi, const string& key, const int64_t start, const int64_t end, ArrayReply& array);
238  /* LREM */ bool lrem(const RedisDBIdx& dbi, const string& key, const int count, const string& value, int64_t num);
239  /* LSET */ bool lset(const RedisDBIdx& dbi, const string& key, const int index, const string& value);
240  /* LTRIM */ bool ltrim(const RedisDBIdx& dbi, const string& key, const int start, const int end);
241  /* RPOP */ bool rpop(const RedisDBIdx& dbi, const string& key, string& value);
242  /* RPOPLPUSH */ bool rpoplpush(const RedisDBIdx& dbi,const string& key_src, const string& key_dest, string& value);
243  /* RPUSH */ bool rpush(const RedisDBIdx& dbi, const string& key, const VALUES& vValue, int64_t& length);
244  /* RPUSHX */ bool rpushx(const RedisDBIdx& dbi, const string& key, const string& value, int64_t& length);
245 
246 
247 
248  /* SADD */ bool sadd(const RedisDBIdx& dbi, const KEY& key, const VALUES& vValue, int64_t& count);
249  /* SCARD */ bool scard(const RedisDBIdx& dbi, const KEY& key, int64_t& count);
250  /* SDIFF */ bool sdiff(const DBIArray& dbi, const KEYS& vKkey, VALUES& vValue);
251  /* SDIFFSTORE */ bool sdiffstore(const RedisDBIdx& dbi, const KEY& destinationkey, const DBIArray& vdbi, const KEYS& vkey, int64_t& count);
252  /* SINTER */ bool sinter(const DBIArray& dbi, const KEYS& vkey, VALUES& vValue);
253  /* SINTERSTORE */ bool sinterstore(const RedisDBIdx& dbi, const KEY& destinationkey, const DBIArray& vdbi, const KEYS& vkey, int64_t& count);
254  /* SISMEMBER */ bool sismember(const RedisDBIdx& dbi, const KEY& key, const VALUE& member);
255  /* SMEMBERS */ bool smembers(const RedisDBIdx& dbi, const KEY& key, VALUES& vValue);
256  /* SMOVE */ bool smove(const RedisDBIdx& dbi, const KEY& srckey, const KEY& deskey, const VALUE& member);
257  /* SPOP */ bool spop(const RedisDBIdx& dbi, const KEY& key, VALUE& member);
258  /* SRANDMEMBER */ bool srandmember(const RedisDBIdx& dbi, const KEY& key, VALUES& vmember, int num=0);
259  /* SREM */ bool srem(const RedisDBIdx& dbi, const KEY& key, const VALUES& vmembers, int64_t& count);
260  /* SSCAN */
261  /* SUNION */ bool sunion(const DBIArray& dbi, const KEYS& vkey, VALUES& vValue);
262  /* SUNIONSTORE */ bool sunionstore(const RedisDBIdx& dbi, const KEY& deskey, const DBIArray& vdbi, const KEYS& vkey, int64_t& count);
263 
264  /* ZADD */ bool zadd(const RedisDBIdx& dbi, const KEY& deskey, const VALUES& vValues, int64_t& count);
265  /* ZCARD */ bool zscrad(const RedisDBIdx& dbi, const string& key, int64_t& num);
266  /* ZCOUNT */
267  /* ZINCRBY */ bool zincrby(const RedisDBIdx& dbi, const string& key, const double &increment, const string& member, string& value );
268  /* ZINTERSTORE */
269  /* ZRANGE */ bool zrange(const RedisDBIdx& dbi, const string& key, int start, int end, VALUES& vValues, bool withscore=false);
270  /* ZRANGEBYSCORE */
271  /* ZRANK */ bool zrank(const RedisDBIdx& dbi, const string& key, const string& member, int64_t &rank);
272  /* ZREM */ bool zrem(const RedisDBIdx& dbi, const KEY& key, const VALUES& vmembers, int64_t &num);
273  /* ZREMRANGEBYRANK */ bool zremrangebyrank(const RedisDBIdx& dbi, const string& key, const int start, const int stop, int64_t& num);
274  /* ZREMRANGEBYSCORE */
275  /* ZREVRANGE */ bool zrevrange(const RedisDBIdx& dbi, const string& key, int start, int end, VALUES& vValues, bool withscore=false);
276  /* ZREVRANGEBYSCORE */
277  /* ZREVRANK */ bool zrevrank(const RedisDBIdx& dbi, const string& key, const string &member, int64_t& rank);
278  /* ZSCAN */
279  /* ZSCORE */ bool zscore(const RedisDBIdx& dbi, const string& key, const string &member, string& score);
280  /* ZUNIONSTORE */
281 
282  /* PSUBSCRIBE */
283  /* PUBLISH */
284  /* PUBSUB */
285  /* PUNSUBSCRIBE */
286  /* SUBSCRIBE */
287  /* UNSUBSCRIBE */
288 
289 
290  /* DISCARD */
291  /* EXEC */
292  /* MULTI */
293  /* UNWATCH */
294  /* WATCH */
295 
296 
297 private:
298  void addparam(VDATA& vDes, const VDATA& vSrc) {
299  for (VDATA::const_iterator iter=vSrc.begin(); iter!=vSrc.end();++iter) {
300  vDes.push_back(*iter);
301  }
302  }
303  void SetErrInfo(const RedisDBIdx& dbi, void *p);
304  void SetErrString(const RedisDBIdx& dbi, const char *str, int len);
305  void SetErrMessage(const RedisDBIdx& dbi, const char* fmt, ...);
306  void SetIOtype(const RedisDBIdx& dbi, unsigned int iotype);
307 public:
308 
309  bool command_bool(const RedisDBIdx& dbi, const char* cmd, ...);
310  bool command_status(const RedisDBIdx& dbi, const char* cmd, ...);
311  bool command_integer(const RedisDBIdx& dbi, int64_t &intval, const char* cmd, ...);
312  bool command_string(const RedisDBIdx& dbi, string &data, const char* cmd, ...);
313  bool command_list(const RedisDBIdx& dbi, VALUES &vValue, const char* cmd, ...);
314  bool command_array(const RedisDBIdx& dbi, ArrayReply& array, const char* cmd, ...);
315 
316 private:
317  bool commandargv_bool(const RedisDBIdx& dbi, const VDATA& vData);
318  bool commandargv_status(const RedisDBIdx& dbi, const VDATA& vData);
319  bool commandargv_array(const RedisDBIdx& dbi, const VDATA& vDataIn, ArrayReply& array);
320  bool commandargv_array(const RedisDBIdx& dbi, const VDATA& vDataIn, VALUES& array);
321  bool commandargv_integer(const RedisDBIdx& dbi,const VDATA& vDataIn, int64_t& retval);
322 
323 private:
324  RedisPool *mRedisPool;
325 };
326 
327 
328 
329 #endif
330 
331 
332 
333 
334 
Definition: xRedisClient.h:82
Definition: xRedisClient.h:57
Definition: xRedisPool.h:472
Definition: xRedisClient.h:116
Definition: xRedisClient.h:42
Definition: xRedisClient.h:136

Generated on Fri Oct 23 2015 13:13:57 for xRedis API version 1.1.