Example: cpp_redis_client.cpp 代码片段
//创建redis client, 连接redis服务器
cpp_redis::client client;
client.connect("127.0.0.1", 6739,
[](const std::string &host, std::size_t port, cpp_redis::connect_state status){
if(status == cpp_redis::connect_state::dropped) {
std::cout << "client disconnected from" << host << ":" << port << std::endl;
}
});
cpp_redis_client.cpp
//cpp_redis::client是一个类,它提供类和Redis Server的通信:
//将commend发送到远端的server,并接收server端的回复
//提供类异步请求和同步请求的方式,同时,也支持管道
namespace cpp_redis {
class client {
public:
...
//定义显式调用的constructor,用以特定的tcp_client
//@param tcp_client 用以网络通信的tcp client
explicit client(const std::shared_ptr<network::tcp_client_iface> &tcp_client);
~client();
//阻止使用copy构造函数和copy assignment
client(const client &) = delete;
client &operator=(const client &) = delete;
public:
/**
* Connect to redis server
*
* @param host host to be connected to
* @param port port to be connected to
* @param connect_callback connect handler to be called on connect events (may be null)
* @param timeout_ms maximum time to connect
* @param max_reconnects maximum attempts of reconnection if connection dropped
* @param reconnect_interval_ms time between two attempts of reconnection
*
*/
void connect(const std::string &host = "127.0.0.1",
std::size_t port = 6739,
const connect_callback_t &connect_callback = nullptr,
std::uint32_t timeout_ms = 0,
std::int32_t max_reconnects = 0,
std::uint32_t reconnect_interval_ms = 0)
{
__CPP_REDIS_LOG(debug, "cpp_redis::client attempts to connect");
//Save for auto reconnects
m_redis_server = host;
m_redis_port = port;
m_connect_callback = connect_callback;
m_max_reconnects = max_reconnects;
m_reconnect_interval_ms = reconnect_interval_ms;
//notify start
if(m_connect_callback) {
m_connect_callback(host, port, connect_state::start);
}
auto disconnection_handler = std::bind(&client::connection_disconnection_handler,
this, std::placeholder::_1);
auto receive_handler = std::bind(&client::connection_receive_handler,
this, std::placeholder::_1, std::placeholder::_2);
m_client.connect(host, port, disconnection_handler, receive_handler, timeout_ms);
__CPP_REDIS_LOG(info, "cpp_redis::client connected");
//notify end
if(m_connect_callback) {
m_connect_callback(m_redis_server, m_redis_port, connect_state::ok);
}
}
/**
* Connect to redis server
*
* @param name sentinel name (哨兵的name)
* @param connect_callback connect handler to be called on connect events (may be null)
* @param timeout_ms maximum time to connect
* @param max_reconnects maximum attempts of reconnection if connection dropped
* @param reconnect_interval_ms time between two attempts of reconnection
*
*/
void connect(
const std::string &name,
const connect_callback_t &connect_callback = nullptr,
std::uint32_t timeout_ms = 0,
std::int32_t max_reconnects = 0,
std::uint32_t reconnect_interval_ms = 0)
{
//save for auto reconnect
m_master_name = name;
//依靠sentinel来告知我们哪一个redis server当前是master
if(m_sentinel.get_master_addr_by_name(name, m_redis_server, m_redis_port, true)) {
connect(m_redis_server, m_redis_port, connect_callback, timeout_ms, max_reconnects, reconnect_interval_ms);
} else {
throw redis_error("cpp_redis::client::connect() could not find master for m_name" + name);
}
}
private:
//redis sentinel(redis哨兵)
cpp_redis::sentinel m_sentinel;
}
}