![]() |
cpp_redis
4.0.0
cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client, with support for synchronous operations and pipelining.
|
#include <redis_connection.hpp>
Public Types | |
typedef std::function< void(redis_connection &)> | disconnection_handler_t |
typedef std::function< void(redis_connection &, reply &)> | reply_callback_t |
Public Member Functions | |
redis_connection (void) | |
ctor More... | |
redis_connection (const std::shared_ptr< tcp_client_iface > &tcp_client) | |
~redis_connection (void) | |
dtor More... | |
redis_connection (const redis_connection &)=delete | |
copy ctor More... | |
redis_connection & | operator= (const redis_connection &)=delete |
assignment operator More... | |
void | connect (const std::string &host="127.0.0.1", std::size_t port=6379, const disconnection_handler_t &disconnection_handler=nullptr, const reply_callback_t &reply_callback=nullptr, std::uint32_t timeout_msecs=0) |
void | disconnect (bool wait_for_removal=false) |
bool | is_connected (void) const |
redis_connection & | send (const std::vector< std::string > &redis_cmd) |
redis_connection & | commit (void) |
Private Member Functions | |
void | tcp_client_receive_handler (const tcp_client_iface::read_result &result) |
void | tcp_client_disconnection_handler (void) |
std::string | build_command (const std::vector< std::string > &redis_cmd) |
void | call_disconnection_handler (void) |
Private Attributes | |
std::shared_ptr< cpp_redis::network::tcp_client_iface > | m_client |
reply_callback_t | m_reply_callback |
disconnection_handler_t | m_disconnection_handler |
builders::reply_builder | m_builder |
std::string | m_buffer |
std::mutex | m_buffer_mutex |
tcp connection wrapper handling redis protocol
typedef std::function<void(redis_connection&)> cpp_redis::network::redis_connection::disconnection_handler_t |
disconnection handler takes as parameter the instance of the redis_connection
typedef std::function<void(redis_connection&, reply&)> cpp_redis::network::redis_connection::reply_callback_t |
reply handler takes as parameter the instance of the redis_connection and the built reply
cpp_redis::network::redis_connection::redis_connection | ( | void | ) |
ctor
|
explicit |
ctor allowing to specify custom tcp client (default ctor uses the default tacopie tcp client)
tcp_client | tcp client to be used for network communications |
cpp_redis::network::redis_connection::~redis_connection | ( | void | ) |
dtor
|
delete |
copy ctor
|
private |
transform a user command to a redis command using the redis protocol format for example, transform {"GET", "HELLO"} to something like "*2\r\n+GET\r\n+HELLO\r\n"
|
private |
simply call the disconnection handler (does nothing if disconnection handler is set to null)
redis_connection& cpp_redis::network::redis_connection::commit | ( | void | ) |
commit pipelined transaction that is, send to the network all commands pipelined by calling send()
void cpp_redis::network::redis_connection::connect | ( | const std::string & | host = "127.0.0.1" , |
std::size_t | port = 6379 , |
||
const disconnection_handler_t & | disconnection_handler = nullptr , |
||
const reply_callback_t & | reply_callback = nullptr , |
||
std::uint32_t | timeout_msecs = 0 |
||
) |
connect to the given host and port, and set both disconnection and reply callbacks
host | host to be connected to |
port | port to be connected to |
disconnection_handler | handler to be called in case of disconnection |
reply_callback | handler to be called once a reply is ready |
timeout_msecs | max time to connect (in ms) |
void cpp_redis::network::redis_connection::disconnect | ( | bool | wait_for_removal = false | ) |
disconnect from redis server
wait_for_removal | when sets to true, disconnect blocks until the underlying TCP client has been effectively removed from the io_service and that all the underlying callbacks have completed. |
bool cpp_redis::network::redis_connection::is_connected | ( | void | ) | const |
|
delete |
assignment operator
redis_connection& cpp_redis::network::redis_connection::send | ( | const std::vector< std::string > & | redis_cmd | ) |
send the given command the command is actually pipelined and only buffered, so nothing is sent to the network please call commit() to flush the buffer
redis_cmd | command to be sent |
|
private |
tcp_client disconnection handler called by the tcp_client whenever a disconnection occured
|
private |
tcp_client receive handler called by the tcp_client whenever a read has completed
result | read result |
|
private |
internal buffer used for pipelining (commands are buffered here and flushed to the tcp client when commit is called)
|
private |
protect internal buffer against race conditions
|
private |
reply builder used to build replies
|
private |
tcp client for redis connection
|
private |
disconnection handler whenever a disconnection occured
|
private |
reply callback called whenever a reply has been read