C++redis 를 사용 하 는 실례 상세 설명

4281 단어 C++redis
C++redis 를 사용 하 는 실례 상세 설명
hiredis 는 redis 데이터베이스 의 C 인터페이스 로 현재 Liux 에서 만 사용 할 수 있 고 몇 가지 기본 적 인 함수 로 redis 데이터 베 이 스 를 조작 할 수 있 습 니 다. 
함수 원형:redisContext*redisConnect(const char*ip,int port);
설명:이 함 수 는 redis 데이터 베 이 스 를 연결 하 는 데 사 용 됩 니 다.매개 변 수 는 데이터 뱅 크 의 ip 주소 와 포트 이 고 일반 redis 데이터 뱅 크 의 포트 는 6379 입 니 다.
함수 반환 값:이 함수 가 구조 체 redisContext 를 되 돌려 줍 니 다.
유사 하 게 하나의 함수 redisContext*redisConnectWith Timeout(const char*ip,int port,timeval tv)을 제공 하여 시간 초과 방식 으로 redis 서버 를 연결 하고 redis 와 연 결 된 컨 텍스트 대상 을 가 져 옵 니 다. 
함수 원형:void*redisCommand(redisContext*c,const char*format,...);
설명:이 함수 실행 명령 은 sql 데이터베이스 에 있 는 SQL 문장 과 같이 redis 데이터베이스 에 있 는 조작 명령 만 실 행 됩 니 다.첫 번 째 매개 변 수 는 데이터 베 이 스 를 연결 할 때 되 돌아 오 는 redis Context 이 고 나머지 매개 변 수 는 변 삼 입 니 다.C 표준 함수 printf 함수 와 같은 변 삼 입 니 다.
함수 반환 값:반환 값 은 void*이 며,일반적으로 redisReply 형식 으로 강제로 변환 하여 진일보 한 처 리 를 할 수 있 습 니 다.
 함수 원형 void freeReplyObject(void*reply);
설명:redisCommand 가 실 행 된 후 돌아 오 는 redisReply 가 사용 하 는 메모리 방출;
 함수 반환 값:없 음.
함수 원형:void redisFree(redisContext*c);
설명:redisConnect()에서 발생 하 는 연결 을 방출 합 니 다.
함수 반환 값:없 음. 
다음은 간단 한 예 로 설명 한다.

#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       6387            
  redisContext* c = redisConnect("127.0.0.1", 6379); 
  if ( c->err) 
  { 
    redisFree(c); 
    printf("Connect to redisServer faile
"); return ; } printf("Connect to redisServer Success
"); const char* command1 = "set stest1 value1"; redisReply* r = (redisReply*)redisCommand(c, command1); if( NULL == r) { printf("Execut command1 failure
"); redisFree(c); return; } if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0)) { printf("Failed to execute command[%s]
",command1); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]
", command1); const char* command2 = "strlen stest1"; r = (redisReply*)redisCommand(c, command2); if ( r->type != REDIS_REPLY_INTEGER) { printf("Failed to execute command[%s]
",command2); freeReplyObject(r); redisFree(c); return; } int length = r->integer; freeReplyObject(r); printf("The length of 'stest1' is %d.
", length); printf("Succeed to execute command[%s]
", command2); const char* command3 = "get stest1"; r = (redisReply*)redisCommand(c, command3); if ( r->type != REDIS_REPLY_STRING) { printf("Failed to execute command[%s]
",command3); freeReplyObject(r); redisFree(c); return; } printf("The value of 'stest1' is %s
", r->str); freeReplyObject(r); printf("Succeed to execute command[%s]
", command3); const char* command4 = "get stest2"; r = (redisReply*)redisCommand(c, command4); if ( r->type != REDIS_REPLY_NIL) { printf("Failed to execute command[%s]
",command4); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]
", command4); redisFree(c); } int main() { doTest(); return 0; }
실행 결 과 는:

궁금 한 점 이 있 으 시 면 메 시 지 를 남기 거나 본 사이트 의 커 뮤 니 티 에 가서 토론 을 교류 하 세 요.읽 어 주 셔 서 감사합니다. 도움 이 되 셨 으 면 좋 겠 습 니 다.본 사이트 에 대한 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기