acl/lib_acl_cpp/include/acl_cpp/redis/redis_client.hpp

201 lines
6.1 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
2014-11-19 00:25:21 +08:00
#include <vector>
2017-06-02 14:47:24 +08:00
#include "../stream/socket_stream.hpp"
#include "../stdlib/string.hpp"
#include "../connpool/connect_client.hpp"
2014-11-19 00:25:21 +08:00
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
2015-01-14 23:35:23 +08:00
namespace acl
{
class dbuf_pool;
class redis_result;
2015-01-26 19:58:02 +08:00
class redis_request;
class polarssl_conf;
2014-11-19 00:25:21 +08:00
2015-03-29 19:27:41 +08:00
/**
* redis <EFBFBD>ͻ<EFBFBD><EFBFBD>˶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֯<EFBFBD>õ<EFBFBD> redis <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳<EFBFBD><EFBFBD><EFBFBD> connect_client <EFBFBD><EFBFBD><EFBFBD>Ҫ
* Ϊ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳع<EFBFBD><EFBFBD>ܡ<EFBFBD>
2015-03-29 19:27:41 +08:00
* 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.
*/
2015-01-14 23:35:23 +08:00
class ACL_CPP_API redis_client : public connect_client
2014-11-19 00:25:21 +08:00
{
public:
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* constructor
* @param addr {const char*} redis-server <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2015-04-08 23:17:16 +08:00
* the redis-server listening addr
* @param conn_timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis-server <EFBFBD>ij<EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)
2015-04-08 23:17:16 +08:00
* the timeout in seconds to connect the redis-server
* @param rw_timeout {int} <EFBFBD><EFBFBD> redis-server <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD>ŵ<EFBFBD> IO <EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)
2015-04-08 23:17:16 +08:00
* the network IO timeout in seconds with the redis-server
*/
2015-01-14 23:35:23 +08:00
redis_client(const char* addr, int conn_timeout = 60,
int rw_timeout = 30, bool retry = true);
~redis_client(void);
2014-11-19 00:25:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL ͨ<EFBFBD>ŷ<EFBFBD>ʽ<EFBFBD>µ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>þ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ȱʡֵΪ NULL<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> SSL <EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD> SSL ͨ<EFBFBD>ŷ<EFBFBD>ʽ
* set SSL communication with Redis-server if ssl_conf not NULL
* @param ssl_conf {polarssl_conf*}
*/
void set_ssl_conf(polarssl_conf* ssl_conf);
2015-10-20 21:07:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-10-20 21:07:23 +08:00
* @param pass {const char*}
*/
void set_password(const char* pass);
2015-10-20 21:07:23 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> db<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> db ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD>Զ<EFBFBD>
* ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD> db<EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD>ù<EFBFBD><EFBFBD>ܽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԷǼ<EFBFBD>Ⱥģʽ
* if db > 0 in no cluster mode, select the db when the connection
* is created.
* @param dbnum {int}
*/
void set_db(int dbnum);
/**
* <EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> db
* get db for the connection
* @return {int}
*/
int get_db(void) const
{
return dbnum_;
}
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>ӵķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis_client <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
* @return {const char*}
*/
const char* get_addr(void) const
{
return addr_;
}
/**
* <EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> socket <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD>򿪸<EFBFBD>ѡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӱ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˸<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ý<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> DEBUG ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>г<EFBFBD><EFBFBD><EFBFBD>
* @param on {bool}
*/
void set_check_addr(bool on);
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD>жϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>ر<EFBFBD>
2015-04-08 23:17:16 +08:00
* check if the connection has been finish
* @return {bool}
*/
bool eof(void) const;
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* close the connection to the redis-server
*/
void close(void);
2015-01-14 23:35:23 +08:00
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* get acl::socket_stream from the connection
* @return {acl::socket_stream*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD>򷵻<EFBFBD> NULL
2015-04-08 23:17:16 +08:00
* NULL will be returned if the connectioin has been closed
*/
socket_stream* get_stream(void);
2015-01-26 19:58:02 +08:00
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ϳ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* just for request package, setting flag for sending data with
* multi data chunks; this is useful when the request data is large
* @param on {bool} <EFBFBD><EFBFBD>Ϊ true ʱ<EFBFBD>򲻻<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺϳ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* if true the request data will not be combined one package
*/
void set_slice_request(bool on);
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD> redis-server <EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷֲ<EFBFBD><EFBFBD>ɶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Բ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԵĴ<EFBFBD><EFBFBD>ڴ<EFBFBD>
2015-04-08 23:17:16 +08:00
* just for response package, settint flag for receiving data
* if split the large response data into multi little chunks
* @param on {bool} <EFBFBD><EFBFBD>Ϊ true ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* if true the response data will be splitted into multi little
* data, which is useful for large reponse data for avoiding
* malloc large continuously memory from system.
*/
void set_slice_respond(bool on);
2015-01-14 23:35:23 +08:00
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ڷǷ<EFBFBD>Ƭ<EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis-server <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>ͬʱ<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD><EFBFBD>ص<EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* send request to redis-server, and read/anlyse response from server,
* this function will be used for no-slice request mode.
* @param pool {dbuf_pool*} <EFBFBD>ڴ<EFBFBD><EFBFBD>ع<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* memory pool manager
* @param req {const string&} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>
2015-04-08 23:17:16 +08:00
* the request package
* @param nchildren {size_t} <EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* the data object number in the server's response data
* @return {const redis_result*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>󣬷<EFBFBD><EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
* <EFBFBD>ö<EFBFBD><EFBFBD>󲻱<EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD>ͷţ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> pool <EFBFBD>ڴ<EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD>
* <EFBFBD>ͷ<EFBFBD> pool ʱ<EFBFBD>ý<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һͬ<EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>
2015-04-08 23:17:16 +08:00
* the result object from server's response, NULL will be returned
* when some error happens; the result object needn't be freed
* manually, which was created in the pool object, and will be freed
* when the pool were freed.
*
*/
const redis_result* run(dbuf_pool* pool, const string& req,
size_t nchildren, int* rw_timeout = NULL);
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD>Ƭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
2015-04-08 23:17:16 +08:00
* just for sending proccess in slice request mode
* @param req {const redis_request&} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><EFBFBD><EFBFBD>
2015-04-08 23:17:16 +08:00
* request object
*/
const redis_result* run(dbuf_pool* pool, const redis_request& req,
size_t nchildren, int* rw_timeout = NULL);
2015-01-14 23:35:23 +08:00
protected:
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BAAF>
// @override
bool open(void);
2015-01-14 23:35:23 +08:00
2014-11-19 00:25:21 +08:00
private:
socket_stream conn_;
bool check_addr_;
char* addr_;
char* pass_;
bool retry_;
bool authing_;
string buf_;
bool slice_req_;
bool slice_res_;
int dbnum_;
polarssl_conf* ssl_conf_;
redis_result* get_redis_objects(dbuf_pool* pool, size_t nobjs);
redis_result* get_redis_object(dbuf_pool* pool);
redis_result* get_redis_error(dbuf_pool* pool);
redis_result* get_redis_status(dbuf_pool* pool);
redis_result* get_redis_integer(dbuf_pool* pool);
redis_result* get_redis_string(dbuf_pool* pool);
redis_result* get_redis_array(dbuf_pool* pool);
2015-01-26 19:58:02 +08:00
void put_data(dbuf_pool* pool, redis_result* rr,
const char* data, size_t len);
bool check_connection(socket_stream& conn);
2014-11-19 00:25:21 +08:00
};
} // end namespace acl
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)