hiredis從安裝到項(xiàng)目實(shí)戰(zhàn)操作
Hiredis是一個(gè)Redis的C客戶端庫函數(shù),基本實(shí)現(xiàn)了Redis的協(xié)議的最小集。
花個(gè)兩分鐘跟我一起配置hiredis
當(dāng)我們下載了最新版redis的時(shí)候,其實(shí)就已經(jīng)自帶了C++版本的操作庫,只不過有些人沒發(fā)現(xiàn)罷了。
進(jìn)入到deps->hiredis目錄下(在你的redis解壓目錄下有deps)
然后:make install
一步到位。
其實(shí)連測(cè)試函數(shù)他們都給你準(zhǔn)備好了,在hedis文件夾中還有個(gè)文件夾,example,里面有個(gè)example.c文件。
這樣編譯,如果不會(huì)的話:首先需要把里面的頭文件改一下:#include<hiredis/hiredis.h>
編譯的時(shí)候記得帶上依賴項(xiàng):
gcc example.c -o example -L/usr/local/lib -lhiredis
當(dāng)你運(yùn)行的時(shí)候,(別給我說你不會(huì)運(yùn)行:./example)如果不出意外,會(huì)跟你說依賴項(xiàng)找不著。
正常,教你一個(gè)治標(biāo)的辦法:
在/etc/ld.so.conf.d/目錄下新建文件usr-libs.conf,內(nèi)容是:/usr/local/lib
然后使用命令/sbin/ldconfig更新一下配置即可。
這東西配置完,你虛擬機(jī)重啟之后就沒了,永久配置好像在我的另一篇博客里有,動(dòng)態(tài)庫專欄下。
最后的運(yùn)行效果:
redis的C/C++ API
redisContext* redisConnect(const char *ip, int port);
參數(shù)釋義:
該函數(shù)用來連接redis數(shù)據(jù)庫, 兩個(gè)參數(shù)分別是redis數(shù)據(jù)庫的ip和端口,端口號(hào)一般為6379。
void *redisCommand(redisContext *c, const char *format...);
該函數(shù)用于執(zhí)行redis數(shù)據(jù)庫中的命令,第一個(gè)參數(shù)為連接數(shù)據(jù)庫返回的redisContext,剩下的參數(shù)為變參.。
此函數(shù)的返回值為void*,但是一般會(huì)強(qiáng)制轉(zhuǎn)換為redisReply類型,以便做進(jìn)一步的處理。
void freeReplyObject(void *reply);
釋放redisCommand執(zhí)行后返回的的redisReply所占用的內(nèi)存。
void redisFree(redisContext *c)
釋放redisConnect()所產(chǎn)生的連接。
實(shí)操代碼示例
#include <stdio.h> #include <stdlib.h> #include <string.h> #include<hiredis/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); //該函數(shù)用來連接redis數(shù)據(jù)庫, 兩個(gè)參數(shù)分別是redis數(shù)據(jù)庫的ip和端口,端口號(hào)一般為6379。 } else { c = redisConnectWithTimeout(hostname, port, timeout); } if (c == NULL || c->err) { if (c) { printf("Connection error: %s\n", c->errstr); redisFree(c); //釋放redisConnect()所產(chǎn)生的連接。 } else { printf("Connection error: can't allocate redis context\n"); } exit(1); } /* PING server */ reply = redisCommand(c,"PING"); //該函數(shù)用于執(zhí)行redis數(shù)據(jù)庫中的命令,第一個(gè)參數(shù)為連接數(shù)據(jù)庫返回的redisContext,剩下的參數(shù)為變參.。 //此函數(shù)的返回值為void*,但是一般會(huì)強(qiáng)制轉(zhuǎn)換為redisReply類型,以便做進(jìn)一步的處理。 printf("PING: %s\n", reply->str); freeReplyObject(reply); //釋放redisCommand執(zhí)行后返回的的redisReply所占用的內(nèi)存。 /* 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; }
到此這篇關(guān)于hiredis從安裝到項(xiàng)目實(shí)戰(zhàn)操作的文章就介紹到這了,更多相關(guān)hiredis安裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis高階使用消息隊(duì)列分布式鎖排行榜等(高階用法)
在大多數(shù)傳統(tǒng)的web系統(tǒng)中,使用Redis一般都是作為緩存使用,在大數(shù)據(jù)查詢時(shí)作為緩解性能的一種解決方案,這篇文章主要介紹了Redis高階使用消息隊(duì)列分布式鎖排行榜等,需要的朋友可以參考下2024-03-03Redis中秒殺場(chǎng)景下超時(shí)與超賣問題的解決方案
當(dāng)我們?cè)趌inux中使用ab來模擬高并發(fā)秒殺時(shí)可能會(huì)遇到兩種問題,“超時(shí)和超賣”,本文就詳細(xì)介紹了Redis中秒殺場(chǎng)景下超時(shí)與超賣問題的解決方案,感興趣的可以了解一下2022-05-05redis中的數(shù)據(jù)結(jié)構(gòu)和編碼詳解
本文主要和大家分享幾種Redis數(shù)據(jù)結(jié)構(gòu)詳解,希望文中的案例和代碼,能幫助到大家。2020-03-03詳解redis在服務(wù)器linux下啟動(dòng)的相關(guān)命令(安裝和配置)
這篇文章主要介紹了redis在服務(wù)器linux下的啟動(dòng)的相關(guān)命令(安裝和配置),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08Redis對(duì)象與redisObject超詳細(xì)分析源碼層
這篇文章主要介紹了Redis對(duì)象與redisObject源碼層的分析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11