c++中使用 hiredis/hiredis.h
生活随笔
收集整理的這篇文章主要介紹了
c++中使用 hiredis/hiredis.h
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
hiredis是redis開(kāi)源庫(kù)對(duì)外發(fā)布的客戶(hù)端API包。
當(dāng)redis-server配置啟動(dòng)后,可以通過(guò)hiredis操作redis資源。
幾個(gè)基本的函數(shù)就可以操作redis數(shù)據(jù)庫(kù)
/* 作用:用于連接redis服務(wù)器ip : 為redis的ip地址;port: 端口地址;tv:連接超時(shí)的參數(shù); */redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv); 該函數(shù)用來(lái) 連接redis數(shù)據(jù)庫(kù) ,參數(shù)為數(shù)據(jù)庫(kù)的ip地址和端口,一般redis數(shù)據(jù)庫(kù)的端口為6379
struct timeval結(jié)構(gòu)體在time.h中的定義為:
/* 作用:執(zhí)行命令 c:redisConnectWitTimeout返回的對(duì)象; format:命令參數(shù); 一般強(qiáng)制轉(zhuǎn)換成為redisReply類(lèi)型,以便做進(jìn)行進(jìn)一步的處理 */void *redisCommand(redisContext *c, const char *format, ...)
例如:
redisReply *reply = NULL;
reply = (redisReply *)redisCommand(m_c,"GET %s", key);
/*說(shuō)明:redisCommand返回的對(duì)象指針,也就是已經(jīng)命令返回的結(jié)果數(shù)據(jù) */typedef struct redisReply { int type; /* REDIS_REPLY_* */long long integer; /* The integer when type is REDIS_REPLY_INTEGER */int len; /* Length of string */char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */} redisReply;
void freeReplyObject(void *reply);
? ? ? ? 說(shuō)明:釋放redisCommand執(zhí)行后返回的redisReply所占用的內(nèi)存
void redisFree(redisContext *c);
? ? ? ? 說(shuō)明:釋放redisConnect()所產(chǎn)生的連接。
總結(jié)
以上是生活随笔為你收集整理的c++中使用 hiredis/hiredis.h的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: dlopen dlsym dlclose
- 下一篇: VirtualBox虚拟机网络环境解析和