RDConnector 函数手册
C++ 后端服务器框架RDConnector 函数手册
CHAPTER 21

RDConnector 函数手册

4 分钟阅读 · 975 字

依据源码:include/RDConnector.h、src/RDConnector.cpp。


0. 模块定位

RdConnector 是基于 hiredis 的轻量 Redis 封装,提供:

  • 连接与认证
  • 数据库选择
  • 基础 KV 操作:get/set/setex/exists/del
  • 连接错误信息获取

1. 数据成员与生命周期

成员

  • host_, port_, password_, db
  • redisContext* context
  • static RdConnector* instance_

构造函数

RdConnector(std::string host, std::string port, std::string password, int db)

  • 仅保存参数,context = nullptr
  • 不主动建连

析构函数

~RdConnector()

  • 若 context != nullptr:
    • redisFree(context)
    • context = nullptr
    • 打印 "redis context free"

2. 单例语义说明

static RdConnector* instance_

  • 初始为 nullptr
  • 在 connect() 成功后,若 instance_ == nullptr,置为 this
  • getInstance() 仅返回静态指针,不负责创建

这不是严格工厂单例,而是“首次成功连接对象成为全局实例”的约束模式。


3. 连接流程

bool connect()

作用

建立到 Redis 的连接,并可选执行 AUTH 与 SELECT。

执行步骤

  1. 实例唯一性检查

    • 若 instance_ != nullptr && instance_ != this:
      • 打印 "Only one instance of RdConnector is allowed."
      • 返回 false
  2. 清理旧连接

    • 若 context != nullptr:
      • redisFree(context),context = nullptr
  3. 端口解析

    • std::stoi(port_)
    • 异常时打印“端口格式错误”,返回 false
  4. 建立连接

    • context = redisConnect(host_.c_str(), port)
    • 若 context == nullptr || context->err:
      • 打印连接失败信息
      • 返回 false
  5. 认证(可选)

    • 若 password_ 非空:
      • redisCommand("AUTH %s", password_)
      • reply 为 null 或 error 时:
        • 打印失败
        • redisFree(context),context = nullptr
        • freeReplyObject(reply)
        • 返回 false
      • 成功时释放 reply
  6. 切库(可选)

    • 若 db != 0:
      • redisCommand("SELECT %d", db)
      • 失败处理同上(释放 context + reply,返回 false)
      • 成功释放 reply
  7. 设置全局实例

    • 若 instance_ == nullptr,则 instance_ = this
  8. 返回 true

风险点

  • freeReplyObject(reply) 在某些失败路径上可能接收到 nullptr,hiredis 一般容忍,但最好显式判空。
  • 当前无连接超时与重连策略。

4. 读操作

std::string get(const std::string& key)

逻辑

  1. 若 context == nullptr:打印“未连接”,返回空字符串
  2. 执行 GET key
  3. reply 为 null 或 error:打印失败,释放 reply,返回空字符串
  4. 按 reply 类型处理:
    • REDIS_REPLY_STRING:返回 reply->str
    • REDIS_REPLY_NIL:返回 ""(键不存在)
    • 其他类型:打印异常类型,返回 ""
  5. 释放 reply

语义提醒

  • “键不存在”与“命令失败”都可能返回空字符串,调用方需结合日志或后续 exists 判断。

5. 写操作

5.1 bool set(const std::string& key, const std::string& value)

逻辑

  1. 检查连接,未连接返回 false
  2. 执行 SET key value
  3. reply null 或 error:打印并返回 false
  4. 判断成功条件:
    • reply->type == REDIS_REPLY_STATUS
    • reply->str == "OK"
  5. 失败时打印 SET 命令失败
  6. 释放 reply,返回 success

5.2 bool set(const std::string& key, const std::string& value, int expireSeconds)

逻辑

  1. 检查连接,未连接 false
  2. 校验 expireSeconds > 0,否则 false
  3. 执行 SETEX key expireSeconds value
  4. reply null 或 error -> false
  5. 成功条件同上(STATUS + “OK”)
  6. 释放 reply,返回结果

6. 存在性检查

bool exists(const std::string& key)

逻辑

  1. 检查连接
  2. 执行 EXISTS key
  3. reply null 或 error -> false
  4. 若 reply->type == REDIS_REPLY_INTEGER:
    • reply->integer == 1 -> true
    • 否则 false
  5. 类型异常时打印告警
  6. 释放 reply

7. 删除操作

bool del(const std::string& key)

逻辑

  1. 检查连接
  2. 执行 DEL key
  3. reply null -> false
  4. 若 REDIS_REPLY_ERROR -> false
  5. 若类型不是 REDIS_REPLY_INTEGER -> false
  6. 否则返回 true
  7. 释放 reply

语义说明

  • 当前实现“DEL 返回整数类型即视为成功”,不区分删除 0 个还是 >0 个键。

8. 实例与错误读取接口

static RdConnector* getInstance()

  • 直接返回 instance_
  • 若未成功连接过,可能为 nullptr

std::string getError()

  • 若 context == nullptr:返回 "未初始化连接"
  • 否则返回 context->errstr

9. 并发与线程安全

当前类内部没有显式互斥保护:

  • 多线程共享同一个 RdConnector/redisContext 调用 redisCommand 可能有并发风险
  • 建议每线程独立连接或引入连接池

10. 面试可讲的改进方向

  1. 引入连接池与自动重连
  2. 增加超时配置(连接/读写)
  3. 增加批量命令与 pipeline
  4. 明确返回语义(键不存在 vs 执行失败)
  5. 增强错误模型(错误码 + 文本)

下一模块(顺序)

Server → docs/modules/detail/Server_FUNCTIONS.md

全部章节