Hiredis是一個Redis的C客戶端庫函數,基本實現了Redis的協議的最小集。
花個兩分鐘跟我一起配置hiredis
當我們下載了最新版redis的時候,其實就已經自帶了C++版本的操作庫,只不過有些人沒發現罷了。
進入到deps->hiredis目錄下(在你的redis解壓目錄下有deps)
然后:make install
一步到位。
其實連測試函數他們都給你準備好了,在hedis文件夾中還有個文件夾,example,里面有個example.c文件。
這樣編譯,如果不會的話:首先需要把里面的頭文件改一下:#includehiredis/hiredis.h>
編譯的時候記得帶上依賴項:
gcc example.c -o example -L/usr/local/lib -lhiredis
當你運行的時候,(別給我說你不會運行:./example)如果不出意外,會跟你說依賴項找不著。
正常,教你一個治標的辦法:
在/etc/ld.so.conf.d/目錄下新建文件usr-libs.conf,內容是:/usr/local/lib
然后使用命令/sbin/ldconfig更新一下配置即可。
這東西配置完,你虛擬機重啟之后就沒了,永久配置好像在我的另一篇博客里有,動態庫專欄下。
最后的運行效果:

redis的C/C++ API
redisContext* redisConnect(const char *ip, int port);
參數釋義:
該函數用來連接redis數據庫, 兩個參數分別是redis數據庫的ip和端口,端口號一般為6379。
void *redisCommand(redisContext *c, const char *format...);
該函數用于執行redis數據庫中的命令,第一個參數為連接數據庫返回的redisContext,剩下的參數為變參.。
此函數的返回值為void*,但是一般會強制轉換為redisReply類型,以便做進一步的處理。
void freeReplyObject(void *reply);
釋放redisCommand執行后返回的的redisReply所占用的內存。
void redisFree(redisContext *c)
釋放redisConnect()所產生的連接。
實操代碼示例
#include stdio.h>
#include stdlib.h>
#include string.h>
#includehiredis/hiredis.h>
int main(int argc, char **argv) {
unsigned int j, isunix = 0;
redisContext *c;
redisReply *reply; :
const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";
if (argc > 2) {
if (*argv[2] == 'u' || *argv[2] == 'U') {
isunix = 1;
/* in this case, host is the path to the unix socket */
printf("Will connect to unix socket @%s\n", hostname);
}
}
int port = (argc > 2) ? atoi(argv[2]) : 6379;
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
if (isunix) {
c = redisConnectUnixWithTimeout(hostname, timeout);
//該函數用來連接redis數據庫, 兩個參數分別是redis數據庫的ip和端口,端口號一般為6379。
} else {
c = redisConnectWithTimeout(hostname, port, timeout);
}
if (c == NULL || c->err) {
if (c) {
printf("Connection error: %s\n", c->errstr);
redisFree(c); //釋放redisConnect()所產生的連接。
} else {
printf("Connection error: can't allocate redis context\n");
}
exit(1);
}
/* PING server */
reply = redisCommand(c,"PING");
//該函數用于執行redis數據庫中的命令,第一個參數為連接數據庫返回的redisContext,剩下的參數為變參.。
//此函數的返回值為void*,但是一般會強制轉換為redisReply類型,以便做進一步的處理。
printf("PING: %s\n", reply->str);
freeReplyObject(reply); //釋放redisCommand執行后返回的的redisReply所占用的內存。
/* Set a key */
reply = redisCommand(c,"SET %s %s", "foo", "hello world");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);
/* Set a key using binary safe API */
reply = redisCommand(c,"SET %b %b", "bar", (size_t) 3, "hello", (size_t) 5);
printf("SET (binary API): %s\n", reply->str);
freeReplyObject(reply);
/* Try a GET and two INCR */
reply = redisCommand(c,"GET foo");
printf("GET foo: %s\n", reply->str);
freeReplyObject(reply);
reply = redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* again ... */
reply = redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* Create a list of numbers, from 0 to 9 */
reply = redisCommand(c,"DEL mylist");
freeReplyObject(reply);
for (j = 0; j 10; j++) {
char buf[64];
snprintf(buf,64,"%u",j);
reply = redisCommand(c,"LPUSH mylist element-%s", buf);
freeReplyObject(reply);
}
/* Let's check what we have inside the list */
reply = redisCommand(c,"LRANGE mylist 0 -1");
if (reply->type == REDIS_REPLY_ARRAY) {
for (j = 0; j reply->elements; j++) {
printf("%u) %s\n", j, reply->element[j]->str);
}
}
freeReplyObject(reply);
/* Disconnects and frees the context */
redisFree(c);
return 0;
}
到此這篇關于hiredis從安裝到項目實戰操作的文章就介紹到這了,更多相關hiredis安裝內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!