用c++簡單封裝了redis的基本操作(hiredis) 介面包括:①鏈接和斷開連接、②設置鍵值對(set)、③查詢鍵值對(get)、④刪除鍵值對(del)、⑤將所有鍵顯示出來 若任何一處發生錯誤,返回對應的錯誤狀態碼,同時可以調用getErrorMsg()查看錯誤信息 所有碼包括: M_REDIS ...
用c++簡單封裝了redis的基本操作(hiredis)
介面包括:①鏈接和斷開連接、②設置鍵值對(set)、③查詢鍵值對(get)、④刪除鍵值對(del)、⑤將所有鍵顯示出來
若任何一處發生錯誤,返回對應的錯誤狀態碼,同時可以調用getErrorMsg()查看錯誤信息
所有碼包括:
M_REDIS_OK = 0, //執行成功
M_CONNECT_FAIL = -1, //連接redis失敗
M_CONTEXT_ERROR = -2, //RedisContext返回錯誤
M_REPLY_ERROR = -3, //redisReply錯誤
M_EXE_COMMAND_ERROR = -4 //redis命令執行錯誤
tips:在鏈接hiredis時需要添加鏈接選項: -levent -lhiredis
代碼如下:
redis_handler.h
#ifndef __REDIS_HANDLER_H__ #define __REDIS_HANDLER_H__ //#include "hiredis\hiredis.h" #include <hiredis/hiredis.h> #include <string> using namespace std; enum { M_REDIS_OK = 0, //執行成功 M_CONNECT_FAIL = -1, //連接redis失敗 M_CONTEXT_ERROR = -2, //RedisContext返回錯誤 M_REPLY_ERROR = -3, //redisReply錯誤 M_EXE_COMMAND_ERROR = -4 //redis命令執行錯誤 }; class RedisHandler { public: RedisHandler(); ~RedisHandler(); int connect(const string &addr, int port, const string &pwd = ""); //連接redis資料庫:addr:IP地址,port:埠號,pwd:密碼(預設為空) int disConnect(); //斷開連接 int setValue(const string &key, const string &value); //添加或修改鍵值對,成功返回0,失敗<0 int getValue(const string &key, string &value); //獲取鍵對應的值,成功返回0,失敗<0 int delKey(const string &key); //刪除鍵,成功返回影響的行數,失敗<0 int printAll(); //列印所有的鍵值對 string getErrorMsg(); //獲取錯誤信息 private: string m_addr; //IP地址 int m_port; //埠號 string m_pwd; //密碼 redisContext* pm_rct; //redis結構體 redisReply* pm_rr; //返回結構體 string error_msg; //錯誤信息 int connectAuth(const string &pwd); //使用密碼登錄 int handleReply(void* value = NULL, redisReply ***array = NULL); //處理返回的結果 }; #endif
redis_handler.cpp
#include "redis_handler.h" #include <string> #include <cstring> #include <iostream> using namespace std; RedisHandler::RedisHandler() { m_addr = ""; m_port = 0; m_pwd = ""; pm_rct = NULL; pm_rr = NULL; error_msg = ""; } RedisHandler::~RedisHandler() { disConnect(); pm_rct = NULL; pm_rr = NULL; } /* 連接redis資料庫 addr: 地址,port:埠號,pwd:密碼 成功返回M_REDIS_OK,失敗返回M_CONNECT_FAIL */ int RedisHandler::connect(const string &addr = "127.0.0.1", int port = 6379, const string &pwd) { m_addr = addr; m_port = port; m_pwd = pwd; pm_rct = redisConnect(m_addr.c_str(), m_port); if (pm_rct->err) { error_msg = pm_rct->errstr; return M_CONNECT_FAIL; } if (!m_pwd.empty()) { return connectAuth(m_pwd); } return M_REDIS_OK; } /* 斷開redis連接 */ int RedisHandler::disConnect() { redisFree(pm_rct); freeReplyObject(pm_rr); } /* 添加或插入鍵值對 key:鍵,value:值 成功返回M_REDIS_OK,失敗返回<0 */ int RedisHandler::setValue(const string &key, const string &value) { string cmd = "set " + key + " " + value; pm_rr = (redisReply*)redisCommand(pm_rct, cmd.c_str()); return handleReply(); } /* 獲取鍵對應的值 key:鍵,value:值引用 成功返回M_REDIS_OK,失敗返回<0 */ int RedisHandler::getValue(const string &key, string &value) { string cmd = "get " + key; pm_rr = (redisReply*)redisCommand(pm_rct, cmd.c_str()); int ret = handleReply(&value); } /* 刪除鍵 key:鍵 成功返回影響的行數(可能為0),失敗返回<0 */ int RedisHandler::delKey(const string &key) { string cmd = "del " + key; pm_rr = (redisReply*)redisCommand(pm_rct, cmd.c_str()); int rows = 0; int ret = handleReply(&rows); if (ret == M_REDIS_OK) return rows; else return ret; } /* 列印所有鍵值對到屏幕上 */ int RedisHandler::printAll() { string cmd = "keys *"; pm_rr = (redisReply*)redisCommand(pm_rct, cmd.c_str()); int len ; redisReply **array; int ret = handleReply(&len, &array); if (ret == M_REDIS_OK) { for (int i = 0; i < len; i++) cout << string(array[i]->str) << endl; } else return 0; } /* 返回錯誤信息 */ string RedisHandler::getErrorMsg() { return error_msg; } /* 使用密碼登錄 psw:登錄密碼 成功返回M_REDIS_OK,失敗返回<0 */ int RedisHandler::connectAuth(const string &psw) { string cmd = "auth " + psw; pm_rr = (redisReply*)redisCommand(pm_rct, cmd.c_str()); return handleReply(); } /* 處理redis返回的信息 value:數據指針,用於保存redis返回的基本類型(value指針指向該數據) array:數組指針,用於保存redis返回的數組 成功返回M_REDIS_OK,失敗返回<0 */ int RedisHandler::handleReply(void* value, redisReply*** array) { if (pm_rct->err) { error_msg = pm_rct->errstr; return M_CONTEXT_ERROR; } if (pm_rr == NULL) { error_msg = "auth redisReply is NULL"; return M_REPLY_ERROR; } switch (pm_rr->type) { case REDIS_REPLY_ERROR: error_msg = pm_rr->str; return M_EXE_COMMAND_ERROR; case REDIS_REPLY_STATUS: if (!strcmp(pm_rr->str, "OK")) return M_REDIS_OK; else { error_msg = pm_rr->str; return M_EXE_COMMAND_ERROR; } case REDIS_REPLY_INTEGER: *(int*)value = pm_rr->integer; return M_REDIS_OK; case REDIS_REPLY_STRING: *(string*)value = pm_rr->str; return M_REDIS_OK; case REDIS_REPLY_NIL: *(string*)value = ""; return M_REDIS_OK; case REDIS_REPLY_ARRAY: *(int*)value = pm_rr->elements; *array = pm_rr->element; return M_REDIS_OK; default: error_msg = "unknow reply type"; return M_EXE_COMMAND_ERROR; } }
測試程式main.cpp
#include <iostream> #include <string> #include "redis_handler.h" using namespace std; int main() { RedisHandler* rh = new RedisHandler(); int ret; //連接測試 cout << "錯誤測試: " << "地址錯誤" << endl; ret = rh->connect("34.15.14.15", 6379, "linesum"); if (ret != M_REDIS_OK) cout << "redis error: " << rh->getErrorMsg() << endl; cout << "錯誤測試: " << "埠錯誤" << endl; ret = rh->connect("127.0.0.1", 1234, "linesum"); if (ret != M_REDIS_OK) cout << "redis error: " << rh->getErrorMsg() << endl; cout << "錯誤測試: " << "密碼錯誤" << endl; ret = rh->connect("127.0.0.1", 6479, "linsum"); if (ret != M_REDIS_OK) cout << "redis error: " << rh->getErrorMsg() << endl; ret = rh->connect("127.0.0.1", 6479, "linesum"); if (ret != M_REDIS_OK) { cout << "redis error: " << rh->getErrorMsg() << endl; return ret; } //set測試 cout << "錯誤測試: " << "set不帶value參數" << endl; ret = rh->setValue("key11", ""); if (ret != M_REDIS_OK) cout << "redis error: " << rh->getErrorMsg() << endl; ret = rh->setValue("key11", "value11"); if (ret != M_REDIS_OK) { cout << "redis error: " << rh->getErrorMsg() << endl; return ret; } ret = rh->setValue("key22", "value22"); if (ret != M_REDIS_OK) { cout << "redis error: " << rh->getErrorMsg() << endl; return ret; } //get測試 string str; cout << "錯誤測試: " << "get不帶key參數" << endl; ret = rh->getValue("key1111", str); if (ret != M_REDIS_OK) cout << "redis error: " << rh->getErrorMsg() << endl; ret = rh->getValue("key11", str); if (ret != M_REDIS_OK) { cout << "redis error: " << rh->getErrorMsg() << endl; return ret; } else cout << "value : " << str << endl; //print測試 ret = rh->printAll(); if (ret != M_REDIS_OK) { cout << "redis error: " << rh->getErrorMsg() << endl; return ret; } //del測試 cout << "錯誤測試: " << "刪除不存在的key" << endl; ret = rh->delKey("key1111"); if (ret != M_REDIS_OK) cout << "redis error: " << rh->getErrorMsg() << endl; ret = rh->delKey("key11"); if (ret != M_REDIS_OK) { cout << "redis error: " << rh->getErrorMsg() << endl; return ret; } delete rh; return 0; }