C++使用redis的實例詳解
C++使用redis的實例詳解
hiredis是redis數(shù)據(jù)庫的C接口,目前只能在linux下使用,幾個基本的函數(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ù)返回一個結(jié)構(gòu)體redisContext;
類似的提供了一個函數(shù)redisContext* redisConnectWithTimeout(const char *ip, int port, timeval tv),以帶有超時的方式連接redis服務(wù)器,同時獲取與redis連接的上下文對象。
函數(shù)原型:void *redisCommand(redisContext *c, const char *format, ...);
說明:該函數(shù)執(zhí)行命令,就如sql數(shù)據(jù)庫中的SQL語句一樣,只是執(zhí)行的是redis數(shù)據(jù)庫中的操作命令,第一個參數(shù)為連接數(shù)據(jù)庫時返回的redisContext,剩下的參數(shù)為變參,就如C標(biāo)準(zhǔn)函數(shù)printf函數(shù)一樣的變參。
函數(shù)返回值:返回值為void*,一般強制轉(zhuǎn)換成為redisReply類型,以便做進一步處理。
函數(shù)原型void freeReplyObject(void *reply);
說明:釋放redisCommand執(zhí)行后返回的redisReply所占用的內(nèi)存;
函數(shù)返回值:無。
函數(shù)原型:void redisFree(redisContext *c);
說明:釋放redisConnect()所產(chǎn)生的連接。
函數(shù)返回值:無。
下面用一個簡單的例子說明:
#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é)果為:

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
VC中使用ADO開發(fā)數(shù)據(jù)庫應(yīng)用程序簡明教程
這篇文章主要介紹了VC中使用ADO開發(fā)數(shù)據(jù)庫應(yīng)用程序的方法,結(jié)合實例形式詳細(xì)講述了ADO的原理及VC使用ADO開發(fā)數(shù)據(jù)庫應(yīng)用程序的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2016-06-06
C++實現(xiàn)LeetCode(642.設(shè)計搜索自動補全系統(tǒng))
這篇文章主要介紹了C++實現(xiàn)LeetCode(642.設(shè)計搜索自動補全系統(tǒng)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C語言中pthread_exit()函數(shù)實現(xiàn)終止線程
本文主要介紹了C語言中pthread_exit()函數(shù)實現(xiàn)終止線程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
C語言實現(xiàn)大數(shù)值金額大寫轉(zhuǎn)換的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C語言實現(xiàn)大數(shù)值金額大寫轉(zhuǎn)換的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-03-03

