创建Client实例
客户端工厂类
namespace pegasus {
class pegasus_client_factory
{
public:
///
/// \brief initialize
/// initialize pegasus client lib. must call this function before anything else.
/// \param config_file
/// the configuration file of client lib
/// \return
/// true indicate the initailize is success.
///
static bool initialize(const char *config_file);
///
/// \brief get_client
/// get an instance for a given cluster and a given app name.
/// \param cluster_name
/// the pegasus cluster name.
/// a cluster can have multiple apps.
/// \param app_name
/// an app is a logical isolated k-v store.
/// a cluster can have multiple apps.
/// \return
/// the client instance. DO NOT delete this client even after usage.
static pegasus_client *get_client(const char *cluster_name, const char *app_name);
};
} //end namespace
客户端的初始化,实际上是对rdsn框架初始化
if (!pegasus::pegasus_client_factory::initialize("config.ini")) {
fprintf(stderr, "ERROR: init pegasus failed\n");
return -1;
}
/**succeed, continue**/
注:
- 参数:config_file 见 配置文件 的介绍
- 返回值:bool值,true代表初始化成功,false初始化失败
- 该函数只需在一个进程生命周期内调用一次即可,并且非线程安全的获取客户端
pegasus::pegasus_client* pg_client = pegasus::pegasus_client_factory::get_client("cluster_name", "table_name");
if (pg_client == nullptr) {
fprintf(stderr, "ERROR: get pegasus client failed\n");
return -1;
}
/*** do what you want with pg_client ****/
注意: get_client返回值不能明确的调用delete,也不能用智能指针封装,框架在停止的时候自动释放(底层使用单例来保存)