文章目录
前言
-
用官方提供的
hiredis
接口
1. 登陆信息配置
编写配置文件:
{
"IP" : "127.0.0.1" ,
"PORT" : 6379
}
提取配置信息:
-
redisConf.h
#ifndef __REDISCONF_H__ #define __REDISCONF_H__ #include <string> namespace ccx{ using std::string; class RedisConf { public: RedisConf(); void getConf(); string getIP(); int getPort(); private: string _ip; int _port; }; } #endif
-
redisConf.cpp
#include "redisConf.h" #include <stdlib.h> #include <json/json.h> #include <iostream> #include <fstream> namespace ccx{ using std::ifstream; using std::cout; using std::endl; RedisConf::RedisConf() { getConf(); } // *** void RedisConf::getConf() { // 打开配置文件 ifstream ifs; ifs.open("redisConf.json"); if(!ifs.good()) { cout << "open RedisConf.json error" << endl; exit(EXIT_FAILURE); } // 提取json信息 Json::Value root; // 根对象 Json::Reader reader;// json解析器 if(!reader.parse(ifs, root, false)) { cout << "RedisConf json reader error" << endl; exit(EXIT_FAILURE); } // 解析json内容 _ip = root["IP"].asString(); _port = root["PORT"].asInt(); ifs.close(); } string RedisConf::getIP() { return _ip