-
Notifications
You must be signed in to change notification settings - Fork 153
/
xRedisClusterClient.h
105 lines (89 loc) · 2.87 KB
/
xRedisClusterClient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* ----------------------------------------------------------------------------
* Copyright (c) 2013-2021, xSky <guozhw at gmail dot com>
* All rights reserved.
* Distributed under GPL license.
* ----------------------------------------------------------------------------
*/
#ifndef _XREDIS_CLUSTER_CLIENT_H_
#define _XREDIS_CLUSTER_CLIENT_H_
#include "hiredis.h"
#include "xLock.h"
#include <list>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
namespace xrc {
#define LOG_LEVEL_ERROR 0
#define LOG_LEVEL_WARN 1
#define LOG_LEVEL_INFO 2
#define LOG_LEVEL_DEBUG 3
class xRedisClusterManager;
class RedisResult;
typedef std::vector<std::string> VString;
class ClusterInfo;
class RedisResult {
public:
RedisResult() { }
~RedisResult()
{
if (Reply.reply) {
freeReplyObject((void*)Reply.reply);
}
}
public:
class RedisReply {
public:
RedisReply(redisReply* r) { reply = r; }
RedisReply(const RedisReply& r) { reply = r.reply; }
RedisReply() { reply = NULL; }
~RedisReply() { }
inline int32_t type() const { return reply->type; }
inline long long integer() const { return reply->integer; }
inline int32_t len() const { return reply->len; }
inline char* str() const { return reply->str; }
inline size_t elements() const { return reply->elements; }
inline struct RedisReply element(uint32_t index) const
{
return RedisReply(reply->element[index]);
}
private:
friend class RedisResult;
redisReply* reply;
};
inline void Init(redisReply* r)
{
if (Reply.reply) {
freeReplyObject((void*)Reply.reply);
}
Reply.reply = r;
}
inline int32_t type() const { return Reply.type(); }
inline long long integer() const { return Reply.integer(); }
inline int32_t len() const { return Reply.len(); }
inline char* str() const { return Reply.str(); }
inline size_t elements() const { return Reply.elements(); }
inline RedisReply element(uint32_t index) const { return Reply.element(index); }
private:
RedisReply Reply;
};
class xRedisClusterClient {
public:
xRedisClusterClient();
~xRedisClusterClient();
public:
bool connect(const std::string& host, uint32_t port,
const std::string& pass, uint32_t poolsize);
void keepalive();
bool commandArgv(const VString& vDataIn, RedisResult& result);
bool command(RedisResult& result, const char* format, ...);
void setLogLevel(uint32_t level, void (*emit)(int level, const char* line));
private:
xRedisClusterManager* mClusterManager_free;
xRedisClusterManager* mClusterManager;
ClusterInfo* mRedisInfo;
};
} // namespace xrc
#endif