Redis学习手册(实例代码)
生活随笔
收集整理的這篇文章主要介紹了
Redis学习手册(实例代码)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在之前的博客中已經(jīng)非常詳細(xì)的介紹了Redis的各種操作命令、運(yùn)行機(jī)制和服務(wù)器初始化參數(shù)配置。本篇博客是該系列博客中的最后一篇,在這里將給出基于Redis客戶端組件訪問并操作Redis服務(wù)器的代碼示例。然而需要說明的是,由于Redis官方并未提供基于C接口的Windows平臺客戶端,因此下面的示例僅可運(yùn)行于Linux/Unix平臺。但是對于使用其它編程語言的開發(fā)者而言,如C#和Java,Redis則提供了針對這些語言的客戶端組件,通過該方式,同樣可以達(dá)到基于Windows平臺與Redis服務(wù)器進(jìn)行各種交互的目的。
?? ?該篇博客中使用的客戶端來自于Redis官方網(wǎng)站,是Redis推薦的基于C接口的客戶端組件,見如下鏈接:
?? ?https://github.com/antirez/hiredis
?? ?在下面的代碼示例中,將給出兩種最為常用的Redis命令操作方式,既普通調(diào)用方式和基于管線的調(diào)用方式。
?? ?注:在閱讀代碼時請留意注釋。
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
9 void doTest()
10 {
11 int timeout = 10000;
12 struct timeval tv;
13 tv.tv_sec = timeout / 1000;
14 tv.tv_usec = timeout * 1000;
15 //以帶有超時的方式鏈接Redis服務(wù)器,同時獲取與Redis連接的上下文對象。
16 //該對象將用于其后所有與Redis操作的函數(shù)。
17 redisContext* c = redisConnectWithTimeout("192.168.149.137",6379,tv);
18 if (c->err) {
19 redisFree(c);
20 return;
21 }
22 const char* command1 = "set stest1 value1";
23 redisReply* r = (redisReply*)redisCommand(c,command1);
24 //需要注意的是,如果返回的對象是NULL,則表示客戶端和服務(wù)器之間出現(xiàn)嚴(yán)重錯誤,必須重新鏈接。
25 //這里只是舉例說明,簡便起見,后面的命令就不再做這樣的判斷了。
26 if (NULL == r) {
27 redisFree(c);
28 return;
29 }
30 //不同的Redis命令返回的數(shù)據(jù)類型不同,在獲取之前需要先判斷它的實(shí)際類型。
31 //至于各種命令的返回值信息,可以參考Redis的官方文檔,或者查看該系列博客的前幾篇
32 //有關(guān)Redis各種數(shù)據(jù)類型的博客。:)
33 //字符串類型的set命令的返回值的類型是REDIS_REPLY_STATUS,然后只有當(dāng)返回信息是"OK"
34 //時,才表示該命令執(zhí)行成功。后面的例子以此類推,就不再過多贅述了。
35 if (!(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK") == 0)) {
36 printf("Failed to execute command[%s].\n",command1);
37 freeReplyObject(r);
38 redisFree(c);
39 return;
40 }
41 //由于后面重復(fù)使用該變量,所以需要提前釋放,否則內(nèi)存泄漏。
42 freeReplyObject(r);
43 printf("Succeed to execute command[%s].\n",command1);
44
45 const char* command2 = "strlen stest1";
46 r = (redisReply*)redisCommand(c,command2);
47 if (r->type != REDIS_REPLY_INTEGER) {
48 printf("Failed to execute command[%s].\n",command2);
49 freeReplyObject(r);
50 redisFree(c);
51 return;
52 }
53 int length = r->integer;
54 freeReplyObject(r);
55 printf("The length of 'stest1' is %d.\n",length);
56 printf("Succeed to execute command[%s].\n",command2);
57
58 const char* command3 = "get stest1";
59 r = (redisReply*)redisCommand(c,command3);
60 if (r->type != REDIS_REPLY_STRING) {
61 printf("Failed to execute command[%s].\n",command3);
62 freeReplyObject(r);
63 redisFree(c);
64 return;
65 }
66 printf("The value of 'stest1' is %s.\n",r->str);
67 freeReplyObject(r);
68 printf("Succeed to execute command[%s].\n",command3);
69
70 const char* command4 = "get stest2";
71 r = (redisReply*)redisCommand(c,command4);
72 //這里需要先說明一下,由于stest2鍵并不存在,因此Redis會返回空結(jié)果,這里只是為了演示。
73 if (r->type != REDIS_REPLY_NIL) {
74 printf("Failed to execute command[%s].\n",command4);
75 freeReplyObject(r);
76 redisFree(c);
77 return;
78 }
79 freeReplyObject(r);
80 printf("Succeed to execute command[%s].\n",command4);
81
82 const char* command5 = "mget stest1 stest2";
83 r = (redisReply*)redisCommand(c,command5);
84 //不論stest2存在與否,Redis都會給出結(jié)果,只是第二個值為nil。
85 //由于有多個值返回,因?yàn)榉祷貞?yīng)答的類型是數(shù)組類型。
86 if (r->type != REDIS_REPLY_ARRAY) {
87 printf("Failed to execute command[%s].\n",command5);
88 freeReplyObject(r);
89 redisFree(c);
90 //r->elements表示子元素的數(shù)量,不管請求的key是否存在,該值都等于請求是鍵的數(shù)量。
91 assert(2 == r->elements);
92 return;
93 }
94 for (int i = 0; i < r->elements; ++i) {
95 redisReply* childReply = r->element[i];
96 //之前已經(jīng)介紹過,get命令返回的數(shù)據(jù)類型是string。
97 //對于不存在key的返回值,其類型為REDIS_REPLY_NIL。
98 if (childReply->type == REDIS_REPLY_STRING)
99 printf("The value is %s.\n",childReply->str);
100 }
101 //對于每一個子應(yīng)答,無需使用者單獨(dú)釋放,只需釋放最外部的redisReply即可。
102 freeReplyObject(r);
103 printf("Succeed to execute command[%s].\n",command5);
104
105 printf("Begin to test pipeline.\n");
106 //該命令只是將待發(fā)送的命令寫入到上下文對象的輸出緩沖區(qū)中,直到調(diào)用后面的
107 //redisGetReply命令才會批量將緩沖區(qū)中的命令寫出到Redis服務(wù)器。這樣可以
108 //有效的減少客戶端與服務(wù)器之間的同步等候時間,以及網(wǎng)絡(luò)IO引起的延遲。
109 //至于管線的具體性能優(yōu)勢,可以考慮該系列博客中的管線主題。
110 if (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);
116 return;
117 }
118
119 redisReply* reply = NULL;
120 //對pipeline返回結(jié)果的處理方式,和前面代碼的處理方式完全一直,這里就不再重復(fù)給出了。
121 if (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
129 if (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
137 if (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
145 if (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("Succeed to execute command[%s] with Pipeline.\n",command4);
152
153 if (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提交的命令結(jié)果均已為返回,如果此時繼續(xù)調(diào)用redisGetReply,
161 //將會導(dǎo)致該函數(shù)阻塞并掛起當(dāng)前線程,直到有新的通過管線提交的命令結(jié)果返回。
162 //最后不要忘記在退出前釋放當(dāng)前連接的上下文對象。
163 redisFree(c);
164 return;
165 }
166
167 int main()
168 {
169 doTest();
170 return 0;
171 }
172
173 //輸出結(jié)果如下:
174 //Succeed to execute command[set stest1 value1].
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] with Pipeline.
184 //Succeed to execute command[strlen stest1] with Pipeline.
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] with Pipeline.
該示例代碼已經(jīng)調(diào)試通過,同時也用Valgrind進(jìn)行了運(yùn)行時檢查,不存在任何內(nèi)存泄露,特此聲明。
總結(jié)
以上是生活随笔為你收集整理的Redis学习手册(实例代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软链接和硬链接的解读
- 下一篇: 判断一个页面中的URL是否是可以正确访问