Redis C++編程實例


基本功能步驟:

1,下載安裝Redis,下載地址:http://www.redis.cn/download.html。

2,下載安裝hiredis,下載地址:https://github.com/redis/hiredis。

將libhiredis.so放到/usr/lib/目錄下。

3,在hiredis-master目錄下編寫客戶端程序。

 

[cpp]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include "hiredis.h"  
  3.   
  4. int main()  
  5. {  
  6.      redisContext *conn  = redisConnect("127.0.0.1",6379);  
  7.      if(conn != NULL && conn->err)  
  8.      {  
  9.          printf("connection error: %s\n",conn->errstr);  
  10.          return 0;  
  11.      }  
  12.      redisReply *reply = (redisReply*)redisCommand(conn,"set foo 1234");  
  13.      freeReplyObject(reply);  
  14.   
  15.      reply = redisCommand(conn,"get foo");  
  16.      printf("%s\n",reply->str);  
  17.      freeReplyObject(reply);  
  18.   
  19.      redisFree(conn);  
  20.      return 0;  
  21. }  

編譯命令:gcc -o redistest redistest.c -lhiredis

 

4,啟動服務器,運行客戶端程序。

 

Redis pub/sub功能實現。

發布pub功能:

 

[cpp]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include <sys/time.h>  
  3. #include "hiredis.h"  
  4.   
  5. int main()  
  6. {  
  7.      struct timeval begin;  
  8.   
  9.      redisContext *conn  = redisConnect("127.0.0.1",6379);  
  10.      if(conn != NULL && conn->err)  
  11.      {  
  12.          printf("connection error: %s\n",conn->errstr);  
  13.          return 0;  
  14.      }  
  15.   
  16.      gettimeofday(&begin, NULL);  
  17.      redisReply *reply = (redisReply*)redisCommand(conn,"publish foo 1234");  
  18.   
  19.      int sec, usec;  
  20.      sec = begin.tv_sec;  
  21.      usec = begin.tv_usec;  
  22.   
  23.      printf("%d\t%d\n", sec, usec);  
  24.   
  25.      freeReplyObject(reply);  
  26.   
  27.      reply = redisCommand(conn,"get foo");  
  28.      printf("%s\n",reply->str);  
  29.      freeReplyObject(reply);  
  30.   
  31.      redisFree(conn);  
  32.      return 0;  
  33. }  

訂閱sub功能:

 

 

[cpp]  view plain  copy
 
  1. #include <sys/time.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <signal.h>  
  6. #include "hiredis.h"  
  7. #include "async.h"  
  8. #include "adapters/libevent.h"  
  9.   
  10. void subCallback(redisAsyncContext *c, void *r, void *priv) {  
  11.     struct timeval end;  
  12.     int sec, usec;  
  13.   
  14.     redisReply *reply = r;  
  15.     if (reply == NULL) return;  
  16.     if ( reply->type == REDIS_REPLY_ARRAY && reply->elements == 3 ) {  
  17.         if ( strcmp( reply->element[0]->str, "subscribe" ) != 0 ) {  
  18.   
  19.                 gettimeofday(&end,NULL);  
  20.   
  21.                 sec = end.tv_sec;  
  22.                 usec = end.tv_usec;  
  23.                 printf("%d\t%d\n", sec, usec);  
  24.   
  25.             printf( "Received[%s] channel %s: %s\n",  
  26.                     (char*)priv,  
  27.                     reply->element[1]->str,  
  28.                     reply->element[2]->str );  
  29.         }  
  30.     }  
  31. }  
  32.   
  33. void connectCallback(const redisAsyncContext *c, int status) {  
  34.     if (status != REDIS_OK) {  
  35.         printf("Error: %s\n", c->errstr);  
  36.         return;  
  37.     }  
  38.     printf("Connected...\n");  
  39. }  
  40.   
  41. void disconnectCallback(const redisAsyncContext *c, int status) {  
  42.     if (status != REDIS_OK) {  
  43.         printf("Error: %s\n", c->errstr);  
  44.         return;  
  45.     }  
  46.     printf("Disconnected...\n");  
  47. }  
  48.   
  49. int main (int argc, char **argv) {  
  50.     signal(SIGPIPE, SIG_IGN);  
  51.     struct event_base *base = event_base_new();  
  52.   
  53.     redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);  
  54.     if (c->err) {  
  55.         /* Let *c leak for now... */  
  56.         printf("Error: %s\n", c->errstr);  
  57.         return 1;  
  58.     }  
  59.   
  60.     redisLibeventAttach(c,base);  
  61.     redisAsyncSetConnectCallback(c,connectCallback);  
  62.     redisAsyncSetDisconnectCallback(c,disconnectCallback);  
  63.     redisAsyncCommand(c, subCallback, (char*) "sub", "SUBSCRIBE foo");  
  64.   
  65.     event_base_dispatch(base);  
  66.     return 0;  
  67. }  

 

pub編譯命令同上,sub編譯命令:gcc -o sub sub.c -lhiredis -levent

 

注意:

1,編譯基本程序時可能需要建立鏈接:ln -s libhiredis.so libhiredis.so.0.12

2,1,訂閱sub需要安裝libevent-dev。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM