hiredis是redis官方推荐的C/C++客户端代码库。使用hiredis库很简易方便的进行redis的相关开发。
同步方式
不过大多数情况下,我们采用的都是同步的调用方式。
1
2
3
4
5
6
7
8
9
10
11
|
/* 和redis服务器建立TCP连接 */
redisContext *redisConnectWithTimeout(const char *ip, int port, const struct timeval tv);
/* 发送指令到redis服务器,并取得结果 */
void *redisCommand(redisContext *c, const char *format, ...);
/* 处理完结果后,释放内存 */
void freeReplyObject(void *reply);
/* 释放和redis服务器的连接 */
void redisFree(redisContext *c);
|
没错,同步的方式就是这么简单易用。
异步方式
不过说到了同步,那必然得说异步。同步简单,异步性能高。为了性能,或者说为了装B,必须得用一下异步调用方式才能显示得出高大上。
当然,本文不是描述基于libevent,或者基于redis本身得ae网络库的异步方式。而是基于公司(或者自己)已有的异步服务器框架上,我们需要:
- 将发送redis的请求按照redis的既有协议打包到一个buffer。
- 将这个buffer丢给异步服务器框架,由框架负责和redis服务器建立(维护)连接,发送请求buffer,并取得redis返回的数据buffer。框架保证这个过程是异步高性能的。
- 然后框架会给出redis的回包buffer。我们需要按照redis的既有协议解开这个包,并得到结果。
redis的既有协议,这里先不展开描述。回到上面的需求,我们怎么才能打包redis的请求,解包redis的返回数据呢?用习惯了同步的方式,还曾经抱怨hiredis为什么不提供一个打包的函数和一个解包的函数呢。原来是自己眼拙,没看出hiredis库的打包和解包的使用方式。
打包函数系列:
1
2
3
4
|
/* Functions to format a command according to the protocol. */
int redisvFormatCommand(char **target, const char *format, va_list ap);
int redisFormatCommand(char **target, const char *format, ...);
int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
|
解包是通过一个redisReader来完成的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/* State for the protocol parser */
typedef struct redisReader {
int err; /* Error flags, 0 when there is no error */
char errstr[128]; /* String representation of error when applicable */
char *buf; /* Read buffer */
size_t pos; /* Buffer cursor */
size_t len; /* Buffer length */
size_t maxbuf; /* Max length of unused buffer */
redisReadTask rstack[9];
int ridx; /* Index of current read task */
void *reply; /* Temporary reply pointer */
redisReplyObjectFunctions *fn;
void *privdata;
} redisReader;
/* Public API for the protocol parser. */
redisReader *redisReaderCreate(void);
void redisReaderFree(redisReader *r);
int redisReaderFeed(redisReader *r, const char *buf, size_t len);
int redisReaderGetReply(redisReader *r, void **reply);
|
呵呵,打包和解包看来不是一个简单的函数搞定的,就原谅自己的眼拙了。哈哈。
实战演习
来一个简单的例子(demo.c),假设实例里面的函数
int asyncFrameWork(const uint8_t* sendBuf, int sendBufLen, uint8_t* recvBuf, int* recvBufLen);
就是公司高大上的异步服务器框架了啊。(汗颜一下。。。)
或者把这个asyncFrameWork替换为目前很前沿的微线程(协程)技术,那性能也是相当高的。
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#include "hiredis.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdint.h>
int asyncFrameWork(const uint8_t* sendBuf, int sendBufLen, uint8_t* recvBuf, int* recvBufLen)
{
if (sendBuf == NULL || recvBuf == NULL || sendBufLen < 1 || *recvBufLen < 1) {
printf("asyncFrameWork param error.\n");
return -1;
}
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
printf("socket error : %d\n", errno);
return -1;
}
struct sockaddr_in addr;
memset(&addr, 0x00, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(6379);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
int ret = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
if (ret) {
printf("connect error : %d\n", errno);
close(fd);
return -2;
}
ret = send(fd, sendBuf, sendBufLen, 0);
if (ret < 0) {
printf("send error : %d\n", errno);
close(fd);
return -3;
}
ret = recv(fd, recvBuf, *recvBufLen, 0);
if (ret <= 0) {
printf("recv error : %d\n", errno);
close(fd);
return -4;
}
*recvBufLen = ret;
printf("asyncFrameWork recv len = %d\n", ret);
close(fd);
return 0;
}
int setCmd(const char* key, const char* value)
{
char* cmdBuf = NULL;
int len = 0;
len = redisFormatCommand(&cmdBuf, "SET %s %s", key, value);
if (len < 0) {
printf("redisFormatCommand error! len = %d\n", len);
return -1;
}
printf("redisFormatCommand result: len = %d, cmdBuf = %s\n", len, cmdBuf);
int ret = 0;
char recvBuf[1024] = {0};
int recvBufLen = sizeof(recvBuf);
ret = asyncFrameWork((const uint8_t*)cmdBuf, len, (uint8_t*)recvBuf, &recvBufLen);
if (ret) {
printf("asyncFrameWork error:%d!\n", ret);
free(cmdBuf);
cmdBuf = NULL;
return -2;
}
printf("asyncFrameWork return : recvBuf = %s, recvBufLen = %d\n", recvBuf, recvBufLen);
free(cmdBuf);
cmdBuf = NULL;
redisReader *reader = NULL;
void *reply = NULL;
reader = redisReaderCreate();
if (reader == NULL) {
printf("redisReaderCreate error!\n");
return -3;
}
redisReaderFeed(reader, recvBuf, recvBufLen);
ret = redisReaderGetReply(reader, &reply);
if (ret != REDIS_OK) {
printf("redisReaderGetReply error:%d!\n", ret);
redisReaderFree(reader);
reader = NULL;
return -4;
}
if (reply == NULL) {
printf("redisReply is NULL!\n");
redisReaderFree(reader);
reader = NULL;
return -5;
}
printf("redisReader get reply : %d\n", *((int*)reply));
struct redisReply *r = (struct redisReply*)reply;
printf("r->type = %d\n", r->type);
if (REDIS_REPLY_ERROR == r->type) {
printf("redisReply return REDIS_REPLY_ERROR!\n");
freeReplyObject(reply);
redisReaderFree(reader);
reader = NULL;
return -6;
}
if (REDIS_REPLY_STATUS != r->type) {
printf("redisReply return is't REDIS_REPLY_STATUS!\n");
freeReplyObject(reply);
redisReaderFree(reader);
reader = NULL;
return -7;
}
printf("-------------------------------------\n");
if (r->integer == REDIS_OK)
printf("set key(%s) OK\n", key);
else
printf("set key(%s) FAIL\n", key);
printf("-------------------------------------\n");
freeReplyObject(reply);
redisReaderFree(reader);
return 0;
}
int getCmd(const char* key)
{
char* cmdBuf = NULL;
int len = 0;
len = redisFormatCommand(&cmdBuf, "GET %s", key);
if (len < 0) {
printf("redisFormatCommand error! len = %d\n", len);
return -1;
}
printf("redisFormatCommand result: len = %d, cmdBuf = %s\n", len, cmdBuf);
int ret = 0;
char recvBuf[1024] = {0};
int recvBufLen = sizeof(recvBuf);
ret = asyncFrameWork((const uint8_t*)cmdBuf, len, (uint8_t*)recvBuf, &recvBufLen);
if (ret) {
printf("asyncFrameWork error:%d!\n", ret);
free(cmdBuf);
cmdBuf = NULL;
return -2;
}
printf("asyncFrameWork return : recvBuf = %s, recvBufLen = %d\n", recvBuf, recvBufLen);
free(cmdBuf);
cmdBuf = NULL;
redisReader *reader = NULL;
void *reply = NULL;
reader = redisReaderCreate();
if (reader == NULL) {
printf("redisReaderCreate error!\n");
return -3;
}
redisReaderFeed(reader, recvBuf, recvBufLen);
ret = redisReaderGetReply(reader, &reply);
if (ret != REDIS_OK) {
printf("redisReaderGetReply error:%d!\n", ret);
redisReaderFree(reader);
reader = NULL;
return -4;
}
if (reply == NULL) {
printf("redisReply is NULL!\n");
redisReaderFree(reader);
reader = NULL;
return -5;
}
printf("redisReader get reply : %d\n", *((int*)reply));
struct redisReply *r = (struct redisReply*)reply;
printf("r->type = %d\n", r->type);
if (REDIS_REPLY_ERROR == r->type) {
printf("redisReply return REDIS_REPLY_ERROR!\n");
freeReplyObject(reply);
redisReaderFree(reader);
reader = NULL;
return -6;
}
if (REDIS_REPLY_STRING != r->type) {
if (REDIS_REPLY_NIL == r->type) {
printf("-------------------------------------\n");
printf("key(%s) not exists!\n", key);
printf("-------------------------------------\n");
}
else
printf("redisReply return is't REDIS_REPLY_STRING!\n");
freeReplyObject(reply);
redisReaderFree(reader);
reader = NULL;
return -7;
}
printf("r->len = %d\n", r->len);
printf("r->str = %s\n", r->str);
printf("-------------------------------------\n");
printf("key(%s), value=(%s)\n", key, r->str);
printf("-------------------------------------\n");
freeReplyObject(reply);
redisReaderFree(reader);
return 0;
}
int main(int argc, char** argv)
{
setCmd("company", "leoox");
getCmd("company..");
getCmd("company");
return 0;
}
|
编译
1
|
gcc -o demo -I/home/leoox/soft/hiredis-master demo.c /home/leoox/soft/hiredis-master/libhiredis.a
|
执行
用valgrind执行一下,看结果是否正确的同时,也顺便看看是否有内存泄露。
[amcool@leoox redisDemo]$ valgrind –leak-check=full –tool=memcheck ./demo
==3868== Memcheck, a memory error detector.
==3868== Copyright (C) 2002-2006, and GNU GPL’d, by Julian Seward et al.
==3868== Using LibVEX rev 1658, a library for dynamic binary translation.
==3868== Copyright (C) 2004-2006, and GNU GPL’d, by OpenWorks LLP.
==3868== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==3868== Copyright (C) 2000-2006, and GNU GPL’d, by Julian Seward et al.
==3868== For more details, rerun with: -v
==3868==
redisFormatCommand result: len = 37, cmdBuf = *3
$3
SET
$7
company
$5
leoox
asyncFrameWork recv len = 5
asyncFrameWork return : recvBuf = +OK
, recvBufLen = 5
redisReader get reply : 5
r->type = 5
————————————-
set key(company) OK
————————————-
redisFormatCommand result: len = 28, cmdBuf = *2
$3
GET
$9
company..
asyncFrameWork recv len = 5
asyncFrameWork return : recvBuf = $-1
, recvBufLen = 5
redisReader get reply : 4
r->type = 4
————————————-
key(company..) not exists!
————————————-
redisFormatCommand result: len = 26, cmdBuf = *2
$3
GET
$7
company
asyncFrameWork recv len = 11
asyncFrameWork return : recvBuf = $5
leoox
, recvBufLen = 11
redisReader get reply : 1
r->type = 1
r->len = 5
r->str = leoox
————————————-
key(company), value=(leoox)
————————————-
==3868==
==3868== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 5 from 1)
==3868== malloc/free: in use at exit: 0 bytes in 0 blocks.
==3868== malloc/free: 41 allocs, 41 frees, 2,352 bytes allocated.
==3868== For counts of detected errors, rerun with: -v
==3868== All heap blocks were freed — no leaks are possible.
结果正确,并且没有内存泄露。perfect!哦,圣诞过完了,那提前说元旦快乐,新的一年要任性!