xRedis API  1.5.0
The C++ Client API for Redis server
 All Classes
xRedisPool.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_POOL_H_
10 #define _XREDIS_POOL_H_
11 
12 #include "hiredis.h"
13 #include "xLock.h"
14 #include "xRedisClient.h"
15 #include <list>
16 #include <string.h>
17 #include <string>
18 
19 namespace xrc {
20 
21 #define MAX_REDIS_CONN_POOLSIZE 128 // 每个DB最大连接数
22 #define MAX_REDIS_CACHE_TYPE 128 // 最大支持的CACHE种类数
23 #define MAX_REDIS_DB_HASHBASE 128 // 最大HASH分库基数
24 
25 #define GET_CONNECT_ERROR "get connection error"
26 #define CONNECT_CLOSED_ERROR "redis connection be closed"
27 
28 #ifdef WIN32
29 #define strcasecmp stricmp
30 #define strncasecmp strnicmp
31 #define usleep(us) Sleep((us)/1000)
32 #define pthread_self() GetCurrentThreadId()
33 #endif
34 
35 enum { REDISDB_UNCONN,
36  REDISDB_WORKING,
37  REDISDB_DEAD };
38 
40 public:
42  ~RedisConnection();
43 
44  void Init(uint32_t cahcetype, uint32_t sliceindex, const std::string& host,
45  uint32_t port, const std::string& pass, uint32_t poolsize,
46  uint32_t timeout, uint32_t role, uint32_t slaveidx);
47 
48  bool RedisConnect();
49  bool RedisReConnect();
50  bool Ping();
51 
52  redisContext* GetCtx() const { return mCtx; }
53  uint32_t GetdbIndex() const { return mSliceIndex; }
54  uint32_t GetType() const { return mType; }
55  uint32_t GetRole() const { return mRole; }
56  uint32_t GetSlaveIdx() const { return mSlaveIdx; }
57  bool GetConnstatus() const { return mConnStatus; }
58 
59 private:
60  bool Auth();
61  redisContext* ConnectWithTimeout();
62 
63 private:
64  // redis connector context
65  redisContext* mCtx;
66  std::string mHost; // redis host
67  uint32_t mPort; // redis sever port
68  std::string mPass; // redis server password
69  uint32_t mTimeout; // connect timeout second
70  uint32_t mPoolsize; // connect pool size for each redis DB
71  uint32_t mType; // redis cache pool type
72  uint32_t mSliceIndex; // redis DB index
73  uint32_t mRole; // redis role
74  uint32_t mSlaveIdx; // the index in the slave group
75  bool mConnStatus; // redis connection status
76 };
77 
78 typedef std::list<RedisConnection*> RedisConnectionPool;
79 typedef std::list<RedisConnection*>::iterator RedisConnectionIter;
80 
81 typedef std::vector<RedisConnectionPool*> RedisSlaveGroup;
82 typedef std::vector<RedisConnectionPool*>::iterator RedisSlaveGroupIter;
83 
84 typedef struct _RedisSliceConn_ {
85  RedisConnectionPool RedisMasterConnection;
86  RedisSlaveGroup RedisSlaveConnection;
87  xLock MasterLock;
88  xLock SlaveLock;
90 
91 class RedisSlice {
92 public:
93  RedisSlice();
94  ~RedisSlice();
95 
96  void Init(uint32_t cahcetype, uint32_t dbindex);
97  // 连到到一个REDIS服务节点
98  bool ConnectRedisSlice(uint32_t cahcetype, uint32_t dbindex,
99  const std::string& host, uint32_t port,
100  const std::string& passwd, uint32_t poolsize,
101  uint32_t timeout, int32_t role);
102 
103  RedisConnection* GetMasterConn();
104  RedisConnection* GetSlaveConn();
105  RedisConnection* GetConn(int32_t ioRole);
106  void FreeConn(RedisConnection* redisconn);
107  void CloseConnPool();
108  void ConnPoolPing();
109  uint32_t GetStatus() const;
110 
111 private:
112  RedisSliceConn mSliceConn;
113  bool mHaveSlave;
114  uint32_t mType; // redis cache pool type
115  uint32_t mSliceindex; // redis slice index
116  uint32_t mStatus; // redis slice status
117 };
118 
119 class RedisGroup {
120 public:
121  RedisGroup();
122  virtual ~RedisGroup();
123 
124  bool InitDB(uint32_t cachetype, uint32_t hashbase);
125  bool ConnectRedisGroup(uint32_t cahcetype, uint32_t dbindex,
126  const std::string& host, uint32_t port,
127  const std::string& passwd, uint32_t poolsize,
128  uint32_t timeout, uint32_t role);
129 
130  RedisConnection* GetConn(uint32_t dbindex, uint32_t ioRole);
131  void FreeConn(RedisConnection* redisconn);
132  void ClosePool();
133  void KeepAlive();
134  uint32_t GetDBStatus(uint32_t dbindex);
135  uint32_t GetHashBase() const;
136 
137 private:
138  RedisSlice* mSliceList;
139  uint32_t mCachetype;
140  uint32_t mHashbase;
141 };
142 
143 class RedisPool {
144 public:
145  RedisPool();
146  ~RedisPool();
147 
148  bool Init(uint32_t typesize);
149  bool SetHashBase(uint32_t cachetype, uint32_t hashbase);
150  uint32_t GetHashBase(uint32_t cachetype);
151  bool ConnectRedisGroup(uint32_t cachetype, uint32_t sliceindex,
152  const std::string& host, uint32_t port,
153  const std::string& passwd, uint32_t poolsize,
154  uint32_t timeout, uint32_t role);
155  static bool CheckReply(const redisReply* reply);
156  static void FreeReply(const redisReply* reply);
157 
158  RedisConnection* GetConnection(uint32_t cachetype, uint32_t index,
159  uint32_t ioType = MASTER);
160  void FreeConnection(RedisConnection* redisconn);
161 
162  void Keepalive();
163  void Release();
164 
165 private:
166  RedisGroup* mRedisGroupList;
167  uint32_t mTypeSize;
168 };
169 
170 } // namespace xrc
171 
172 #endif
Definition: xRedisPool.h:39
Definition: xLock.h:22
Definition: xRedisPool.h:84
Definition: xRedisPool.h:143
Definition: xRedisPool.h:119
Definition: xRedisPool.h:91

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