C++操作redis数据库

来源:互联网 发布:口腔医学专升本知乎 编辑:程序博客网 时间:2024/06/09 19:48

  用C++C++

  C++操作redis数据库。通过hiredis.h接口来实现,目前只能在Linux环境使用。


hiredis.h的下载地址为:https://github.com/redis/hiredis


 

主要包括如下四个方法

 

1. redisContext* redisConnect(const char *ip, int port)

 

   该函数用来连接redis数据库, 两个参数分别是redis数据库的ip和端口,端口号一般为6379。类似

   的还提供了一个函数,供连接超时限定,即


   redisContext* redisConnectWithTimeout(const char *ip, int port, timeval tv)。


 

2. void *redisCommand(redisContext *c, const char *format...)

 

   该函数用于执行redis数据库中的命令,第一个参数为连接数据库返回的redisContext,剩下的参数

   为变参,如同C语言中的prinf()函数。

 

   此函数的返回值为void*,但是一般会强制转换为redisReply类型,以便做进一步的处理。


 

3. void freeReplyObject(void *reply)

 

   释放redisCommand执行后返回的的redisReply所占用的内存。


 

4. void redisFree(redisContext *c)

 

   释放redisConnect()所产生的连接。



接下来就是就让Mayuyu来教大家如何安装hiredis吧!


首先上网站下载hiredis.tar.gz包,解压后发现里面有一个Makefile文件,然后执行make进行编译,得到




接下来把libhiredis.so放到/usr/local/lib/中,把hiredis.h放到/usr/local/inlcude/hiredis/中。

或者直接用命令make install配置。如下图




接下来在程序中就可以直接用了。在程序中包含#include <hiredis/hiredis.h>即可。


为了操作方便,一般情况下我们需要写一个头文件类,这个类的方法根据自己项目需要适当添加。如下


代码:redis.h

[cpp] view plain copy
  1. #ifndef _REDIS_H_  
  2. #define _REDIS_H_  
  3.   
  4. #include <iostream>  
  5. #include <string.h>  
  6. #include <string>  
  7. #include <stdio.h>  
  8.   
  9. #include <hiredis/hiredis.h>  
  10.   
  11. class Redis  
  12. {  
  13. public:  
  14.   
  15.     Redis(){}  
  16.   
  17.     ~Redis()  
  18.     {  
  19.         this->_connect = NULL;  
  20.         this->_reply = NULL;               
  21.     }  
  22.   
  23.     bool connect(std::string host, int port)  
  24.     {  
  25.         this->_connect = redisConnect(host.c_str(), port);    //c_str 是c++ 中 string类 (class) 的 函数,它能把 string类 的对象里的字符串 转换成 C 中 char 型变量 的 字符串
  26.         if(this->_connect != NULL && this->_connect->err)  
  27.         {  
  28.             printf("connect error: %s\n"this->_connect->errstr);  
  29.             return 0;  
  30.         }  
  31.         return 1;  
  32.     }  
  33.   
  34.     std::string get(std::string key)  
  35.     {  
  36.         this->_reply = (redisReply*)redisCommand(this->_connect, "GET %s", key.c_str());  
  37.         std::string str = this->_reply->str;  
  38.         freeReplyObject(this->_reply);  
  39.         return str;  
  40.     }  
  41.   
  42.     void set(std::string key, std::string value)  
  43.     {  
  44.         redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str());  
  45.     }  
  46.   
  47. private:  
  48.   
  49.     redisContext* _connect;  
  50.     redisReply* _reply;  
  51.                   
  52. };  
  53.   
  54. #endif  //_REDIS_H_  

 

redis.cpp

[cpp] view plain copy
  1. #include "redis.h"  
  2.   
  3. int main()  
  4. {  
  5.     Redis *r = new Redis();  
  6.     if(!r->connect("192.168.13.128", 6379))  
  7.     {  
  8.         printf("connect error!\n");  
  9.         return 0;  
  10.     }  
  11.     r->set("name""Mayuyu");  
  12.     printf("Get the name is %s\n", r->get("name").c_str());  
  13.     delete r;  
  14.     return 0;  
  15. }  


Makefile文件

[cpp] view plain copy
  1. redis: redis.cpp redis.h  
  2.     g++ redis.cpp -o redis -L/usr/local/lib/ -lhiredis  
  3.   
  4. clean:  
  5.     rm redis.o redis  


注意在g++和rm之前都是一个tab键。


 

在编译的时候需要加参数,假设文件为redis.cpp,那么编译命令如下


       g++ redis.cpp -o redis -L/usr/local/lib/ -lhiredis 


在执行的时候如果出现动态库无法加载,那么需要进行如下配置


       在/etc/ld.so.conf.d/目录下新建文件usr-libs.conf,内容是:/usr/local/lib 


如下图所示


       

 

然后使用命令/sbin/ldconfig更新一下配置即可。

原创粉丝点击