• 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 ",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 ",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 ",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 %d ", sec, usec);  
    24.   
    25.      freeReplyObject(reply);  
    26.   
    27.      reply = redisCommand(conn,"get foo");  
    28.      printf("%s ",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 %d ", sec, usec);  
    24.   
    25.             printf( "Received[%s] channel %s: %s ",  
    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 ", c->errstr);  
    36.         return;  
    37.     }  
    38.     printf("Connected... ");  
    39. }  
    40.   
    41. void disconnectCallback(const redisAsyncContext *c, int status) {  
    42.     if (status != REDIS_OK) {  
    43.         printf("Error: %s ", c->errstr);  
    44.         return;  
    45.     }  
    46.     printf("Disconnected... ");  
    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 ", 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。

  • 相关阅读:
    oracle索引
    linux命令
    struts2的配置文件struts.xml详解
    oracle删除一个表中的重复数据,且只保留一条
    初步研究java类的加载机制
    java读取Properties配置文件
    利用oracle闪回来找回被drop的表
    快速排序Quicksort
    树与树的概念
    递归
  • 原文地址:https://www.cnblogs.com/fire909090/p/6770059.html
Copyright © 2020-2023  润新知