A)編譯安裝
make make install (/usr/local) make install PREFIX=$HOME/progs(可以自由指定安裝路徑)
B)同步的API接口
redisContext *redisConnect(const char *ip, int port); void *redisCommand(redisContext *c, const char *format, ...); void freeReplyObject(void *reply);
1)建立連接
redisContext *c = redisConnect("127.0.0.1", 6379); if (c != NULL && c->err) { printf("Error: %s\n", c->errstr); // handle error }
redisConnect函數用來創建一個叫redisContext的東西,它包含了連接相關的信息,它里面有個err字段,0表示正常,其他表示出錯了!通過errstr字段可以知曉錯誤信息。
2)執行命令
reply = redisCommand(context, "SET key value"); reply = redisCommand(context, "SET key %s", value); reply = redisCommand(context, "SET key %b", value, (size_t) valuelen); reply = redisCommand(context, "SET key:%s %s", myid, value);
redisCommand的調用格式類似printf函數,上面的第二條調用語句的作用在於輸入二進制格式的value內容,其后必須表明二進制的字節長度!
3)redisCommand函數返回一個東西叫redisReply,我們需要通過判斷它的type字段來知道返回了具體什么樣的內容:
REDIS_REPLY_STATUS 表示狀態,內容通過str字段查看,字符串長度是len字段
REDIS_REPLY_ERROR 表示出錯,查看出錯信息,如上的str,len字段
REDIS_REPLY_INTEGER 返回整數,從integer字段獲取值
REDIS_REPLY_NIL 沒有數據返回
REDIS_REPLY_STRING 返回字符串,查看str,len字段
REDIS_REPLY_ARRAY 返回一個數組,查看elements的值(數組個數),通過element[index]的方式訪問數組元素,每個數組元素是 一個redisReply對象的指針
4)另外有一個類似的函數,批量執行命令:
void *redisCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
5)redisReply使用完畢后,需要使用函數freeReplyObject進行釋放銷毀
void redisFree(redisContext *c)的作用是斷開連接,並釋放redisContext的內容
6)redisCommand的函數執行流程說明:
a.格式化redis command
b.格式化后的命令內容放入redisContext的輸出緩沖區
c.調用redisGetReply函數執行命令,得到結果
7)管道的使用方式:
a.填入需要執行的命令
void redisAppendCommand(redisContext *c, const char *format, ...); void redisAppendCommandArgv(redisContext *c, int argc, const char **argv, const size_t *argvlen);
b.獲取命令的輸出結果
int redisGetReply(redisContext *c, void **reply);
c.釋放輸出結果
void freeReplyObject(void *reply);
例子:
redisReply *reply = NULL; redisAppendCommand(context,"set key1 value"); redisAppendCommand(context,"get key2"); redisGetReply(context,&reply); // reply for set freeReplyObject(reply); redisGetReply(context,&reply); // reply for get freeReplyObject(reply);
訂閱模式:
reply = redisCommand(context,"SUBSCRIBE test"); freeReplyObject(reply); while(redisGetReply(context,&reply) == REDIS_OK) { // consume message freeReplyObject(reply); }
8)redisReply返回結果處理:
REDIS_OK 正常
REDIS_ERR_IO IO讀/寫出現異常,通過errno查看原因
REDIS_ERR_EOF 服務器關閉了鏈接,讀結束
REDIS_ERR_PROTOCOL 分析redis協議內容出錯
EDIS_ERR_OTHER 其他未知的錯誤
上述錯誤類型都可以通過redisReply的errstr字段查看簡短的描述
C)異步API(異步API的使用方式和同步API差不多,在這兒列出不同的函數吧)
1.連接redis服務器
redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379); if (c->err) { printf("Error: %s\n", c->errstr); // handle error }
2.設置連接、斷開的鈎子函數
int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn); int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn);
3.插入命令信息
int redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata,const char *format, ...); int redisAsyncCommandArgv( redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen);
獲取命令輸出和同步API相同
4.關閉連接
void redisAsyncDisconnect(redisAsyncContext *ac);
D)輔助API
下面的API主要用於其他編程語言綁定的術后,可以讀取分析數據
redisReader *redisReaderCreate(void); void redisReaderFree(redisReader *reader); int redisReaderFeed(redisReader *reader, const char *buf, size_t len); int redisReaderGetReply(redisReader *reader, void **reply);
官方例子:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #include <hiredis.h> 6 7 int main(int argc, char **argv) { 8 unsigned int j; 9 redisContext *c; 10 redisReply *reply; 11 const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1"; 12 int port = (argc > 2) ? atoi(argv[2]) : 6379; 13 14 struct timeval timeout = { 1, 500000 }; // 1.5 seconds 15 c = redisConnectWithTimeout(hostname, port, timeout); 16 if (c == NULL || c->err) { 17 if (c) { 18 printf("Connection error: %s\n", c->errstr); 19 redisFree(c); 20 } else { 21 printf("Connection error: can't allocate redis context\n"); 22 } 23 exit(1); 24 } 25 26 /* PING server */ 27 reply = redisCommand(c,"PING"); 28 printf("PING: %s\n", reply->str); 29 freeReplyObject(reply); 30 31 /* Set a key */ 32 reply = redisCommand(c,"SET %s %s", "foo", "hello world"); 33 printf("SET: %s\n", reply->str); 34 freeReplyObject(reply); 35 36 /* Set a key using binary safe API */ 37 reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5); 38 printf("SET (binary API): %s\n", reply->str); 39 freeReplyObject(reply); 40 41 /* Try a GET and two INCR */ 42 reply = redisCommand(c,"GET foo"); 43 printf("GET foo: %s\n", reply->str); 44 freeReplyObject(reply); 45 46 reply = redisCommand(c,"INCR counter"); 47 printf("INCR counter: %lld\n", reply->integer); 48 freeReplyObject(reply); 49 /* again ... */ 50 reply = redisCommand(c,"INCR counter"); 51 printf("INCR counter: %lld\n", reply->integer); 52 freeReplyObject(reply); 53 54 /* Create a list of numbers, from 0 to 9 */ 55 reply = redisCommand(c,"DEL mylist"); 56 freeReplyObject(reply); 57 for (j = 0; j < 10; j++) { 58 char buf[64]; 59 60 snprintf(buf,64,"%d",j); 61 reply = redisCommand(c,"LPUSH mylist element-%s", buf); 62 freeReplyObject(reply); 63 } 64 65 /* Let's check what we have inside the list */ 66 reply = redisCommand(c,"LRANGE mylist 0 -1"); 67 if (reply->type == REDIS_REPLY_ARRAY) { 68 for (j = 0; j < reply->elements; j++) { 69 printf("%u) %s\n", j, reply->element[j]->str); 70 } 71 } 72 freeReplyObject(reply); 73 74 /* Disconnects and frees the context */ 75 redisFree(c); 76 77 return 0; 78 }
整理自:http://www.mamicode.com/info-detail-501902.html