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

147 lines
4.7 KiB
C++
Raw Normal View History

2014-11-19 00:25:21 +08:00
#pragma once
2015-01-14 23:35:23 +08:00
#include "acl_cpp/acl_cpp_define.hpp"
2014-11-19 00:25:21 +08:00
#include <vector>
#include "acl_cpp/stream/socket_stream.hpp"
2015-01-14 23:35:23 +08:00
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/connpool/connect_client.hpp"
2014-11-19 00:25:21 +08:00
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;
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><EFBFBD>ܡ<EFBFBD>
* 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>
* constructor
* @param addr {const char*} redis-server <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
* the redis-server listening addr
* @param conn_timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> redis-server <EFBFBD>ij<EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD>)
* 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>)
* 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);
2014-11-19 00:25:21 +08:00
~redis_client();
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>
* @param pass {const char*}
* @return {redis_client&}
*/
void set_password(const char* pass);
2015-10-20 21:07: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><EFBFBD>Ѿ<EFBFBD><EFBFBD>ر<EFBFBD>
* check if the connection has been finish
* @return {bool}
*/
2015-01-29 00:38:29 +08:00
bool eof() const;
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* close the connection to the redis-server
*/
2014-11-19 00:25:21 +08:00
void close();
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>
* 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
* NULL will be returned if the connectioin has been closed
*/
socket_stream* get_stream();
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>
* 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>
* 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>
* 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>
* 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><EFBFBD><EFBFBD>
* 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>
* memory pool manager
* @param req {const string&} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݰ<EFBFBD>
* the request package
* @param nchildren {size_t} <EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><EFBFBD><EFBFBD>
* 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>
* 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);
2015-04-08 23:17:16 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD>Ƭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
* just for sending proccess in slice request mode
* @param req {const redis_request&} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><EFBFBD><EFBFBD>
* request object
*/
const redis_result* run(dbuf_pool* pool, const redis_request& req,
size_t nchildren);
2015-01-14 23:35:23 +08:00
protected:
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E9BAAF>
virtual bool open();
2014-11-19 00:25:21 +08:00
private:
socket_stream conn_;
char* addr_;
2015-10-20 21:07:23 +08:00
char* pass_;
2014-11-19 00:25:21 +08:00
bool retry_;
2015-01-14 23:35:23 +08:00
string buf_;
bool slice_req_;
bool slice_res_;
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);
2014-11-19 00:25:21 +08:00
};
} // end namespace acl