redisplusplus笔记

设计点

  • 在redis层使用函数作为模板类型参数,也就是command层定义的函数。
cpp 复制代码
template <typename Cmd, typename ...Args>
auto Redis::command(Cmd cmd, Args &&...args)
    -> typename std::enable_if<!std::is_convertible<Cmd, StringView>::value, ReplyUPtr>::type {
    if (_connection) {
        // Single Connection Mode.
        // TODO: In this case, should we reconnect?
        auto &connection = _connection->connection();
        if (connection.broken()) {
            throw Error("Connection is broken");
        }

        return _command(connection, cmd, std::forward<Args>(args)...);
    } else {
        assert(_pool);

        // Pool Mode, i.e. get connection from pool.
        SafeConnection connection(*_pool);

        return _command(connection.connection(), cmd, std::forward<Args>(args)...);
    }
}

template <typename ...Args>
auto Redis::command(const StringView &cmd_name, Args &&...args)
    -> typename std::enable_if<!IsIter<typename LastType<Args...>::type>::value, ReplyUPtr>::type {
    auto cmd = [](Connection &connection, const StringView &name, Args &&...params) {
                    CmdArgs cmd_args;
                    cmd_args.append(name, std::forward<Args>(params)...);
                    connection.send(cmd_args);
    };

    return command(cmd, cmd_name, std::forward<Args>(args)...);
}

template <typename Input>
auto Redis::command(Input first, Input last)
    -> typename std::enable_if<IsIter<Input>::value, ReplyUPtr>::type {
    range_check("command", first, last);

    auto cmd = [](Connection &connection, Input start, Input stop) {
                    CmdArgs cmd_args;
                    while (start != stop) {
                        cmd_args.append(*start);
                        ++start;
                    }
                    connection.send(cmd_args);
    };

    return command(cmd, first, last);
}
  • 使用可变参数模板
cpp 复制代码
template <typename Cmd, typename ...Args>
    ReplyUPtr _command(Connection &connection, Cmd cmd, Args &&...args);
//command系列
template <typename Cmd, typename ...Args>
    auto command(Cmd cmd, Args &&...args)
        -> typename std::enable_if<!std::is_convertible<Cmd, StringView>::value, ReplyUPtr>::type;
  • 在redis层最终会调用不带连接的command方法,调用带连接参数的_command方法(调用command层具体的函数,然后接收响应)
cpp 复制代码
template <typename Cmd, typename ...Args>
ReplyUPtr Redis::_command(Connection &connection, Cmd cmd, Args &&...args) {
    assert(!connection.broken());

    cmd(connection, std::forward<Args>(args)...);

    auto reply = connection.recv();

    return reply;
}

redis与连接

Redis处理命令

connection主要方法及与reply关系

connection只支持移动语义,不支持拷贝和赋值

recv使用ReplyUPtr,即unique_ptr<redisReply, ReplyDeleter>,其中ReplyDeleter定义如下

cpp 复制代码
struct ReplyDeleter {
    void operator()(redisReply *reply) const {
        if (reply != nullptr) {
            freeReplyObject(reply);
        }
    }
};

其调用redisGetReply获取响应,当中会处理以下几种异常情况

  • redisGetReply返回不是REDIS_OK,抛出异常
  • 调用broken判断连接是否断了
  • 在handle_error_reply标识为true,并且响应的type值为REDIS_REPLY_ERROR,抛出异常

send方法主要是调用redisAppendCommandArgv,会处理下面情况

  • redisAppendCommandArgv如果失败,会抛出异常
  • 调用broken()看连接是否断了
cpp 复制代码
   bool broken() const noexcept {
        return !_ctx || _ctx->err != REDIS_OK;
    }

连接池及其管理

SafeConnection:包含连接池的引用,构造函数负责从连接池中得到可用的连接,析构函数中将连接还到连接池中

GuardedConnection:使用ConnectionPoolSPtr _pool指向连接池的指针

连接池分配连接是通过ConnectionPool::fetch

  • 在池子中没有连接时,创建连接,并且连接使用计数+1
  • 池子中有时,从池子中取
cpp 复制代码
Connection ConnectionPool::fetch() {
    std::unique_lock<std::mutex> lock(_mutex);

    auto connection = _fetch(lock);

    auto connection_lifetime = _pool_opts.connection_lifetime;
    auto connection_idle_time = _pool_opts.connection_idle_time;

    if (_sentinel) {
        auto opts = _opts;
        auto role_changed = _role_changed(connection.options());
        auto sentinel = _sentinel;

        lock.unlock();

        if (role_changed || _need_reconnect(connection, connection_lifetime, connection_idle_time)) {
            try {
                connection = _create(sentinel, opts);
            } catch (const Error &) {
                // Failed to reconnect, return it to the pool, and retry latter.
                release(std::move(connection));
                throw;
            }
        }

        return connection;
    }

    lock.unlock();

    if (_need_reconnect(connection, connection_lifetime, connection_idle_time)) {
        try {
            connection.reconnect();
        } catch (const Error &) {
            // Failed to reconnect, return it to the pool, and retry latter.
            release(std::move(connection));
            throw;
        }
    }

    return connection;
}

Connection ConnectionPool::_fetch(std::unique_lock<std::mutex> &lock) {
    if (_pool.empty()) {
        if (_used_connections == _pool_opts.size) {
            _wait_for_connection(lock);
        } else {
            ++_used_connections;

            // Lazily create a new (broken) connection to avoid connecting with lock.
            return Connection(_opts, Connection::Dummy{});
        }
    }

    // _pool is NOT empty.
    return _fetch();
}

Connection ConnectionPool::_fetch() {
    assert(!_pool.empty());

    auto connection = std::move(_pool.front());
    _pool.pop_front();

    return connection;
}

连接释放到连接池通过ConnectionPool::release

cpp 复制代码
void ConnectionPool::release(Connection connection) {
    {
        std::lock_guard<std::mutex> lock(_mutex);

        _pool.push_back(std::move(connection));
    }

    _cv.notify_one();
}

解析响应

主要在reply.cpp和reply.h文件中

通过模板函数来作具体类型的的解析转发

cpp 复制代码
template <typename T>
inline T parse(redisReply &reply) {
    return parse(ParseTag<T>(), reply);
}

解析tag

cpp 复制代码
template <typename T>
struct ParseTag {};

optional解析

cpp 复制代码
template <typename T>
Optional<T> parse(ParseTag<Optional<T>>, redisReply &reply) {
    if (reply::is_nil(reply)) {
        // Because of a GCC bug, we cannot return {} for -std=c++17
        // Refer to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86465
#if defined REDIS_PLUS_PLUS_HAS_OPTIONAL
        return std::nullopt;
#else
        return {};
#endif
    }

    return Optional<T>(parse<T>(reply));
}

字符串解析

cpp 复制代码
std::string parse(ParseTag<std::string>, redisReply &reply) {
#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3
    if (!reply::is_string(reply) && !reply::is_status(reply)
            && !reply::is_verb(reply) && !reply::is_bignum(reply)) {
        throw ParseError("STRING or STATUS or VERB or BIGNUM", reply);
    }
#else
    if (!reply::is_string(reply) && !reply::is_status(reply)) {
        throw ParseError("STRING or STATUS", reply);
    }
#endif

    if (reply.str == nullptr) {
        throw ProtoError("A null string reply");
    }

    // Old version hiredis' *redisReply::len* is of type int.
    // So we CANNOT have something like: *return {reply.str, reply.len}*.
    return std::string(reply.str, reply.len);
}

解析void

主要是解析set命令类的

  • 看type是不是REDIS_REPLY_STATUS
  • str是否等于OK
cpp 复制代码
void parse(ParseTag<void>, redisReply &reply) {
    if (!reply::is_status(reply)) {
        throw ParseError("STATUS", reply);
    }

    if (reply.str == nullptr) {
        throw ProtoError("A null status reply");
    }

    static const std::string OK = "OK";

    // Old version hiredis' *redisReply::len* is of type int.
    // So we have to cast it to an unsigned int.
    if (static_cast<std::size_t>(reply.len) != OK.size()
            || OK.compare(0, OK.size(), reply.str, reply.len) != 0) {
        throw ProtoError("NOT ok status reply: " + reply::to_status(reply));
    }
}

long long类型解析

  • 检查type是不是REDIS_REPLY_INTEGER
cpp 复制代码
long long parse(ParseTag<long long>, redisReply &reply) {
    if (!reply::is_integer(reply)) {
        throw ParseError("INTEGER", reply);
    }

    return reply.integer;
}

double类型解析

  • 会区分协议版本,如果是resp3,检查类型是否是REDIS_REPLY_DOUBLE,如果是,则取dval,否则作字符串处理,然后转成double类型
  • 如果不是resp3,则字符串处理,转为double类型
cpp 复制代码
double parse(ParseTag<double>, redisReply &reply) {
#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3
    if (is_double(reply)) {
        return reply.dval;
    } else {
        // Return by string reply.
#endif
    try {
        return std::stod(parse<std::string>(reply));
    } catch (const std::invalid_argument &) {
        throw ProtoError("not a double reply");
    } catch (const std::out_of_range &) {
        throw ProtoError("double reply out of range");
    }
#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3
    }
#endif
}

bool类型解析

  • resp3协议时,判断类型是否是REDIS_REPLY_BOOL或者REDIS_REPLY_INTEGER
  • resp2协议时,当作long long类型来解析
  • 根据值是1,0来处理
cpp 复制代码
bool parse(ParseTag<bool>, redisReply &reply) {
#ifdef REDIS_PLUS_PLUS_RESP_VERSION_3
    long long ret = 0;
    if (is_bool(reply) || is_integer(reply)) {
        ret = reply.integer;
    } else {
        throw ProtoError("BOOL or INTEGER");
    }
#else
    auto ret = parse<long long>(reply);
#endif

    if (ret == 1) {
        return true;
    } else if (ret == 0) {
        return false;
    } else {
        throw ProtoError("Invalid bool reply: " + std::to_string(ret));
    }
}

error体系

相关推荐
·云扬·6 分钟前
Lambda 表达式详解
java·开发语言·笔记·学习·1024程序员节
厌世小晨宇yu.1 小时前
Git超详细笔记包含IDEA整合操作
笔记·git·intellij-idea
学生信的大叔1 小时前
Conpair: 配对样本一致性concordance与污染contamination分析
笔记
dal118网工任子仪2 小时前
web安全漏洞之命令注入
笔记·学习·安全·web安全·网络安全
java亮小白19973 小时前
ArcGIS软件之“计算面积几何”地图制作
经验分享·笔记
ljt27249606614 小时前
鸿蒙笔记--skills
笔记·harmonyos
mailangduoduo4 小时前
Web前端开发--HTML语言
前端·经验分享·笔记·html·爬虫前提知识
非概念10 小时前
常见的排序算法(二)
数据结构·笔记·算法·排序算法
傻傻虎虎11 小时前
【真题笔记】16年系统架构设计师要点总结
笔记·系统架构
萌面小侠Plus11 小时前
Android笔记(三十五):用责任链模式封装一个App首页Dialog管理工具
android·dialog·笔记·kotlin·责任链模式