acl/lib_acl_cpp/include/acl_cpp/memcache/memcache.hpp

257 lines
9.0 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 <time.h>
2017-06-02 14:47:24 +08:00
#include "../connpool/connect_client.hpp"
#include "../stdlib/string.hpp"
#include "../mime/rfc2047.hpp"
2014-11-19 00:25:21 +08:00
#ifndef ACL_CLIENT_ONLY
2014-11-19 00:25:21 +08:00
namespace acl {
class socket_stream;
typedef class memcache mem_cache;
/**
* memcached
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API memcache : public connect_client
{
public:
/**
*
* @param addr {const char*} memcached
* ip:port: 127.0.0.1:11211
* @param conn_timeout {int} ()
* @param rw_timeout {int} IO ()
2014-11-19 00:25:21 +08:00
*/
memcache(const char* addr = "127.0.0.1:11211", int conn_timeout = 30,
int rw_timeout = 10);
2014-11-19 00:25:21 +08:00
~memcache();
/**
* key key +key
* memcached
* key key
* @param keypre {const char*} key key
2014-11-19 00:25:21 +08:00
* @return {memcache&}
*/
memcache& set_prefix(const char* keypre);
/**
*
* @param onoff {bool} true
2014-11-19 00:25:21 +08:00
* @return {memcache&}
*/
memcache& auto_retry(bool onoff);
/**
* KEY key key
* key
* @param onoff {bool} true key
2014-11-19 00:25:21 +08:00
* @return {memcache&}
*/
memcache& encode_key(bool onoff);
/**
* memcached
* @param key {const char*}
* @param klen {size_t} key
* @param dat {const void*}
* @param dlen {size_t} data
* @param timeout {time_t} ()
* @param flags {unsigned short}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool set(const char* key, size_t klen,
const void* dat, size_t dlen,
time_t timeout = 0, unsigned short flags = 0);
/**
* memcached
* @param key {const char*}
* @param dat {const void*}
* @param dlen {size_t} data
* @param timeout {time_t} ()
* @param flags {unsigned short}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool set(const char* key, const void* dat, size_t dlen,
time_t timeout = 0, unsigned short flags = 0);
/**
* memcached libmemcached
* get
* set
* @param key {const char*}
* @param klen {size_t} key
* @param timeout {time_t} ()
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool set(const char* key, size_t klen, time_t timeout = 0);
/**
* memcached libmemcached
* get
* set
* @param key {const char*}
* @param timeout {time_t} ()
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool set(const char* key, time_t timeout = 0);
/**
*
* @param key {const char*}
* @param dlen {size_t}
* @param timeout {time_t} ()
* @param flags {unsigned short}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool set_begin(const char* key, size_t dlen,
time_t timeout = 0, unsigned short flags = 0);
/**
*
* set_begin "\r\n"
*
* @param data {const void*}
* @param len {data} data
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool set_data(const void* data, size_t len);
/**
* memcached
* @param key {const char*}
* @param klen {size_t}
* @param buf {string&}
* @param flags {unsigned short*}
* @return {bool} true
*
2014-11-19 00:25:21 +08:00
*/
bool get(const char* key, size_t klen, string& buf,
unsigned short* flags = NULL);
/**
* memcached
* @param key {const char*}
* @param buf {string&}
* @param flags {unsigned short*}
* @return {bool} true
*
2014-11-19 00:25:21 +08:00
*/
bool get(const char* key, string& buf, unsigned short* flags = NULL);
/**
*
* @param key {const void*}
* @param klen {size_t} key
* @param flags {unsigned short*}
* @return {int}
* 0
* -1
* >0
2014-11-19 00:25:21 +08:00
*/
int get_begin(const void* key, size_t klen, unsigned short* flags = NULL);
/**
*
* @param key {const char*}
* @param flags {unsigned short*}
* @return {int}
* 0
* -1
* >0
2014-11-19 00:25:21 +08:00
*/
int get_begin(const char* key, unsigned short* flags = NULL);
/**
*
* @param buf {void*}
* @param size {size_t}
* @return {int}
* 0
* > 0:
* -1
2014-11-19 00:25:21 +08:00
*/
int get_data(void* buf, size_t size);
/**
* memcached
* @param key {const char*}
* @param klen {size_t}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool del(const char* key, size_t klen);
/**
* memcached
* @param key {const char*}
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
bool del(const char* key);
/**
* memcached
* @return {const char*}
2014-11-19 00:25:21 +08:00
*/
const char* last_serror() const;
/**
* memcached
* @return {int}
2014-11-19 00:25:21 +08:00
*/
int last_error() const;
/**
* memcached , set/get/del
* memcached memcached
*
* @return {bool}
2014-11-19 00:25:21 +08:00
*/
virtual bool open();
/**
* memcached
*
2014-11-19 00:25:21 +08:00
*/
void close();
/**
* memcached
2014-11-19 00:25:21 +08:00
*/
void property_list();
private:
bool set(const string& key, const void* dat, size_t dlen,
time_t timeout, unsigned short flags);
bool get(const string& key, string& buf, unsigned short* flags);
const string& build_key(const char* key, size_t klen);
string* keypre_; // 非空时,该字符串被添加在 KEY 值前组成新的 KEY
rfc2047 coder_; // 当需要对 KEY 编码时的编码器
bool encode_key_; // 是否需要对 KEY 进行编码
2014-11-19 00:25:21 +08:00
bool opened_; // 连接是否打开
bool retry_; // 是否支持连接中断重试
char* addr_; // 服务地址(ip:port)
int enum_; // 出错号,留作将来扩充用
string ebuf_; // 存储出错信息
string kbuf_; // 存储经转码后的 KEY 值缓冲区
2014-11-19 00:25:21 +08:00
size_t content_length_; // 当采用流式上传/下载大数据时此值记录数据体的总长度
size_t length_; // 已经上传/下载的数据总和
2014-11-19 00:25:21 +08:00
socket_stream* conn_; // 与后端服务的连接对象
string req_line_; // 存储请求数据
string res_line_; // 存储响应数据
2014-11-19 00:25:21 +08:00
bool error_happen(const char* line);
};
} // namespace acl
#endif // ACL_CLIENT_ONLY