redis举例调用两种方式方式

在以下的代码演示样例中。将给出两种最为经常使用的Redis命令操作方式,既普通调用方式和基于管线的调用方式。


    注:在阅读代码时请留意凝视。

  1 #include <stdio.h>
  2#include <stdlib.h>
  3#include <stddef.h>
  4#include <stdarg.h>
  5#include <string.h>
  6#include <assert.h>
  7#include <hiredis.h>
  8
  9void doTest()
10 {
11int timeout = 10000;
12struct timeval tv;
13     tv.tv_sec = timeout / 1000;
14     tv.tv_usec = timeout * 1000;
15//
以带有超时的方式链接Redisserver。同一时候获取与Redis连接的上下文对象。


16//该对象将用于其后全部与Redis操作的函数。
17     redisContext* c = redisConnectWithTimeout("192.168.149.137",6379,tv);
18if (c->err) {
19         redisFree(c);
20return;
21     }
22constchar* command1 = "setstest1 value1";
23     redisReply* r =(redisReply*)redisCommand(c,command1);
24//
须要注意的是。假设返回的对象是NULL。则表示client和server之间出现严重错误,必须又一次链接。


25//这里仅仅是举例说明,简便起见,后面的命令就不再做这种推断了。
26if (NULL == r) {
27         redisFree(c);
28return;
29     }
30//
不同的Redis命令返回的数据类型不同,在获取之前须要先推断它的实际类型。
31    //至于各种命令的返回值信息,能够參考Redis的官方文档,或者查看该系列博客的前几篇
32    //有关Redis各种数据类型的博客。
33    //字符串类型的set命令的返回值的类型是REDIS_REPLY_STATUS,然后仅仅有当返回信息是"OK"
34//时,才表示该命令运行成功。后面的样例以此类推,就不再过多赘述了。
35if (!(r->type == REDIS_REPLY_STATUS &&strcasecmp(r->str,"OK") == 0)) {
36         printf("Failedto execute command[%s]. ",command1);
37         freeReplyObject(r);
38         redisFree(c);
39return;
40     }
41//
因为后面反复使用该变量。所以须要提前释放,否则内存泄漏。
42     freeReplyObject(r);
43     printf("Succeedto execute command[%s]. ",command1);
44
45constchar* command2 = "strlenstest1";
46     r = (redisReply*)redisCommand(c,command2);
47if (r->type != REDIS_REPLY_INTEGER) {
48         printf("Failedto execute command[%s]. ",command2);
49         freeReplyObject(r);
50         redisFree(c);
51return;
52     }
53int length = r->integer;
54     freeReplyObject(r);
55     printf("Thelength of 'stest1' is %d. ",length);
56     printf("Succeedto execute command[%s]. ",command2);
57
58constchar* command3 = "getstest1";
59     r = (redisReply*)redisCommand(c,command3);
60if (r->type != REDIS_REPLY_STRING) {
61         printf("Failedto execute command[%s]. ",command3);
62         freeReplyObject(r);
63         redisFree(c);
64return;
65     }
66     printf("Thevalue of 'stest1' is %s. ",r->str);
67     freeReplyObject(r);
68     printf("Succeedto execute command[%s]. ",command3);
69
70constchar* command4 = "getstest2";
71     r = (redisReply*)redisCommand(c,command4);
72//
这里须要先说明一下,因为stest2键并不存在。因此Redis会返回空结果,这里仅仅是为了演示。
73if (r->type != REDIS_REPLY_NIL) {
74         printf("Failedto execute command[%s]. ",command4);
75         freeReplyObject(r);
76         redisFree(c);
77return;
78     }
79     freeReplyObject(r);
80     printf("Succeedto execute command[%s]. ",command4);
81
82constchar* command5 = "mgetstest1 stest2";
83     r = (redisReply*)redisCommand(c,command5);
84//
不论stest2存在与否,Redis都会给出结果。仅仅是第二个值为nil。
85    //由于有多个值返回,由于返回应答的类型是数组类型。
86if (r->type != REDIS_REPLY_ARRAY) {
87         printf("Failedto execute command[%s]. ",command5);
88         freeReplyObject(r);
89         redisFree(c);
90//r->elements
表示子元素的数量,无论请求的key是否存在,该值都等于请求是键的数量。


91         assert(2== r->elements);
92return;
93     }
94for (int i = 0; i < r->elements; ++i) {
95         redisReply* childReply =r->element;
96//
之前已经介绍过。get命令返回的数据类型是string。
97        //对于不存在key的返回值。其类型为REDIS_REPLY_NIL。
98if (childReply->type == REDIS_REPLY_STRING)
99             printf("Thevalue is %s. ",childReply->str);
100     }
101//
对于每个子应答。无需使用者单独释放,仅仅需释放最外部的redisReply就可以。
102    freeReplyObject(r);
103    printf("Succeed to executecommand[%s]. ",command5);
104
105    printf("Begin to test pipeline. ");
106//
该命令仅仅是将待发送的命令写入到上下文对象的输出缓冲区中。直到调用后面的
107   //redisGetReply命令才会批量将缓冲区中的命令写出到Redisserver。这样能够
108//有效的降低client与server之间的同步等候时间。以及网络IO引起的延迟。


109    //至于管线的详细性能优势。能够考虑该系列博客中的管线主题。
110if (REDIS_OK!= redisAppendCommand(c,command1)
111        || REDIS_OK != redisAppendCommand(c,command2)
112        || REDIS_OK != redisAppendCommand(c,command3)
113        || REDIS_OK != redisAppendCommand(c,command4)
114        || REDIS_OK != redisAppendCommand(c,command5)) {
115        redisFree(c);
116return;
117     }
118
119     redisReply* reply = NULL;
120//
对pipeline返回结果的处理方式,和前面代码的处理方式全然一直,这里就不再反复给出了。


121if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
122        printf("Failed to execute command[%s]with Pipeline. ",command1);
123        freeReplyObject(reply);
124         redisFree(c);
125     }
126    freeReplyObject(reply);
127    printf("Succeed to execute command[%s]with Pipeline. ",command1);
128
129if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
130        printf("Failed to execute command[%s]with Pipeline. ",command2);
131        freeReplyObject(reply);
132        redisFree(c);
133     }
134    freeReplyObject(reply);
135    printf("Succeed to execute command[%s]with Pipeline. ",command2);
136
137if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
138        printf("Failed to execute command[%s]with Pipeline. ",command3);
139        freeReplyObject(reply);
140        redisFree(c);
141     }
142    freeReplyObject(reply);
143    printf("Succeed to execute command[%s]with Pipeline. ",command3);
144
145if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
146        printf("Failed to execute command[%s]with Pipeline. ",command4);
147        freeReplyObject(reply);
148        redisFree(c);
149     }
150    freeReplyObject(reply);
151     printf("Succeedto execute command[%s] with Pipeline. ",command4);
152
153if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
154        printf("Failed to execute command[%s]with Pipeline. ",command5);
155        freeReplyObject(reply);
156        redisFree(c);
157     }
158    freeReplyObject(reply);
159    printf("Succeed to execute command[%s]with Pipeline. ",command5);
160//
因为全部通过pipeline提交的命令结果均已为返回,假设此时继续调用redisGetReply。
161    //将会导致该函数堵塞并挂起当前线程,直到有新的通过管线提交的命令结果返回。
162    //最后不要忘记在退出前释放当前连接的上下文对象。
163     redisFree(c);
164return;
165 }
166
167int main() 
168 {
169    doTest();
170return0;
171 }
172
173//
输出结果例如以下:
174//Succeed to execute command[set stest1value1].
175//The length of 'stest1' is 6.
176//Succeed to execute command[strlen stest1].
177//The value of 'stest1' is value1.
178//Succeed to execute command[get stest1].
179//Succeed to execute command[get stest2].
180//The value is value1.
181//Succeed to execute command[mget stest1 stest2].
182//Begin to test pipeline.
183//Succeed to execute command[set stest1 value1] withPipeline.
184//Succeed to execute command[strlen stest1] withPipeline.
185//Succeed to execute command[get stest1] with Pipeline.
186//Succeed to execute command[get stest2] with Pipeline.
187//Succeed to execute command[mget stest1 stest2] withPipeline.


很多其它精彩内容请关注:http://bbs.superwu.cn
关注超人学院微信二维码:
关注超人学院java免费学习交流群:

版权声明:本文博主原创文章。博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/gcczhongduan/p/4916920.html