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体系

相关推荐
Komorebi.py1 小时前
【Linux】-学习笔记05
linux·笔记·学习
亦枫Leonlew1 小时前
微积分复习笔记 Calculus Volume 1 - 6.5 Physical Applications
笔记·数学·微积分
冰帝海岸6 小时前
01-spring security认证笔记
java·笔记·spring
小二·7 小时前
java基础面试题笔记(基础篇)
java·笔记·python
wusong99910 小时前
mongoDB回顾笔记(一)
数据库·笔记·mongodb
猫爪笔记10 小时前
前端:HTML (学习笔记)【1】
前端·笔记·学习·html
Resurgence0310 小时前
【计组笔记】习题
笔记
pq113_610 小时前
ftdi_sio应用学习笔记 3 - GPIO
笔记·学习·ftdi_sio
爱米的前端小笔记11 小时前
前端八股自学笔记分享—页面布局(二)
前端·笔记·学习·面试·求职招聘
寒笙LED14 小时前
C++详细笔记(六)string库
开发语言·c++·笔记