标签:密码登录 stream else hand end error 保存 端口号 返回
用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; }
标签:密码登录 stream else hand end error 保存 端口号 返回
原文地址:http://www.cnblogs.com/sysu-huangwei/p/7339729.html