cpp_redis icon indicating copy to clipboard operation
cpp_redis copied to clipboard

Suggest better approach for connect and reconnect(when disconnected by server).

Open ashishsb95 opened this issue 7 years ago • 0 comments

Bear minimum code

Connect and reconnect code

`void RedisUtil::connect_callback(const std::string& host, std::size_t port, cpp_redis::client::connect_state status){ if (status == cpp_redis::client::connect_state::dropped) { std::unique_lockstd::mutex ulock(m_mutex); m_con_var.wait(ulock,std::bind(&RedisUtil::is_client_available,this)); release(); while(intialize(host,port)){}; } }

bool RedisUtil::intialize(const std::string &redis_host , const int redis_port ){ auto conn_call_wrap = std::bind( &RedisUtil::connect_callback,this,_1,_2,_3); redis_client.connect(redis_host, redis_port,conn_call_wrap); }`

Redis client get wrapper

           `std::lock_guard<std::mutex> guard(m_mutex);
	m_is_cli_avail = false;
	redis_client.get(key, getCallBack);
	redis_client.sync_commit();
	m_is_cli_avail = true;
	m_con_var.notify_one();`

I am looking for a better approach, any help will be gratefully welcome.

ashishsb95 avatar Oct 03 '18 13:10 ashishsb95