xRedis API  1.5.0
The C++ Client API for Redis server
 All Classes
xRedisClient.h
1 /*
2  * ----------------------------------------------------------------------------
3  * Copyright (c) 2013-2021, 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 
22 namespace xrc {
23 
24 
25 #define LOG_LEVEL_ERROR 0
26 #define LOG_LEVEL_WARN 1
27 #define LOG_LEVEL_INFO 2
28 #define LOG_LEVEL_DEBUG 3
29 
30 
31 #define REDIS_REPLY_STRING 1
32 #define REDIS_REPLY_ARRAY 2
33 #define REDIS_REPLY_INTEGER 3
34 #define REDIS_REPLY_NIL 4
35 #define REDIS_REPLY_STATUS 5
36 #define REDIS_REPLY_ERROR 6
37 
38 
39 #define MAX_ERR_STR_LEN 128
40 
41 typedef std::string KEY;
42 typedef std::string VALUE;
43 typedef std::vector<KEY> KEYS;
44 typedef KEYS FILEDS;
45 typedef std::vector<VALUE> VALUES;
46 typedef std::vector<std::string> VDATA;
47 typedef std::set<std::string> SETDATA;
48 
49 typedef struct _REDIS_NODE_{
50  uint32_t dbindex; // 节点编号索引,从0开始
51  std::string host; // REDIS节点主机IP地址
52  uint32_t port; // redis服务端口
53  std::string passwd; // redis认证密码
54  uint32_t poolsize; // 此节点上的连接池大小
55  uint32_t timeout; // 连接超时时间 秒
56  uint32_t role; // 节点角色
57 }RedisNode;
58 
59 /* This is the reply object returned by redisCommand() */
60 typedef struct rReply {
61  int32_t type; /* REDIS_REPLY_* */
62  long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
63  int32_t len; /* Length of string */
64  char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
65  size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
66  struct rReply **element; /* elements vector for REDIS_REPLY_ARRAY */
67 } rReply;
68 
69 typedef uint32_t (*HASHFUN)(const char *);
70 
71 class RedisPool;
72 class xRedisClient;
73 
74 class SliceIndex {
75 public:
76  SliceIndex();
77  SliceIndex(xRedisClient *xredisclient, uint32_t slicetype);
78  ~SliceIndex();
79 
80  bool Create(const char *key, HASHFUN fun=APHash);
81  bool CreateByID(int64_t id);
82  char *GetErrInfo() {return mStrerr;}
83  void SetIOMaster();
84  void SetIOSlave();
85 
86 private:
87  static unsigned int APHash(const char *str);
88  bool SetErrInfo(const char *info, int32_t len);
89  void IOtype(uint32_t iotype);
90  friend class xRedisClient;
91 
92 private:
93  uint32_t mType;
94  uint32_t mIndex;
95  bool mIOFlag;
96  uint32_t mIOtype;
97  char *mStrerr;
98  xRedisClient *mClient;
99 };
100 
101 typedef struct _DATA_ITEM_{
102  int32_t type;
103  std::string str;
104 
105  _DATA_ITEM_ & operator=(const _DATA_ITEM_ &data) {
106  type = data.type;
107  str = data.str;
108  return *this;
109  }
110 }DataItem;
111 typedef std::vector<DataItem> ReplyData;
112 typedef ReplyData ArrayReply;
113 typedef std::map<std::string, double> ZSETDATA;
114 typedef std::vector<SliceIndex> DBIArray;
115 
116 typedef struct xRedisContext_{
117  void* conn;
119 
120 typedef enum _SET_TYPE_{
121  TYPE_NONE = 0,
122  PX = 1,
123  EX = 2
124 }SETPXEX;
125 
126 typedef enum _SET_TYPE_NXEX_{
127  TNXXX_NONE = 0,
128  NX = 1,
129  XX = 2
130 }SETNXXX;
131 
132 typedef enum _BIT_OP_{
133  AND = 0,
134  OR = 1,
135  XOR = 2,
136  NOT = 3
137 }BITOP;
138 
139 typedef enum _LIST_MODEL_{
140  BEFORE = 0,
141  AFTER = 1
142 }LMODEL;
143 
144 
145 typedef enum _SORT_ORDER_{
146  ASC = 0,
147  DESC = 1
148 }SORTODER;
149 
150 typedef struct _SORT_LIMIT_
151 {
152  int32_t offset;
153  int32_t count;
154 }LIMIT;
155 
156 template<class T>
157 std::string toString(const T &t) {
158  std::ostringstream oss;
159  oss << t;
160  return oss.str();
161 }
162 
163 typedef enum _REDIS_ROLE_{
164  MASTER = 0,
165  SLAVE = 1
166 }ROLE;
167 
168 #define SETDEFAULTIOTYPE(type) if (!index.mIOFlag) {SetIOtype(index, type);}
169 
170 
171 
173 public:
174  xRedisClient();
175  ~xRedisClient();
176 
177  bool Init(uint32_t maxtype);
178  void Release();
179  void Keepalive();
180  inline RedisPool *GetRedisPool();
181  static void FreeReply(const rReply* reply);
182  static int32_t GetReply(xRedisContext* ctx, ReplyData& vData);
183  bool GetxRedisContext(const SliceIndex& index, xRedisContext* ctx);
184  void FreexRedisContext(xRedisContext* ctx);
185  bool ConnectRedisCache(const RedisNode *redisnodelist, uint32_t nodecount,
186  uint32_t hashbase, uint32_t cachetype);
187 
188  static void SetLogLevel(uint32_t level, void (*emit)(int level, const char* line));
189 
190 public:
191 
192  // Connection
193  /* AUTH */ /* nonsupport */
194  /* ECHO */ bool echo(const SliceIndex& index, const std::string& str, std::string &value);
195  /* PING */ /* nonsupport */
196  /* QUIT */ void quit();
197  /* SELECT */ /* nonsupport */
198 
199  // Commands operating on std::string values
200  /* APPEND */ bool append(const SliceIndex& index, const std::string& key, const std::string& value);
201  /* BITCOUNT */ bool bitcount(const SliceIndex& index,const std::string& key, int32_t& count, int32_t start=0, int32_t end=0);
202  /* BITOP */ bool bitop(const SliceIndex& index, const BITOP operation, const std::string& destkey, const KEYS& keys, int32_t& lenght);
203  /* BITPOS */ bool bitpos(const SliceIndex& index, const std::string& key, int32_t bit, int64_t& pos, int32_t start=0, int32_t end=0);
204  /* DECR */ bool decr(const SliceIndex& index, const std::string& key, int64_t& result);
205  /* DECRBY */ bool decrby(const SliceIndex& index, const std::string& key, int32_t by, int64_t& result);
206  /* GET */ bool get(const SliceIndex& index, const std::string& key, std::string& value);
207  /* GETBIT */ bool getbit(const SliceIndex& index, const std::string& key, int32_t& offset, int32_t& bit);
208  /* GETRANGE */ bool getrange(const SliceIndex& index,const std::string& key, int32_t start, int32_t end, std::string& out);
209  /* GETSET */ bool getset(const SliceIndex& index, const std::string& key, const std::string& newValue, std::string& oldValue);
210  /* INCR */ bool incr(const SliceIndex& index, const std::string& key, int64_t& result);
211  /* INCRBY */ bool incrby(const SliceIndex& index, const std::string& key, int32_t by, int64_t& result);
212  /* INCRBYFLOAT */
213  /* MGET */ bool mget(const DBIArray& index, const KEYS & keys, ReplyData& vDdata);
214  /* MSET */ bool mset(const DBIArray& index, const VDATA& data);
215  /* MSETNX */
216  /* PSETEX */ bool psetex(const SliceIndex& index, const std::string& key, int32_t milliseconds, const std::string& value);
217  /* SET */ bool set(const SliceIndex& index, const std::string& key, const std::string& value);
218  /* SET */ bool set(const SliceIndex& index, const std::string& key, const char *value, int32_t len, int32_t second);
219  /* SET */ bool set(const SliceIndex& index, const std::string& key, const std::string& value,
220  SETPXEX pxex, int32_t expiretime, SETNXXX nxxx);
221  /* SETBIT */ bool setbit(const SliceIndex& index, const std::string& key, int32_t offset, int64_t newbitValue, int64_t oldbitValue);
222  /* SETEX */ bool setex(const SliceIndex& index, const std::string& key, int32_t seconds, const std::string& value);
223  /* SETNX */ bool setnx(const SliceIndex& index, const std::string& key, const std::string& value);
224  /* SETRANGE */ bool setrange(const SliceIndex& index,const std::string& key, int32_t offset, const std::string& value, int32_t& length);
225  /* STRLEN */ bool strlen(const SliceIndex& index, const std::string& key, int32_t& length);
226 
227 
228  /* DEL */ bool del(const SliceIndex& index, const std::string& key);
229  bool del(const DBIArray& index, const KEYS & vkey, int64_t& count);
230  /* DUMP */
231  /* EXISTS */ bool exists(const SliceIndex& index, const std::string& key);
232  /* EXPIRE */ bool expire(const SliceIndex& index, const std::string& key, uint32_t second);
233  /* EXPIREAT */ bool expireat(const SliceIndex& index, const std::string& key, uint32_t timestamp);
234  /* KEYS */
235  /* MIGRATE */
236  /* MOVE */
237  /* OBJECT */
238  /* PERSIST */ bool persist(const SliceIndex& index, const std::string& key);
239  /* PEXPIRE */ bool pexpire(const SliceIndex& index, const std::string& key, uint32_t milliseconds);
240  /* PEXPIREAT */ bool pexpireat(const SliceIndex& index, const std::string& key, uint32_t millisecondstimestamp);
241  /* PTTL */ bool pttl(const SliceIndex& index, const std::string& key, int64_t &milliseconds);
242  /* RANDOMKEY */ bool randomkey(const SliceIndex& index, KEY& key);
243  /* RENAME */
244  /* RENAMENX */
245  /* RESTORE */
246  /* SCAN */ bool scan(const SliceIndex& index, int64_t &cursor,
247  const char *pattern, uint32_t count, ArrayReply& array, xRedisContext& ctx);
248 
249 
250  /* SORT */ bool sort(const SliceIndex& index, ArrayReply& array, const std::string& key, const char* by = NULL,
251  LIMIT *limit = NULL, bool alpha = false, const FILEDS* get = NULL,
252  const SORTODER order = ASC, const char* destination = NULL);
253 
254  /* TTL */ bool ttl(const SliceIndex& index, const std::string& key, int64_t& seconds);
255  /* TYPE */ bool type(const SliceIndex& index, const std::string& key, std::string& value);
256 
257 
258  /* HDEL */ bool hdel(const SliceIndex& index, const std::string& key, const std::string& field, int64_t& num);
259  bool hdel(const SliceIndex& index, const std::string& key, const KEYS& vfiled, int64_t& num);
260  /* HEXISTS */ bool hexist(const SliceIndex& index, const std::string& key, const std::string& field);
261  /* HGET */ bool hget(const SliceIndex& index, const std::string& key, const std::string& field, std::string& value);
262  /* HGETALL */ bool hgetall(const SliceIndex& index, const std::string& key, ArrayReply& array);
263  /* HINCRBY */ bool hincrby(const SliceIndex& index, const std::string& key, const std::string& field, int64_t increment ,int64_t& value);
264  /* HINCRBYFLOAT */ bool hincrbyfloat(const SliceIndex& index, const std::string& key, const std::string& field, const float increment, float& value);
265  /* HKEYS */ bool hkeys(const SliceIndex& index, const std::string& key, KEYS& keys);
266  /* HLEN */ bool hlen(const SliceIndex& index, const std::string& key, int64_t& count);
267  /* HMGET */ bool hmget(const SliceIndex& index, const std::string& key, const KEYS& field, ArrayReply& array);
268  /* HMSET */ bool hmset(const SliceIndex& index, const std::string& key, const VDATA& vData);
269  /* HSCAN */ bool hscan(const SliceIndex& index, const std::string& key, int64_t &cursor,
270  const char *pattern, uint32_t count, ArrayReply& array, xRedisContext& ctx);
271  /* HSET */ bool hset(const SliceIndex& index, const std::string& key, const std::string& field, const std::string& value, int64_t& retval);
272  /* HSETNX */ bool hsetnx(const SliceIndex& index, const std::string& key, const std::string& field, const std::string& value);
273  /* HVALS */ bool hvals(const SliceIndex& index, const std::string& key, VALUES& values);
274 
275  /* BLPOP */ bool blPop(const SliceIndex& index, const std::string& key, VALUES& vValues, int64_t timeout);
276  /* BRPOP */ bool brPop(const SliceIndex& index, const std::string& key, VALUES& vValues, int64_t timeout);
277  /* BRPOPLPUSH */ bool brPoplpush(const SliceIndex& index, const std::string& key, std::string& targetkey, VALUE& value, int64_t timeout);
278  /* LINDEX */ bool lindex(const SliceIndex& index, const std::string& key, int64_t idx, VALUE& value);
279  /* LINSERT */ bool linsert(const SliceIndex& index, const std::string& key, LMODEL mod, const std::string& pivot, const std::string& value, int64_t& retval);
280  /* LLEN */ bool llen(const SliceIndex& index, const std::string& key, int64_t& len);
281  /* LPOP */ bool lpop(const SliceIndex& index, const std::string& key, std::string& value);
282  /* LPUSH */ bool lpush(const SliceIndex& index, const std::string& key, const VALUES& vValue, int64_t& length);
283  /* LPUSHX */ bool lpushx(const SliceIndex& index, const std::string& key, const std::string& value, int64_t& length);
284  /* LRANGE */ bool lrange(const SliceIndex& index, const std::string& key, int64_t start, int64_t end, ArrayReply& array);
285  /* LREM */ bool lrem(const SliceIndex& index, const std::string& key, int32_t count, const std::string& value, int64_t num);
286  /* LSET */ bool lset(const SliceIndex& index, const std::string& key, int32_t idx, const std::string& value);
287  /* LTRIM */ bool ltrim(const SliceIndex& index, const std::string& key, int32_t start, int32_t end);
288  /* RPOP */ bool rpop(const SliceIndex& index, const std::string& key, std::string& value);
289  /* RPOPLPUSH */ bool rpoplpush(const SliceIndex& index,const std::string& key_src, const std::string& key_dest, std::string& value);
290  /* RPUSH */ bool rpush(const SliceIndex& index, const std::string& key, const VALUES& vValue, int64_t& length);
291  /* RPUSHX */ bool rpushx(const SliceIndex& index, const std::string& key, const std::string& value, int64_t& length);
292 
293  /* SADD */ bool sadd(const SliceIndex& index, const KEY& key, const VALUES& vValue, int64_t& count);
294  /* SCARD */ bool scard(const SliceIndex& index, const KEY& key, int64_t& count);
295  /* SDIFF */ bool sdiff(const DBIArray& index, const KEYS& vKkey, VALUES& vValue);
296  /* SDIFFSTORE */ bool sdiffstore(const SliceIndex& index, const KEY& destinationkey, const DBIArray& vdbi, const KEYS& vkey, int64_t& count);
297  /* SINTER */ bool sinter(const DBIArray& index, const KEYS& vkey, VALUES& vValue);
298  /* SINTERSTORE */ bool sinterstore(const SliceIndex& index, const KEY& destinationkey, const DBIArray& vdbi, const KEYS& vkey, int64_t& count);
299  /* SISMEMBER */ bool sismember(const SliceIndex& index, const KEY& key, const VALUE& member);
300  /* SMEMBERS */ bool smembers(const SliceIndex& index, const KEY& key, VALUES& vValue);
301  /* SMOVE */ bool smove(const SliceIndex& index, const KEY& srckey, const KEY& deskey, const VALUE& member);
302  /* SPOP */ bool spop(const SliceIndex& index, const KEY& key, VALUE& member);
303  /* SRANDMEMBER */ bool srandmember(const SliceIndex& index, const KEY& key, VALUES& vmember, int32_t num=0);
304  /* SREM */ bool srem(const SliceIndex& index, const KEY& key, const VALUES& vmembers, int64_t& count);
305  /* SSCAN */ bool sscan(const SliceIndex& index, const std::string& key, int64_t &cursor,
306  const char *pattern, uint32_t count, ArrayReply& array, xRedisContext& ctx);
307  /* SUNION */ bool sunion(const DBIArray& index, const KEYS& vkey, VALUES& vValue);
308  /* SUNIONSTORE */ bool sunionstore(const SliceIndex& index, const KEY& deskey, const DBIArray& vdbi, const KEYS& vkey, int64_t& count);
309 
310  /* ZADD */ bool zadd(const SliceIndex& index, const KEY& deskey, const VALUES& vValues, int64_t& count);
311  /* ZCARD */ bool zscrad(const SliceIndex& index, const std::string& key, int64_t& num);
312  /* ZCOUNT */
313  /* ZINCRBY */ bool zincrby(const SliceIndex& index, const std::string& key, const double &increment, const std::string& member, std::string& value );
314  /* ZINTERSTORE */
315  /* ZPOPMAX */ bool zpopmax(const SliceIndex& index, const std::string& key, VALUES& vValues);
316  /* ZPOPMIN */ bool zpopmin(const SliceIndex& index, const std::string& key, VALUES& vValues);
317  /* ZRANGE */ bool zrange(const SliceIndex& index, const std::string& key, int32_t start, int32_t end, VALUES& vValues, bool withscore=false);
318  /* ZRANGEBYSCORE */ bool zrangebyscore(const SliceIndex& index, const std::string& key, const std::string& min,
319  const std::string& max, VALUES& vValues, bool withscore=false, LIMIT *limit = NULL);
320  /* ZRANK */ bool zrank(const SliceIndex& index, const std::string& key, const std::string& member, int64_t &rank);
321  /* ZREM */ bool zrem(const SliceIndex& index, const KEY& key, const VALUES& vmembers, int64_t &num);
322  /* ZREMRANGEBYRANK */ bool zremrangebyrank(const SliceIndex& index, const std::string& key, int32_t start, int32_t stop, int64_t& num);
323  /* ZREMRANGEBYSCORE */ bool zremrangebyscore(const SliceIndex& index, const KEY& key, double min, double max, int64_t& count);
324  /* ZREVRANGE */ bool zrevrange(const SliceIndex& index, const std::string& key, int32_t start, int32_t end, VALUES& vValues, bool withscore=false);
325  /* ZREVRANGEBYLEX */ bool zrevrangebylex(const SliceIndex& index, const std::string& key, std::string& start, std::string& end, VALUES& vValues, int32_t offset = 0, int32_t count = 0);
326  /* ZREVRANGEBYSCORE */
327  /* ZREVRANK */ bool zrevrank(const SliceIndex& index, const std::string& key, const std::string &member, int64_t& rank);
328  /* ZSCAN */ bool zscan(const SliceIndex& index, const std::string& key, int64_t &cursor, const char *pattern,
329  uint32_t count, ArrayReply& array, xRedisContext& ctx);
330  /* ZSCORE */ bool zscore(const SliceIndex& index, const std::string& key, const std::string &member, std::string& score);
331  /* ZUNIONSTORE */
332 
333  /* 注意: 使用下面的 pub/sub 命令时,一定要保证数据都在同一个REDIS实例里,xredis目前的pub/sub命令实现不支持多节点数据分布的场景。 */
334  /* PSUBSCRIBE */ bool psubscribe(const SliceIndex& index, const KEYS& patterns, xRedisContext& ctx);
335  /* PUBLISH */ bool publish(const SliceIndex& index, const KEY& channel, const std::string& message, int64_t& count);
336  /* PUBSUB */ bool pubsub_channels(const SliceIndex& index, const std::string &pattern, ArrayReply &reply);
337  bool pubsub_numsub(const SliceIndex& index, const KEYS &keys, ArrayReply &reply);
338  bool pubsub_numpat(const SliceIndex& index, int64_t& count);
339  /* PUNSUBSCRIBE */ bool punsubscribe(const SliceIndex& index, const KEYS& patterns, xRedisContext& ctx);
340  /* SUBSCRIBE */ bool subscribe(const SliceIndex& index, const KEYS& channels, xRedisContext& ctx);
341  /* UNSUBSCRIBE */ bool unsubscribe(const SliceIndex& index, const KEYS& channels, xRedisContext& ctx);
342 
343 
344  /* DISCARD */
345  /* EXEC */
346  /* MULTI */
347  /* UNWATCH */
348  /* WATCH */
349 
350 
351 private:
352  void addparam(VDATA& vDes, const VDATA& vSrc) {
353  for (VDATA::const_iterator iter=vSrc.begin(); iter!=vSrc.end();++iter) {
354  vDes.push_back(*iter);
355  }
356  }
357  void SetErrInfo(const SliceIndex& index, void *p);
358  void SetErrString(const SliceIndex& index, const char *str, int32_t len);
359  void SetErrMessage(const SliceIndex& index, const char* fmt, ...);
360  void SetIOtype(const SliceIndex& index, uint32_t iotype, bool ioflag = false);
361  bool ScanFun(const char* cmd, const SliceIndex& index, const std::string *key, int64_t &cursor,
362  const char* pattern, uint32_t count, ArrayReply& array, xRedisContext& ctx);
363 
364 public:
365 
366  bool command_bool(const SliceIndex& index, const char* cmd, ...);
367  bool command_status(const SliceIndex& index, const char* cmd, ...);
368  bool command_integer(const SliceIndex& index, int64_t &intval, const char* cmd, ...);
369  bool command_string(const SliceIndex& index, std::string &data, const char* cmd, ...);
370  bool command_list(const SliceIndex& index, VALUES &vValue, const char* cmd, ...);
371  bool command_array(const SliceIndex& index, ArrayReply& array, const char* cmd, ...);
372  rReply *command(const SliceIndex& index, const char* cmd);
373 private:
374  bool commandargv_bool(const SliceIndex& index, const VDATA& vData);
375  bool commandargv_status(const SliceIndex& index, const VDATA& vData);
376  bool commandargv_array(const SliceIndex& index, const VDATA& vDataIn, ArrayReply& array);
377  bool commandargv_array(const SliceIndex& index, const VDATA& vDataIn, VALUES& array);
378  bool commandargv_integer(const SliceIndex& index,const VDATA& vDataIn, int64_t& retval);
379  bool commandargv_array_ex(const SliceIndex& index, const VDATA& vDataIn, xRedisContext& ctx);
380 private:
381  RedisPool *mRedisPool;
382 };
383 
384 }
385 
386 #endif
387 
388 
389 
390 
391 
Definition: xRedisClient.h:150
Definition: xRedisClient.h:101
Definition: xRedisClient.h:60
Definition: xRedisClient.h:116
Definition: xRedisClient.h:172
Definition: xRedisPool.h:143
Definition: xRedisClient.h:49
Definition: xRedisClient.h:74

Generated on Mon Mar 21 2022 10:57:13 for xRedis API version 1.5.0.