今天就跟大家聊聊有關(guān)怎么在C++項(xiàng)目中使用redis,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)公司是一家專業(yè)提供定結(jié)企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、HTML5、小程序制作等業(yè)務(wù)。10年已為定結(jié)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
C++使用redis的實(shí)例詳解
hiredis是redis數(shù)據(jù)庫(kù)的C接口,目前只能在linux下使用,幾個(gè)基本的函數(shù)就可以操作redis數(shù)據(jù)庫(kù)了。
函數(shù)原型:redisContext *redisConnect(const char *ip, int port);
說(shuō)明:該函數(shù)用來(lái)連接redis數(shù)據(jù)庫(kù),參數(shù)為數(shù)據(jù)庫(kù)的ip地址和端口,一般redis數(shù)據(jù)庫(kù)的端口為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, ...);
說(shuō)明:該函數(shù)執(zhí)行命令,就如sql數(shù)據(jù)庫(kù)中的SQL語(yǔ)句一樣,只是執(zhí)行的是redis數(shù)據(jù)庫(kù)中的操作命令,第一個(gè)參數(shù)為連接數(shù)據(jù)庫(kù)時(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);
說(shuō)明:釋放redisCommand執(zhí)行后返回的redisReply所占用的內(nèi)存;
函數(shù)返回值:無(wú)。
函數(shù)原型:void redisFree(redisContext *c);
說(shuō)明:釋放redisConnect()所產(chǎn)生的連接。
函數(shù)返回值:無(wú)。
下面用一個(gè)簡(jiǎn)單的例子說(shuō)明:
#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)聽(tīng)端口為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)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享文章:怎么在C++項(xiàng)目中使用redis
鏈接地址:http://www.chinadenli.net/article42/iiidhc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站內(nèi)鏈、手機(jī)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)