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。

 

原文链接: https://www.cnblogs.com/fire909090/p/6770059.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Redis C++编程实例

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/253060

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午6:37
下一篇 2023年2月14日 上午6:38

相关推荐