Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / C++操作Redis数据库

今天,本人来学习如何用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()所产生的连接。接下来就是就让本人来教大家如何安装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#ifndef _REDIS_H_
#define _REDIS_H_#include <iostream>
#include <string.h>
#include <string>
#include <stdio.h>#include <hiredis/hiredis.h>class Redis
{
public:    Redis(){}    ~Redis()
 {
        this->_connect = NULL;
  this->_reply = NULL;      
 } bool connect(std::string host, int port)
 {
   this->_connect = redisConnect(host.c_str(), port);
  if(this->_connect != NULL && this->_connect->err)
  {
      printf("connect error: %s ", this->_connect->errstr);
   return 0;
  }
  return 1;
 }    std::string get(std::string key)
 {
  this->_reply = (redisReply*)redisCommand(this->_connect, "GET %s", key.c_str());
  std::string str = this->_reply->str;
  freeReplyObject(this->_reply);
  return str;
 } void set(std::string key, std::string value)
 {
        redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str());
 }private:    redisContext* _connect;
 redisReply* _reply;
   
};#endif  //_REDIS_H_redis.cpp#include "redis.h"int main()
{
 Redis *r = new Redis();
 if(!r->connect("192.168.13.128", 6379))
 {
  printf("connect error! ");
  return 0;
 }
 r->set("name", "Mayuyu");
 printf("Get the name is %s ", r->get("name").c_str());
 delete r;
 return 0;
}Makefile文件redis: redis.cpp redis.h
 g++ redis.cpp -o redis -L/usr/local/lib/ -lhiredisclean:
 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更新一下配置即可。《C++ 设计新思维》 下载见 http://www.linuxidc.com/Linux/2014-07/104850.htmC++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码 http://www.linuxidc.com/Linux/2014-05/101227.htm读C++ Primer 之构造函数陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm读C++ Primer 之智能指针 http://www.linuxidc.com/Linux/2011-08/40177.htm读C++ Primer 之句柄类 http://www.linuxidc.com/Linux/2011-08/40175.htm将C语言梳理一下,分布在以下10个章节中:
  1. Linux-C成长之路(一):Linux下C编程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成长之路(二):基本数据类型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成长之路(三):基本IO函数操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成长之路(四):运算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成长之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成长之路(六):函数要义 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成长之路(七):数组与指针 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成长之路(八):存储类,动态内存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成长之路(九):复合数据类型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成长之路(十):其他高级议题
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-12/111009.htm