数据库:c++ 操作 redis

前言

  • 用官方提供的 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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值