Example: cpp_redis_client.cpp 代码片段

    1. //创建redis client, 连接redis服务器
    2. cpp_redis::client client;
    3. client.connect("127.0.0.1", 6739,
    4. [](const std::string &host, std::size_t port, cpp_redis::connect_state status){
    5. if(status == cpp_redis::connect_state::dropped) {
    6. std::cout << "client disconnected from" << host << ":" << port << std::endl;
    7. }
    8. });

    cpp_redis_client.cpp

    1. //cpp_redis::client是一个类,它提供类和Redis Server的通信:
    2. //将commend发送到远端的server,并接收server端的回复
    3. //提供类异步请求和同步请求的方式,同时,也支持管道
    4. namespace cpp_redis {
    5. class client {
    6. public:
    7. ...
    8. //定义显式调用的constructor,用以特定的tcp_client
    9. //@param tcp_client 用以网络通信的tcp client
    10. explicit client(const std::shared_ptr<network::tcp_client_iface> &tcp_client);
    11. ~client();
    12. //阻止使用copy构造函数和copy assignment
    13. client(const client &) = delete;
    14. client &operator=(const client &) = delete;
    15. public:
    16. /**
    17. * Connect to redis server
    18. *
    19. * @param host host to be connected to
    20. * @param port port to be connected to
    21. * @param connect_callback connect handler to be called on connect events (may be null)
    22. * @param timeout_ms maximum time to connect
    23. * @param max_reconnects maximum attempts of reconnection if connection dropped
    24. * @param reconnect_interval_ms time between two attempts of reconnection
    25. *
    26. */
    27. void connect(const std::string &host = "127.0.0.1",
    28. std::size_t port = 6739,
    29. const connect_callback_t &connect_callback = nullptr,
    30. std::uint32_t timeout_ms = 0,
    31. std::int32_t max_reconnects = 0,
    32. std::uint32_t reconnect_interval_ms = 0)
    33. {
    34. __CPP_REDIS_LOG(debug, "cpp_redis::client attempts to connect");
    35. //Save for auto reconnects
    36. m_redis_server = host;
    37. m_redis_port = port;
    38. m_connect_callback = connect_callback;
    39. m_max_reconnects = max_reconnects;
    40. m_reconnect_interval_ms = reconnect_interval_ms;
    41. //notify start
    42. if(m_connect_callback) {
    43. m_connect_callback(host, port, connect_state::start);
    44. }
    45. auto disconnection_handler = std::bind(&client::connection_disconnection_handler,
    46. this, std::placeholder::_1);
    47. auto receive_handler = std::bind(&client::connection_receive_handler,
    48. this, std::placeholder::_1, std::placeholder::_2);
    49. m_client.connect(host, port, disconnection_handler, receive_handler, timeout_ms);
    50. __CPP_REDIS_LOG(info, "cpp_redis::client connected");
    51. //notify end
    52. if(m_connect_callback) {
    53. m_connect_callback(m_redis_server, m_redis_port, connect_state::ok);
    54. }
    55. }
    56. /**
    57. * Connect to redis server
    58. *
    59. * @param name sentinel name (哨兵的name)
    60. * @param connect_callback connect handler to be called on connect events (may be null)
    61. * @param timeout_ms maximum time to connect
    62. * @param max_reconnects maximum attempts of reconnection if connection dropped
    63. * @param reconnect_interval_ms time between two attempts of reconnection
    64. *
    65. */
    66. void connect(
    67. const std::string &name,
    68. const connect_callback_t &connect_callback = nullptr,
    69. std::uint32_t timeout_ms = 0,
    70. std::int32_t max_reconnects = 0,
    71. std::uint32_t reconnect_interval_ms = 0)
    72. {
    73. //save for auto reconnect
    74. m_master_name = name;
    75. //依靠sentinel来告知我们哪一个redis server当前是master
    76. if(m_sentinel.get_master_addr_by_name(name, m_redis_server, m_redis_port, true)) {
    77. connect(m_redis_server, m_redis_port, connect_callback, timeout_ms, max_reconnects, reconnect_interval_ms);
    78. } else {
    79. throw redis_error("cpp_redis::client::connect() could not find master for m_name" + name);
    80. }
    81. }
    82. private:
    83. //redis sentinel(redis哨兵)
    84. cpp_redis::sentinel m_sentinel;
    85. }
    86. }