after the connection with redis-server is created, the dbnum can be selected in acl::redis_client, acl::redis_client_pool.

This commit is contained in:
zhengshuxin 2018-10-20 22:20:57 +08:00
parent 1ec33ddd2b
commit 7b343621e0
7 changed files with 142 additions and 30 deletions

View File

@ -1,5 +1,8 @@
修改历史列表:
545) 2018.10.21
545.1) feature: redis_client, redis_client_pool 在非集群模式下创建连接时自动选择db
544) 2018.10.20
544.1) bugfix: websocket::read_frame_data 被循环调用时,掩码使用方式有误
544.2) bugfix: WebSocketServlet.cpp 中有多个成员变量未初始化

View File

@ -13,9 +13,9 @@ class redis_result;
class redis_request;
/**
* redis redis redis
* redis connect_client 使
*
* redis redis redis
* redis connect_client
* 使
* redis client network IO class. The redis request is sent to server
* and the server's respond is handled in this class. The class inherits
* connect_client, which can use the connection pool function.
@ -44,6 +44,25 @@ public:
*/
void set_password(const char* pass);
/**
* db db 0
* db
* if db > 0 in no cluster mode, select the db when the connection
* is created.
* @param dbnum {int}
*/
void set_db(int dbnum);
/**
* db
* get db for the connection
* @return {int}
*/
int get_db(void) const
{
return dbnum_;
}
/**
* redis_client
* @return {const char*}
@ -66,13 +85,13 @@ public:
* check if the connection has been finish
* @return {bool}
*/
bool eof() const;
bool eof(void) const;
/**
*
* close the connection to the redis-server
*/
void close();
void close(void);
/**
*
@ -80,7 +99,7 @@ public:
* @return {acl::socket_stream*} NULL
* NULL will be returned if the connectioin has been closed
*/
socket_stream* get_stream();
socket_stream* get_stream(void);
/**
*
@ -137,18 +156,20 @@ public:
protected:
// 基类虚函数
virtual bool open();
// @override
virtual bool open(void);
private:
socket_stream conn_;
bool check_addr_;
char* addr_;
char* pass_;
bool retry_;
bool authing_;
string buf_;
bool slice_req_;
bool slice_res_;
bool check_addr_;
char* addr_;
char* pass_;
bool retry_;
bool authing_;
string buf_;
bool slice_req_;
bool slice_res_;
int dbnum_;
redis_result* get_redis_objects(dbuf_pool* pool, size_t nobjs);
redis_result* get_redis_object(dbuf_pool* pool);

View File

@ -37,16 +37,36 @@ public:
*/
redis_client_pool& set_password(const char* pass);
/**
* db
* in no-cluster mode, the method is used to select the db after
* the connection is created
* @param dbnum {int}
* @return {redis_client_pool&}
*/
redis_client_pool& set_db(int dbnum);
/**
* db
* get the current db of the connections pool
* @return {int}
*/
int get_db(void) const
{
return dbnum_;
}
protected:
/**
* :
* virtual function in class connect_pool to create a new connection
* @return {connect_client*}
*/
connect_client* create_connect();
connect_client* create_connect(void);
private:
char* pass_;
int dbnum_;
};
} // namespace acl

View File

@ -161,6 +161,7 @@ static void usage(const char* procname)
printf("usage: %s -h[help]\r\n"
"-s redis_addr[127.0.0.1:6379]\r\n"
"-p password[default: \"\"]\r\n"
"-d dbnum[default: 0]\r\n"
"-n count\r\n"
"-C connect_timeout[default: 10]\r\n"
"-T rw_timeout[default: 10]\r\n"
@ -170,10 +171,10 @@ static void usage(const char* procname)
int main(int argc, char* argv[])
{
int ch, n = 1, conn_timeout = 10, rw_timeout = 10;
int ch, n = 1, conn_timeout = 10, rw_timeout = 10, dbnum = 0;
acl::string addr("127.0.0.1:6379"), command, passwd;
while ((ch = getopt(argc, argv, "hs:n:C:T:a:p:")) > 0)
while ((ch = getopt(argc, argv, "hs:n:C:T:a:p:d:")) > 0)
{
switch (ch)
{
@ -198,6 +199,9 @@ int main(int argc, char* argv[])
case 'p':
passwd = optarg;
break;
case 'd':
dbnum = atoi(optarg);
break;
default:
break;
}
@ -205,8 +209,12 @@ int main(int argc, char* argv[])
acl::acl_cpp_init();
acl::log::stdout_open(true);
acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
client.set_password(passwd);
if (dbnum > 0)
client.set_db(dbnum);
acl::redis cmd(&client);
bool ret;

View File

@ -2,6 +2,24 @@
static acl::string __keypre("test_key");
static bool test_set(acl::redis_client* conn, int i)
{
acl::redis_string cmd(conn);
acl::string key, val;
key.format("%s_%d", __keypre.c_str(), i);
val.format("val_%d", i);
if (cmd.set(key, val) == false)
{
printf("set key: %s val: %s error: %s\r\n", key.c_str(),
val.c_str(), cmd.result_error());
return false;
}
else if (i < 10)
printf("set ok, key=%s, value=%s\r\n", key.c_str(), val.c_str());
return true;
}
static bool test_del(acl::redis_key& redis, int i)
{
acl::string key;
@ -115,7 +133,9 @@ protected:
redis.set_client(conn);
if (cmd_ == "del")
if (cmd_ == "set")
ret = test_set(conn, i);
else if (cmd_ == "del")
ret = test_del(redis, i);
else if (cmd_ == "expire")
ret = test_expire(redis, i);
@ -161,6 +181,8 @@ static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n"
"-s redis_addr[127.0.0.1:6379]\r\n"
"-p password[default: '']\r\n"
"-d dbnum[default: 0]\r\n"
"-n count[default: 10]\r\n"
"-C connect_timeout[default: 10]\r\n"
"-I rw_timeout[default: 10]\r\n"
@ -171,11 +193,11 @@ static void usage(const char* procname)
int main(int argc, char* argv[])
{
int ch, n = 1, conn_timeout = 10, rw_timeout = 10;
int ch, n = 1, conn_timeout = 10, rw_timeout = 10, dbnum = 0;
int max_threads = 10;
acl::string addr("127.0.0.1:6379"), cmd;
acl::string addr("127.0.0.1:6379"), cmd, passwd;
while ((ch = getopt(argc, argv, "hs:n:C:I:c:a:")) > 0)
while ((ch = getopt(argc, argv, "hs:n:C:I:c:a:p:d:")) > 0)
{
switch (ch)
{
@ -185,6 +207,12 @@ int main(int argc, char* argv[])
case 's':
addr = optarg;
break;
case 'p':
passwd = optarg;
break;
case 'd':
dbnum = atoi(optarg);
break;
case 'n':
n = atoi(optarg);
break;
@ -209,6 +237,10 @@ int main(int argc, char* argv[])
acl::redis_client_pool pool(addr.c_str(), max_threads);
pool.set_timeout(conn_timeout, rw_timeout);
if (!passwd.empty())
pool.set_password(passwd);
if (dbnum > 0)
pool.set_db(dbnum);
std::vector<test_thread*> threads;
for (int i = 0; i < max_threads; i++)

View File

@ -23,13 +23,14 @@ redis_client::redis_client(const char* addr, int conn_timeout /* = 60 */,
, authing_(false)
, slice_req_(false)
, slice_res_(false)
, dbnum_(0)
{
addr_ = acl_mystrdup(addr);
pass_ = NULL;
set_timeout(conn_timeout, rw_timeout);
}
redis_client::~redis_client()
redis_client::~redis_client(void)
{
acl_myfree(addr_);
if (pass_)
@ -54,7 +55,13 @@ void redis_client::set_password(const char* pass)
pass_ = NULL;
}
socket_stream* redis_client::get_stream()
void redis_client::set_db(int dbnum)
{
if (dbnum > 0)
dbnum_ = dbnum;
}
socket_stream* redis_client::get_stream(void)
{
if (conn_.opened())
return (socket_stream*) &conn_;
@ -86,7 +93,7 @@ bool redis_client::check_connection(socket_stream& conn)
return true;
}
bool redis_client::open()
bool redis_client::open(void)
{
if (conn_.opened())
return true;
@ -98,7 +105,7 @@ bool redis_client::open()
}
// 如果连接密码非空,则尝试用该密码向 redis-server 认证合法性
if (pass_ && *pass_ && !authing_)
if (pass_ && *pass_) // && !authing_)
{
// 设置当前连接的状态为认证状态,以避免进入死循环
authing_ = true;
@ -106,6 +113,7 @@ bool redis_client::open()
if (connection.auth(pass_) == false)
{
authing_ = false;
conn_.close();
logger_error("auth error, addr: %s, passwd: %s",
addr_, pass_);
return false;
@ -113,16 +121,26 @@ bool redis_client::open()
authing_ = false;
}
if (dbnum_ > 0)
{
redis_connection connection(this);
if (connection.select(dbnum_) == false)
{
conn_.close();
logger_error("select db error, db=%d", dbnum_);
return false;
}
}
return true;
}
void redis_client::close()
void redis_client::close(void)
{
if (conn_.opened())
conn_.close();
}
bool redis_client::eof() const
bool redis_client::eof(void) const
{
return conn_.eof();
}

View File

@ -11,10 +11,11 @@ redis_client_pool::redis_client_pool(const char* addr, size_t count,
size_t idx /* = 0 */)
: connect_pool(addr, count, idx)
, pass_(NULL)
, dbnum_(0)
{
}
redis_client_pool::~redis_client_pool()
redis_client_pool::~redis_client_pool(void)
{
if (pass_)
acl_myfree(pass_);
@ -31,12 +32,21 @@ redis_client_pool& redis_client_pool::set_password(const char* pass)
return *this;
}
connect_client* redis_client_pool::create_connect()
redis_client_pool& redis_client_pool::set_db(int dbnum)
{
if (dbnum > 0)
dbnum_ = dbnum;
return *this;
}
connect_client* redis_client_pool::create_connect(void)
{
redis_client* conn = NEW redis_client(addr_, conn_timeout_,
rw_timeout_);
if (pass_)
conn->set_password(pass_);
if (dbnum_ > 0)
conn->set_db(dbnum_);
return conn;
}