歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++操作Redis數據庫

C++操作Redis數據庫

日期:2017/3/1 9:35:53   编辑:Linux編程

今天,本人來學習如何用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\n", 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!\n");
return 0;
}
r->set("name", "Mayuyu");
printf("Get the name is %s\n", r->get("name").c_str());
delete r;
return 0;
}

Makefile文件

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

clean:
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.htm

C++ 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成長之路(十):其他高級議題

Copyright © Linux教程網 All Rights Reserved