亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++使用redis的實(shí)例詳解

 更新時(shí)間:2017年10月11日 14:01:36   作者:愛思考的小鳥  
這篇文章主要介紹了C++使用redis的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下

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)聽端口為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é)果為:

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論