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//
以帶有超時的方式鏈接Redis服務器,同時獲取與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,則表示客戶端和服務器之間出現嚴重錯誤,必須重新鏈接。
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].\n",command1);
37         freeReplyObject(r);
38         redisFree(c);
39return;
40     }
41//
由於後面重複使用該變量,所以需要提前釋放,否則內存泄漏。
42     freeReplyObject(r);
43     printf("Succeedto execute command[%s].\n",command1);
44
45constchar* command2 = "strlenstest1";
46     r = (redisReply*)redisCommand(c,command2);
47if (r->type != REDIS_REPLY_INTEGER) {
48         printf("Failedto execute command[%s].\n",command2);
49         freeReplyObject(r);
50         redisFree(c);
51return;
52     }
53int length = r->integer;
54     freeReplyObject(r);
55     printf("Thelength of 'stest1' is %d.\n",length);
56     printf("Succeedto execute command[%s].\n",command2);
57
58constchar* command3 = "getstest1";
59     r = (redisReply*)redisCommand(c,command3);
60if (r->type != REDIS_REPLY_STRING) {
61         printf("Failedto execute command[%s].\n",command3);
62         freeReplyObject(r);
63         redisFree(c);
64return;
65     }
66     printf("Thevalue of 'stest1' is %s.\n",r->str);
67     freeReplyObject(r);
68     printf("Succeedto execute command[%s].\n",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].\n",command4);
75         freeReplyObject(r);
76         redisFree(c);
77return;
78     }
79     freeReplyObject(r);
80     printf("Succeedto execute command[%s].\n",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].\n",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.\n",childReply->str);
100     }
101//
對於每一個子應答,無需使用者單獨釋放,只需釋放最外部的redisReply即可。
102    freeReplyObject(r);
103    printf("Succeed to executecommand[%s].\n",command5);
104
105    printf("Begin to test pipeline.\n");
106//
該命令只是將待發送的命令寫入到上下文對象的輸出緩衝區中,直到調用後面的
107   //redisGetReply命令纔會批量將緩衝區中的命令寫出到Redis服務器。這樣可以
108//有效的減少客戶端與服務器之間的同步等候時間,以及網絡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.\n",command1);
123        freeReplyObject(reply);
124         redisFree(c);
125     }
126    freeReplyObject(reply);
127    printf("Succeed to execute command[%s]with Pipeline.\n",command1);
128
129if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
130        printf("Failed to execute command[%s]with Pipeline.\n",command2);
131        freeReplyObject(reply);
132        redisFree(c);
133     }
134    freeReplyObject(reply);
135    printf("Succeed to execute command[%s]with Pipeline.\n",command2);
136
137if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
138        printf("Failed to execute command[%s]with Pipeline.\n",command3);
139        freeReplyObject(reply);
140        redisFree(c);
141     }
142    freeReplyObject(reply);
143    printf("Succeed to execute command[%s]with Pipeline.\n",command3);
144
145if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
146        printf("Failed to execute command[%s]with Pipeline.\n",command4);
147        freeReplyObject(reply);
148        redisFree(c);
149     }
150    freeReplyObject(reply);
151     printf("Succeedto execute command[%s] with Pipeline.\n",command4);
152
153if (REDIS_OK!= redisGetReply(c,(void**)&reply)) {
154        printf("Failed to execute command[%s]with Pipeline.\n",command5);
155        freeReplyObject(reply);
156        redisFree(c);
157     }
158    freeReplyObject(reply);
159    printf("Succeed to execute command[%s]with Pipeline.\n",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免費學習交流羣:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章