C++使用redis的實(shí)例詳解
C++使用redis的實(shí)例詳解
hiredis是redis數(shù)據(jù)庫(kù)的C接口,目前只能在linux下使用,幾個(gè)基本的函數(shù)就可以操作redis數(shù)據(jù)庫(kù)了。
函數(shù)原型:redisContext *redisConnect(const char *ip, int port);
說明:該函數(shù)用來連接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, ...);
說明:該函數(shù)執(zhí)行命令,就如sql數(shù)據(jù)庫(kù)中的SQL語句一樣,只是執(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);
說明:釋放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é)果為:

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

