C++操作Redis資料庫

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

今天,Mayuyu來學習如何用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

#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.hg++ 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++操作Redis資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.