CHAPTER 21
RDConnector 函数手册
依据源码:include/RDConnector.h、src/RDConnector.cpp。
0. 模块定位
RdConnector 是基于 hiredis 的轻量 Redis 封装,提供:
- 连接与认证
- 数据库选择
- 基础 KV 操作:
get/set/setex/exists/del - 连接错误信息获取
1. 数据成员与生命周期
成员
host_,port_,password_,dbredisContext* contextstatic 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。
执行步骤
-
实例唯一性检查
- 若
instance_ != nullptr && instance_ != this:- 打印
"Only one instance of RdConnector is allowed." - 返回
false
- 打印
- 若
-
清理旧连接
- 若
context != nullptr:redisFree(context),context = nullptr
- 若
-
端口解析
std::stoi(port_)- 异常时打印“端口格式错误”,返回
false
-
建立连接
context = redisConnect(host_.c_str(), port)- 若
context == nullptr || context->err:- 打印连接失败信息
- 返回
false
-
认证(可选)
- 若
password_非空:redisCommand("AUTH %s", password_)- reply 为 null 或 error 时:
- 打印失败
redisFree(context),context = nullptrfreeReplyObject(reply)- 返回
false
- 成功时释放
reply
- 若
-
切库(可选)
- 若
db != 0:redisCommand("SELECT %d", db)- 失败处理同上(释放 context + reply,返回 false)
- 成功释放
reply
- 若
-
设置全局实例
- 若
instance_ == nullptr,则instance_ = this
- 若
-
返回
true
风险点
freeReplyObject(reply)在某些失败路径上可能接收到nullptr,hiredis一般容忍,但最好显式判空。- 当前无连接超时与重连策略。
4. 读操作
std::string get(const std::string& key)
逻辑
- 若
context == nullptr:打印“未连接”,返回空字符串 - 执行
GET key - reply 为 null 或 error:打印失败,释放 reply,返回空字符串
- 按 reply 类型处理:
REDIS_REPLY_STRING:返回reply->strREDIS_REPLY_NIL:返回""(键不存在)- 其他类型:打印异常类型,返回
""
- 释放 reply
语义提醒
- “键不存在”与“命令失败”都可能返回空字符串,调用方需结合日志或后续
exists判断。
5. 写操作
5.1 bool set(const std::string& key, const std::string& value)
逻辑
- 检查连接,未连接返回 false
- 执行
SET key value - reply null 或 error:打印并返回 false
- 判断成功条件:
reply->type == REDIS_REPLY_STATUSreply->str == "OK"
- 失败时打印
SET 命令失败 - 释放 reply,返回
success
5.2 bool set(const std::string& key, const std::string& value, int expireSeconds)
逻辑
- 检查连接,未连接 false
- 校验
expireSeconds > 0,否则 false - 执行
SETEX key expireSeconds value - reply null 或 error -> false
- 成功条件同上(STATUS + “OK”)
- 释放 reply,返回结果
6. 存在性检查
bool exists(const std::string& key)
逻辑
- 检查连接
- 执行
EXISTS key - reply null 或 error -> false
- 若
reply->type == REDIS_REPLY_INTEGER:reply->integer == 1-> true- 否则 false
- 类型异常时打印告警
- 释放 reply
7. 删除操作
bool del(const std::string& key)
逻辑
- 检查连接
- 执行
DEL key - reply null -> false
- 若
REDIS_REPLY_ERROR-> false - 若类型不是
REDIS_REPLY_INTEGER-> false - 否则返回 true
- 释放 reply
语义说明
- 当前实现“DEL 返回整数类型即视为成功”,不区分删除 0 个还是 >0 个键。
8. 实例与错误读取接口
static RdConnector* getInstance()
- 直接返回
instance_ - 若未成功连接过,可能为
nullptr
std::string getError()
- 若
context == nullptr:返回"未初始化连接" - 否则返回
context->errstr
9. 并发与线程安全
当前类内部没有显式互斥保护:
- 多线程共享同一个
RdConnector/redisContext调用redisCommand可能有并发风险 - 建议每线程独立连接或引入连接池
10. 面试可讲的改进方向
- 引入连接池与自动重连
- 增加超时配置(连接/读写)
- 增加批量命令与 pipeline
- 明确返回语义(键不存在 vs 执行失败)
- 增强错误模型(错误码 + 文本)
下一模块(顺序)
Server → docs/modules/detail/Server_FUNCTIONS.md