您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在C++項(xiàng)目中使用redis,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
C++使用redis的實(shí)例詳解
hiredis是redis數(shù)據(jù)庫的C接口,目前只能在linux下使用,幾個(gè)基本的函數(shù)就可以操作redis數(shù)據(jù)庫了。
函數(shù)原型:redisContext *redisConnect(const char *ip, int port);
說明:該函數(shù)用來連接redis數(shù)據(jù)庫,參數(shù)為數(shù)據(jù)庫的ip地址和端口,一般redis數(shù)據(jù)庫的端口為6379;
函數(shù)返回值:該函數(shù)返回一個(gè)結(jié)構(gòu)體redisContext;
類似的提供了一個(gè)函數(shù)redisContext* redisConnectWithTimeout(const char *ip, int port, timeval tv),以帶有超時(shí)的方式連接redis服務(wù)器,同時(shí)獲取與redis連接的上下文對(duì)象。
函數(shù)原型:void *redisCommand(redisContext *c, const char *format, ...);
說明:該函數(shù)執(zhí)行命令,就如sql數(shù)據(jù)庫中的SQL語句一樣,只是執(zhí)行的是redis數(shù)據(jù)庫中的操作命令,第一個(gè)參數(shù)為連接數(shù)據(jù)庫時(shí)返回的redisContext,剩下的參數(shù)為變參,就如C標(biāo)準(zhǔn)函數(shù)printf函數(shù)一樣的變參。
函數(shù)返回值:返回值為void*,一般強(qiáng)制轉(zhuǎn)換成為redisReply類型,以便做進(jìn)一步處理。
函數(shù)原型void freeReplyObject(void *reply);
說明:釋放redisCommand執(zhí)行后返回的redisReply所占用的內(nèi)存;
函數(shù)返回值:無。
函數(shù)原型:void redisFree(redisContext *c);
說明:釋放redisConnect()所產(chǎn)生的連接。
函數(shù)返回值:無。
下面用一個(gè)簡(jiǎn)單的例子說明:
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <stdarg.h> #include <string.h> #include <assert.h> #include <hiredis/hiredis.h> void doTest() { //redis默認(rèn)監(jiān)聽端口為6387 可以再配置文件中修改 redisContext* c = redisConnect("127.0.0.1", 6379); if ( c->err) { redisFree(c); printf("Connect to redisServer faile\n"); return ; } printf("Connect to redisServer Success\n"); const char* command1 = "set stest1 value1"; redisReply* r = (redisReply*)redisCommand(c, command1); if( NULL == r) { printf("Execut command1 failure\n"); redisFree(c); return; } if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0)) { printf("Failed to execute command[%s]\n",command1); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command1); const char* command2 = "strlen stest1"; r = (redisReply*)redisCommand(c, command2); if ( r->type != REDIS_REPLY_INTEGER) { printf("Failed to execute command[%s]\n",command2); freeReplyObject(r); redisFree(c); return; } int length = r->integer; freeReplyObject(r); printf("The length of 'stest1' is %d.\n", length); printf("Succeed to execute command[%s]\n", command2); const char* command3 = "get stest1"; r = (redisReply*)redisCommand(c, command3); if ( r->type != REDIS_REPLY_STRING) { printf("Failed to execute command[%s]\n",command3); freeReplyObject(r); redisFree(c); return; } printf("The value of 'stest1' is %s\n", r->str); freeReplyObject(r); printf("Succeed to execute command[%s]\n", command3); const char* command4 = "get stest2"; r = (redisReply*)redisCommand(c, command4); if ( r->type != REDIS_REPLY_NIL) { printf("Failed to execute command[%s]\n",command4); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command4); redisFree(c); } int main() { doTest(); return 0; }
執(zhí)行結(jié)果為:
看完上述內(nèi)容,你們對(duì)怎么在C++項(xiàng)目中使用redis有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。