rewrite some redis interface; fix bug for mysql lib on win32

This commit is contained in:
ubuntu14 2015-01-19 08:10:17 -08:00
parent b6bbade6e1
commit 1d09396c8c
29 changed files with 974 additions and 1029 deletions

View File

@ -1,6 +1,11 @@
修改历史列表:
------------------------------------------------------------------------
274) 2015.1.19
274.1) bugfix: db_mysql.cpp 在 WIN32 下使用 mysql 库采用了动态加载的方式,在函数指针
前应增加 STDCALL 宏定义
274.2) redis: 重构 redis 客户端库
273) 2015.1.17-1.18
273.1) feature: 实现了 list/pubsub/connection/transaction redist 客户端命令
273.2) feature: redis 客户端库中的 redis_key 类增加了 migrate/move 支持

View File

@ -142,3 +142,5 @@
#include "acl_cpp/redis/redis_list.hpp"
#include "acl_cpp/redis/redis_pubsub.hpp"
#include "acl_cpp/redis/redis_transaction.hpp"
#include "acl_cpp/redis/redis_set.hpp"
#include "acl_cpp/redis/redis_zset.hpp"

View File

@ -24,11 +24,35 @@ public:
}
void reset();
void close();
const redis_result* get_result() const
{
return result_;
}
const redis_result* run(const string& request, size_t nobjs = 1);
int get_number(const string& req, bool* success = NULL);
long long int get_number64(const string& req, bool* success = NULL);
bool get_status(const string& req, const char* success = "OK");
const char* get_status_string(const char* req);
int get_string(const string& req, string& buf);
int get_string(const string& req, string* buf);
int get_string(const string& req, char* buf, size_t size);
int get_strings(const string& req, std::vector<string>& result);
int get_strings(const string& req, std::map<string, string>& result);
int get_strings(const string& req, std::vector<string>& names,
std::vector<string>& values);
int get_strings(const string& req, std::vector<const char*>& names,
std::vector<const char*>& values);
const char* get_value(size_t i, size_t* len = NULL);
const redis_result* get_child(size_t i) const;
size_t get_size() const;
/*******************************************************************/
const string& build_request(size_t argc, const char* argv[],
size_t argv_lens[], string* buf = NULL);

View File

@ -0,0 +1,29 @@
#pragma once
#include "acl_cpp/acl_cpp_define.hpp"
namespace acl
{
class redis_client;
class redis_result;
class ACL_CPP_API redis_command
{
public:
redis_command(redis_client* conn = NULL);
virtual ~redis_command() = 0;
void reset();
void set_client(redis_client* conn);
redis_client* get_client() const
{
return conn_;
}
const redis_result* get_result() const;
protected:
redis_client* conn_;
};
} // namespace acl

View File

@ -1,5 +1,6 @@
#pragma once
#include "acl_cpp/acl_cpp_define.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
@ -7,23 +8,12 @@ namespace acl
class redis_client;
class redis_result;
class ACL_CPP_API redis_connection
class ACL_CPP_API redis_connection : public redis_command
{
public:
redis_connection(redis_client* conn = NULL);
~redis_connection();
const redis_result* get_result() const
{
return result_;
}
void set_client(redis_client* conn);
redis_client* get_client() const
{
return conn_;
}
/////////////////////////////////////////////////////////////////////
bool auth(const char* passwd);
@ -31,10 +21,6 @@ public:
bool ping();
bool echo(const char* s);
bool quit();
private:
redis_client* conn_;
const redis_result* result_;
};
} // namespace acl

View File

@ -3,6 +3,7 @@
#include <map>
#include <vector>
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
@ -10,26 +11,12 @@ namespace acl
class redis_client;
class redis_result;
class ACL_CPP_API redis_hash
class ACL_CPP_API redis_hash : public redis_command
{
public:
redis_hash(redis_client* conn = NULL);
~redis_hash();
void reset();
const redis_result* get_result() const
{
return result_;
}
void set_client(redis_client* conn);
redis_client* get_client() const
{
return conn_;
}
/////////////////////////////////////////////////////////////////////
/**
@ -131,14 +118,7 @@ public:
/////////////////////////////////////////////////////////////////////
private:
redis_client* conn_;
const redis_result* result_;
bool hmset(const string& req);
bool hmget(const string& req, std::vector<string>* result = NULL);
int hset(const string& req);
int hsetnx(const string& req);
int hdel(const string& req);
};
} // namespace acl

View File

@ -2,6 +2,7 @@
#include "acl_cpp/acl_cpp_define.hpp"
#include <vector>
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl {
@ -19,21 +20,12 @@ typedef enum
REDIS_KEY_ZSET // sorted set
} redis_key_t;
class ACL_CPP_API redis_key
class ACL_CPP_API redis_key : public redis_command
{
public:
redis_key(redis_client* conn = NULL);
~redis_key();
void reset();
void set_client(redis_client* conn);
redis_client* get_client() const
{
return conn_;
}
/**
* KEY
* @return {int} KEY
@ -89,12 +81,6 @@ public:
unsigned timeout, const char* option = NULL);
int move(const char* key, unsigned dest_db);
private:
redis_client* conn_;
const redis_result* result_;
int del(const string& req);
};
} // namespace acl

View File

@ -1,5 +1,6 @@
#pragma once
#include "acl_cpp/acl_cpp_define.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
@ -7,26 +8,12 @@ namespace acl
class redis_client;
class redis_result;
class ACL_CPP_API redis_list
class ACL_CPP_API redis_list : public redis_command
{
public:
redis_list(redis_client* conn = NULL);
~redis_list();
void reset();
const redis_result* get_result() const
{
return result_;
}
void set_client(redis_client* conn);
redis_client* get_client() const
{
return conn_;
}
/////////////////////////////////////////////////////////////////////
int llen(const char* key);
@ -99,12 +86,8 @@ public:
bool ltrim(const char* key, size_t start, size_t end);
private:
redis_client* conn_;
const redis_result* result_;
int linsert(const char* key, const char* pos, const char* pivot,
size_t pivot_len, const char* value, size_t value_len);
int push(const string& req);
int pushx(const char* cmd, const char* key,
const char* value, size_t len);
int pop(const char* cmd, const char* key, string& buf);
@ -113,7 +96,6 @@ private:
bool bpop(const char* cmd, const std::vector<string>& keys,
size_t timeout, std::pair<string, string>& result);
bool bpop(const string& req, std::pair<string, string>& result);
};
} // namespace acl

View File

@ -1,5 +1,6 @@
#pragma once
#include "acl_cpp/acl_cpp_define.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
@ -8,25 +9,12 @@ class redis_client;
class redis_result;
class string;
class ACL_CPP_API redis_pubsub
class ACL_CPP_API redis_pubsub : public redis_command
{
public:
redis_pubsub(redis_client* conn = NULL);
~redis_pubsub();
void reset();
void set_client(redis_client* conn);
redis_client* get_client() const
{
return conn_;
}
const redis_result* get_result() const
{
return result_;
}
/////////////////////////////////////////////////////////////////////
/**
@ -44,10 +32,7 @@ public:
bool get_message(string& channel, string& msg);
private:
redis_client* conn_;
const redis_result* result_;
int subscribe(const char* cmd, const std::vector<string>& channels);
int subop(const char* cmd, const std::vector<string>& channels);
int check_channel(const redis_result* obj, const char* cmd,
const string& channel);
};

View File

@ -0,0 +1,53 @@
#pragma once
#include "acl_cpp/acl_cpp_define.hpp"
#include <vector>
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
class redis_client;
class redis_result;
class ACL_CPP_API redis_set : public redis_command
{
public:
redis_set(redis_client* conn = NULL);
~redis_set();
/////////////////////////////////////////////////////////////////////
int sadd(const char* key, const char* first_member, ...);
int sadd(const char* key, const std::vector<const char*>& memsbers);
int sadd(const char* key, const std::vector<string>& members);
int sadd(const char* key, const char* argv[], size_t argc);
int sadd(const char* key, const char* argv[], const size_t lens[],
size_t argc);
bool spop(const char* key, string& buf);
int scard(const char* key);
int smembers(const char* key, std::vector<string>& members);
int smove(const char* src, const char* dst, const char* member);
int smove(const char* src, const char* dst, const string& member);
int smove(const char* src, const char* dst,
const char* member, size_t len);
int sinter(std::vector<string>& members, const char* first_key, ...);
int sinter(const std::vector<const char*>& keys,
std::vector<string>& members);
int sinter(const std::vector<string>& keys,
std::vector<string>& members);
int sdiff(std::vector<string>& members, const char* first_key, ...);
int sdiff(const std::vector<const char*>& keys,
std::vector<string>& members);
int sdiff(const std::vector<string>& keys,
std::vector<string>& members);
int sdiffstore(const char* dst, const char* first_key, ...);
int sdiffstore(const char* dst, const std::vector<const char*>& keys);
int sdiffstore(const char* dst, const std::vector<string>& keys);
};
} // namespace acl

View File

@ -2,6 +2,7 @@
#include "acl_cpp/acl_cpp_define.hpp"
#include <map>
#include <vector>
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
@ -10,26 +11,12 @@ class string;
class redis_client;
class redis_result;
class ACL_CPP_API redis_string
class ACL_CPP_API redis_string : public redis_command
{
public:
redis_string(redis_client* conn = NULL);
~redis_string();
void reset();
const redis_result* get_result() const
{
return result_;
}
void set_client(redis_client* conn);
redis_client* get_client() const
{
return conn_;
}
/////////////////////////////////////////////////////////////////////
bool set(const char* key, const char* value);
@ -129,22 +116,22 @@ public:
/////////////////////////////////////////////////////////////////////
bool mget(const std::vector<string>& keys,
std::vector<string>* result = NULL);
std::vector<string>* out = NULL);
bool mget(const std::vector<const char*>& keys,
std::vector<string>* result = NULL);
std::vector<string>* out = NULL);
bool mget(const std::vector<char*>& keys,
std::vector<string>* result = NULL);
std::vector<string>* out = NULL);
bool mget(const std::vector<int>& keys,
std::vector<string>* result = NULL);
std::vector<string>* out = NULL);
bool mget(std::vector<string>* result, const char* first_key, ...)
ACL_CPP_PRINTF(3, 4);;
bool mget(const char* keys[], size_t argc,
std::vector<string>* result = NULL);
std::vector<string>* out = NULL);
bool mget(const int keys[], size_t argc,
std::vector<string>* result = NULL);
std::vector<string>* out = NULL);
bool mget(const char* keys[], const size_t keys_len[], size_t argc,
std::vector<string>* result = NULL);
std::vector<string>* out = NULL);
size_t mget_size() const;
const char* mget_value(size_t i, size_t* len = NULL) const;
@ -167,19 +154,13 @@ public:
/////////////////////////////////////////////////////////////////////
private:
redis_client* conn_;
const redis_result* result_;
int bitop(const char* op, const char* destkey,
const std::vector<string>& keys);
int bitop(const char* op, const char* destkey,
const std::vector<const char*>& keys);
int bitop(const char* op, const char* destkey,
const char* keys[], size_t size);
int bitop(const string& req);
bool mset(const string& req);
int msetnx(const string& req);
bool mget(const string& req, std::vector<string>* result = NULL);
bool mget(const string& req, std::vector<string>* out = NULL);
};
} // namespace acl

View File

@ -2,6 +2,7 @@
#include "acl_cpp/acl_cpp_define.hpp"
#include <vector>
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
@ -9,25 +10,12 @@ namespace acl
class redis_client;
class redis_result;
class ACL_CPP_API redis_transaction
class ACL_CPP_API redis_transaction : public redis_command
{
public:
redis_transaction(redis_client* conn = NULL);
~redis_transaction();
void reset();
void set_client(redis_client* conn);
redis_client* get_client() const
{
return conn_;
}
const redis_result* get_result() const
{
return result_;
}
/////////////////////////////////////////////////////////////////////
bool watch(const std::vector<string>& keys);
@ -38,12 +26,10 @@ public:
bool queue_cmd(const char* cmd, const char* argv[],
const size_t lens[], size_t argc);
size_t get_size();
const redis_result* get_child(size_t i, string* cmd);
size_t get_size() const;
const redis_result* get_child(size_t i, string* cmd) const;
private:
redis_client* conn_;
const redis_result* result_;
std::vector<string> cmds_;
};

View File

@ -0,0 +1,18 @@
#pragma once
#include "acl_cpp/acl_cpp_define.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
class redis_client;
class redis_result;
class ACL_CPP_API redis_zset : public redis_command
{
public:
redis_zset(redis_client* conn = NULL);
~redis_zset();
};
} // namespace acl

View File

@ -301,6 +301,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClCompile Include="src\queue\queue_manager.cpp" />
<ClCompile Include="src\redis\redic_connection.cpp" />
<ClCompile Include="src\redis\redis_client.cpp" />
<ClCompile Include="src\redis\redis_command.cpp" />
<ClCompile Include="src\redis\redis_hash.cpp" />
<ClCompile Include="src\redis\redis_key.cpp" />
<ClCompile Include="src\redis\redis_list.cpp" />
@ -308,8 +309,10 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClCompile Include="src\redis\redis_pool.cpp" />
<ClCompile Include="src\redis\redis_pubsub.cpp" />
<ClCompile Include="src\redis\redis_result.cpp" />
<ClCompile Include="src\redis\redis_set.cpp" />
<ClCompile Include="src\redis\redis_string.cpp" />
<ClCompile Include="src\redis\redis_transaction.cpp" />
<ClCompile Include="src\redis\redis_zset.cpp" />
<ClCompile Include="src\session\memcache_session.cpp" />
<ClCompile Include="src\session\session.cpp" />
<ClCompile Include="src\stdlib\charset_conv.cpp" />
@ -458,6 +461,7 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\queue\queue_file.hpp" />
<ClInclude Include="include\acl_cpp\queue\queue_manager.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_client.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_command.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_connection.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_hash.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_key.hpp" />
@ -466,8 +470,10 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
<ClInclude Include="include\acl_cpp\redis\redis_pool.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_pubsub.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_result.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_set.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_string.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_transaction.hpp" />
<ClInclude Include="include\acl_cpp\redis\redis_zset.hpp" />
<ClInclude Include="include\acl_cpp\session\memcache_session.hpp" />
<ClInclude Include="include\acl_cpp\session\session.hpp" />
<ClInclude Include="include\acl_cpp\stdlib\charset_conv.hpp" />

View File

@ -448,6 +448,15 @@
<ClCompile Include="src\redis\redis_transaction.cpp">
<Filter>src\redis</Filter>
</ClCompile>
<ClCompile Include="src\redis\redis_set.cpp">
<Filter>src\redis</Filter>
</ClCompile>
<ClCompile Include="src\redis\redis_zset.cpp">
<Filter>src\redis</Filter>
</ClCompile>
<ClCompile Include="src\redis\redis_command.cpp">
<Filter>src\redis</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\acl_stdafx.hpp">
@ -990,6 +999,15 @@
<ClInclude Include="include\acl_cpp\redis\redis_transaction.hpp">
<Filter>include\redis</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\redis\redis_set.hpp">
<Filter>include\redis</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\redis\redis_zset.hpp">
<Filter>include\redis</Filter>
</ClInclude>
<ClInclude Include="include\acl_cpp\redis\redis_command.hpp">
<Filter>include\redis</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="changes.txt" />

View File

@ -3,7 +3,7 @@
ProjectType="Visual C++"
Version="9.00"
Name="redis_string"
ProjectGUID="{70296D43-9E0F-46D7-8C2B-40A960D31D50}"
ProjectGUID="{2DABFAD1-114B-4F96-9185-DC0C56A3662D}"
Keyword="Win32Proj"
TargetFrameworkVersion="131072"
>
@ -342,10 +342,6 @@
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath=".\redis_string.cpp"
>
</File>
<File
RelativePath=".\stdafx.cpp"
>
@ -382,6 +378,10 @@
/>
</FileConfiguration>
</File>
<File
RelativePath=".\redis_string.cpp"
>
</File>
</Filter>
<Filter
Name="Í·Îļþ"
@ -404,4 +404,6 @@
>
</File>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@ -19,7 +19,7 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{58580DE9-8034-4B8E-B9F2-67A538E98FB8}</ProjectGuid>
<ProjectGuid>{2DABFAD1-114B-4F96-9185-DC0C56A3662D}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

View File

@ -3,35 +3,45 @@
#include "mysql.h"
#include "errmsg.h"
#include "mysqld_error.h"
#include "acl_cpp/stdlib/snprintf.hpp"
#include "acl_cpp/stdlib/log.hpp"
#include "acl_cpp/db/db_mysql.hpp"
//////////////////////////////////////////////////////////////////////////
#ifdef HAS_MYSQL
#if defined(ACL_CPP_DLL) || defined(USE_DYNAMIC)
typedef unsigned long (*mysql_libversion_fn)(void);
typedef const char* (*mysql_client_info_fn)(void);
typedef MYSQL* (*mysql_init_fn)(MYSQL*);
typedef MYSQL* (*mysql_open_fn)(MYSQL*, const char*, const char*,
#ifndef STDCALL
# ifdef WIN32
# define STDCALL __stdcall
# else
# define STDCALL
# endif // WIN32
#endif // STDCALL
typedef unsigned long (STDCALL *mysql_libversion_fn)(void);
typedef const char* (STDCALL *mysql_client_info_fn)(void);
typedef MYSQL* (STDCALL *mysql_init_fn)(MYSQL*);
typedef MYSQL* (STDCALL *mysql_open_fn)(MYSQL*, const char*, const char*,
const char*, const char*, unsigned int,
const char*, unsigned long);
typedef void (*mysql_close_fn)(MYSQL*);
typedef int (*mysql_options_fn)(MYSQL*,enum mysql_option option, const void*);
typedef my_bool (*mysql_autocommit_fn)(MYSQL*, my_bool);
typedef unsigned int (*mysql_errno_fn)(MYSQL*);
typedef const char* (*mysql_error_fn)(MYSQL*);
typedef int (*mysql_query_fn)(MYSQL*, const char*);
typedef unsigned int (*mysql_num_fields_fn)(MYSQL_RES*);
typedef MYSQL_FIELD* (*mysql_fetch_fields_fn)(MYSQL_RES*);
typedef MYSQL_ROW (*mysql_fetch_row_fn)(MYSQL_RES*);
typedef MYSQL_RES* (*mysql_store_result_fn)(MYSQL*);
typedef my_ulonglong (*mysql_num_rows_fn)(MYSQL_RES*);
typedef void (*mysql_free_result_fn)(MYSQL_RES*);
typedef my_ulonglong (*mysql_affected_rows_fn)(MYSQL*);
typedef int (*mysql_set_character_set_fn)(MYSQL*, const char*);
typedef const char* (*mysql_character_set_name_fn)(MYSQL*);
typedef void (STDCALL *mysql_close_fn)(MYSQL*);
typedef int (STDCALL *mysql_options_fn)(MYSQL*,enum mysql_option option,
const void*);
typedef my_bool (STDCALL *mysql_autocommit_fn)(MYSQL*, my_bool);
typedef unsigned int (STDCALL *mysql_errno_fn)(MYSQL*);
typedef const char* (STDCALL *mysql_error_fn)(MYSQL*);
typedef int (STDCALL *mysql_query_fn)(MYSQL*, const char*);
typedef unsigned int (STDCALL *mysql_num_fields_fn)(MYSQL_RES*);
typedef MYSQL_FIELD* (STDCALL *mysql_fetch_fields_fn)(MYSQL_RES*);
typedef MYSQL_ROW (STDCALL *mysql_fetch_row_fn)(MYSQL_RES*);
typedef MYSQL_RES* (STDCALL *mysql_store_result_fn)(MYSQL*);
typedef my_ulonglong (STDCALL *mysql_num_rows_fn)(MYSQL_RES*);
typedef void (STDCALL *mysql_free_result_fn)(MYSQL_RES*);
typedef my_ulonglong (STDCALL *mysql_affected_rows_fn)(MYSQL*);
typedef int (STDCALL *mysql_set_character_set_fn)(MYSQL*, const char*);
typedef const char* (STDCALL *mysql_character_set_name_fn)(MYSQL*);
static mysql_libversion_fn __mysql_libversion = NULL;
static mysql_client_info_fn __mysql_client_info = NULL;

View File

@ -2,14 +2,13 @@
#include "acl_cpp/stdlib/string.hpp"
#include "acl_cpp/stdlib/snprintf.hpp"
#include "acl_cpp/redis/redis_client.hpp"
#include "acl_cpp/redis/redis_result.hpp"
#include "acl_cpp/redis/redis_connection.hpp"
namespace acl
{
redis_connection::redis_connection(redis_client* conn /* = NULL */)
: conn_(conn)
: redis_command(conn)
{
}
@ -19,11 +18,6 @@ redis_connection::~redis_connection()
}
void redis_connection::set_client(redis_client* conn)
{
conn_ = conn;
}
bool redis_connection::auth(const char* passwd)
{
const char* argv[2];
@ -36,16 +30,7 @@ bool redis_connection::auth(const char* passwd)
lens[1] = strlen(argv[1]);
const string& req = conn_->build_request(2, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* res = result_->get(0);
if (res == NULL || strcasecmp(res, "OK") != 0)
return false;
else
return true;
return conn_->get_status(req);
}
bool redis_connection::select(int dbnum)
@ -62,15 +47,7 @@ bool redis_connection::select(int dbnum)
lens[1] = strlen(argv[1]);
const string& req = conn_->build_request(2, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* res = result_->get(0);
if (res == NULL || strcasecmp(res, "ok") != 0)
return false;
return true;
return conn_->get_status(req);
}
bool redis_connection::ping()
@ -82,15 +59,7 @@ bool redis_connection::ping()
lens[0] = strlen(argv[0]);
const string& req = conn_->build_request(1, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* res = result_->get(0);
if (res == NULL || strcasecmp(res, "PONG") != 0)
return false;
return true;
return conn_->get_status(req, "PONG");
}
bool redis_connection::echo(const char* s)
@ -104,18 +73,9 @@ bool redis_connection::echo(const char* s)
argv[1] = s;
lens[1] = strlen(argv[1]);
const string& req = conn_->build_request(2, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STRING)
return false;
string buf;
result_->argv_to_string(buf);
if (buf != s)
return false;
else
return true;
const string& req = conn_->build_request(2, argv, lens);
return conn_->get_string(req, buf) >= 0 ? true : false;
}
bool redis_connection::quit()
@ -127,17 +87,9 @@ bool redis_connection::quit()
lens[0] = strlen(argv[0]);
const string& req = conn_->build_request(1, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* res = result_->get(0);
if (res == NULL || strcasecmp(res, "OK") != 0)
return false;
bool ret = conn_->get_status(req);
conn_->close();
return true;
return ret;
}
} // namespace acl

View File

@ -90,6 +90,8 @@ void redis_client::close()
conn_.close();
}
/////////////////////////////////////////////////////////////////////////////
redis_result* redis_client::get_error()
{
buf_.clear();
@ -286,6 +288,273 @@ const redis_result* redis_client::run(const string& request,
return NULL;
}
//////////////////////////////////////////////////////////////////////////
int redis_client::get_number(const string& req, bool* success /* = NULL */)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_INTEGER)
{
if (success)
*success = false;
return -1;
}
if (success)
*success = true;
return result->get_integer();
}
long long int redis_client::get_number64(const string& req,
bool* success /* = NULL */)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_INTEGER)
{
if (success)
*success = false;
return -1;
}
if (success)
*success = true;
return result->get_integer64();
}
bool redis_client::get_status(const string& req, const char* success /* = "OK" */)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result->get_status();
if (status == NULL || strcasecmp(status, success) != 0)
return false;
return true;
}
const char* redis_client::get_status_string(const char* req)
{
const redis_result* result = run(req);
return result == NULL ? NULL : result->get_status();
}
int redis_client::get_string(const string& req, string& buf)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_STRING)
return -1;
return (int) result->argv_to_string(buf);
}
int redis_client::get_string(const string& req, string* buf)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_STRING)
return -1;
if (buf == NULL)
return (int) result->get_length();
return (int) result->argv_to_string(*buf);
}
int redis_client::get_string(const string& req, char* buf, size_t size)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_STRING)
return -1;
return (int) result->argv_to_string(buf, size);
}
int redis_client::get_strings(const string& req, std::vector<string>& out)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
return -1;
size_t size;
const redis_result** children = result->get_children(&size);
if (children == NULL)
return 0;
const redis_result* rr;
string buf;
for (size_t i = 0; i < size; i++)
{
rr = children[i];
if (rr->get_type() != REDIS_RESULT_STRING)
continue;
rr->argv_to_string(buf);
out.push_back(buf);
buf.clear();
}
return (int) size;
}
int redis_client::get_strings(const string& req,
std::map<string, string>& out)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
return -1;
if (result->get_size() == 0)
return 0;
size_t size;
const redis_result** children = result->get_children(&size);
if (children == NULL)
return -1;
if (size % 2 != 0)
return -1;
string name_buf, value_buf;
const redis_result* rr;
for (size_t i = 0; i < size;)
{
rr = children[i];
if (rr->get_type() != REDIS_RESULT_STRING)
{
i += 2;
continue;
}
name_buf.clear();
value_buf.clear();
rr->argv_to_string(name_buf);
i++;
rr->argv_to_string(value_buf);
i++;
out[name_buf] = value_buf;
}
return (int) out.size();
}
int redis_client::get_strings(const string& req, std::vector<string>& names,
std::vector<string>& values)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
return -1;
if (result->get_size() == 0)
return 0;
size_t size;
const redis_result** children = result->get_children(&size);
if (children == NULL)
return -1;
if (size % 2 != 0)
return -1;
string buf;
const redis_result* rr;
for (size_t i = 0; i < size;)
{
rr = children[i];
if (rr->get_type() != REDIS_RESULT_STRING)
{
i += 2;
continue;
}
buf.clear();
rr->argv_to_string(buf);
i++;
names.push_back(buf);
buf.clear();
rr->argv_to_string(buf);
i++;
values.push_back(buf);
}
return (int) names.size();
}
int redis_client::get_strings(const string& req,
std::vector<const char*>& names, std::vector<const char*>& values)
{
const redis_result* result = run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
return -1;
if (result->get_size() == 0)
return 0;
size_t size;
const redis_result** children = result->get_children(&size);
if (children == NULL)
return -1;
if (size % 2 != 0)
return -1;
char* buf;
size_t len;
const redis_result* rr;
std::vector<const redis_result*>::const_iterator cit;
for (size_t i = 0; i < size;)
{
rr = children[i];
if (rr->get_type() != REDIS_RESULT_STRING)
{
i += 2;
continue;
}
len = rr->get_length() + 1;
buf = (char*) pool_->dbuf_alloc(len);
rr->argv_to_string(buf, len);
i++;
names.push_back(buf);
len = rr->get_length() + 1;
buf = (char*) pool_->dbuf_alloc(len);
rr->argv_to_string(buf, len);
i++;
values.push_back(buf);
}
return (int) names.size();
}
const char* redis_client::get_value(size_t i, size_t* len /* = NULL */)
{
if (result_ == NULL || result_->get_type() != REDIS_RESULT_ARRAY)
return NULL;
const redis_result* child = result_->get_child(i);
if (child == NULL)
return NULL;
size_t size = child->get_size();
if (size == 0)
return NULL;
if (size == 1)
return child->get(0, len);
// 大内存有可能被切片成多个不连续的小内存
size = child->get_length();
size++;
char* buf = (char*) pool_->dbuf_alloc(size);
size = child->argv_to_string(buf, size);
if (len)
*len = size;
return buf;
}
const redis_result* redis_client::get_child(size_t i) const
{
if (result_ == NULL || result_->get_type() != REDIS_RESULT_ARRAY)
return NULL;
return result_->get_child(i);
}
size_t redis_client::get_size() const
{
if (result_ == NULL || result_->get_type() != REDIS_RESULT_ARRAY)
return 0;
return result_->get_size();
}
//////////////////////////////////////////////////////////////////////////
const string& redis_client::build_request(size_t argc, const char* argv[],
size_t argv_lens[], string* buf /* = NULL */)
{
@ -350,9 +619,6 @@ const string& redis_client::build_request(const std::vector<const char*>& args,
/////////////////////////////////////////////////////////////////////////////
/***************************************************************************/
/***************************************************************************/
const string& redis_client::build(const char* cmd, const char* key,
const std::map<string, string>& attrs, string* buf /* = NULL */)
{
@ -955,10 +1221,6 @@ const string& redis_client::build(const char* cmd, const char* key,
/////////////////////////////////////////////////////////////////////////////
/***************************************************************************/
/* for other request */
/***************************************************************************/
const string& redis_client::build(const char* cmd, const char* key,
const std::vector<string>& names, string* buf /* = NULL */)
{

View File

@ -0,0 +1,35 @@
#include "acl_stdafx.hpp"
#include "acl_cpp/redis/redis_client.hpp"
#include "acl_cpp/redis/redis_command.hpp"
namespace acl
{
redis_command::redis_command(redis_client* conn /* = NULL */)
: conn_(conn)
{
}
redis_command::~redis_command()
{
}
void redis_command::reset()
{
if (conn_)
conn_->reset();
}
void redis_command::set_client(redis_client* conn)
{
conn_ = conn;
}
const redis_result* redis_command::get_result() const
{
return conn_ ? conn_->get_result() : NULL;
}
} // namespace acl

View File

@ -1,7 +1,6 @@
#include "acl_stdafx.hpp"
#include "acl_cpp/stdlib/snprintf.hpp"
#include "acl_cpp/redis/redis_client.hpp"
#include "acl_cpp/stdlib/dbuf_pool.hpp"
#include "acl_cpp/redis/redis_result.hpp"
#include "acl_cpp/redis/redis_hash.hpp"
@ -12,8 +11,7 @@ namespace acl
#define FLOAT_LEN 32
redis_hash::redis_hash(redis_client* conn /* = NULL */)
: conn_(conn)
, result_(NULL)
: redis_command(conn)
{
}
@ -22,64 +20,42 @@ redis_hash::~redis_hash()
{
}
void redis_hash::reset()
{
if (conn_ != NULL)
conn_->reset();
}
void redis_hash::set_client(redis_client* conn)
{
conn_ = conn;
}
/////////////////////////////////////////////////////////////////////////////
bool redis_hash::hmset(const char* key, const std::map<string, string>& attrs)
{
const string& req = conn_->build("HMSET", key, attrs);
return hmset(req);
return conn_->get_status(req);
}
bool redis_hash::hmset(const char* key, const std::map<string, char*>& attrs)
{
const string& req = conn_->build("HMSET", key, attrs);
return hmset(req);
return conn_->get_status(req);
}
bool redis_hash::hmset(const char* key, const std::map<string, const char*>& attrs)
{
const string& req = conn_->build("HMSET", key, attrs);
return hmset(req);
return conn_->get_status(req);
}
bool redis_hash::hmset(const char* key, const std::map<int, string>& attrs)
{
const string& req = conn_->build("HMSET", key, attrs);
return hmset(req);
return conn_->get_status(req);
}
bool redis_hash::hmset(const char* key, const std::map<int, char*>& attrs)
{
const string& req = conn_->build("HMSET", key, attrs);
return hmset(req);
return conn_->get_status(req);
}
bool redis_hash::hmset(const char* key, const std::map<int, const char*>& attrs)
{
const string& req = conn_->build("HMSET", key, attrs);
return hmset(req);
}
bool redis_hash::hmset(const string& req)
{
result_ = conn_->run(req);
if (result_ == NULL)
return false;
const char* res = result_->get(0);
if (res == NULL || strcasecmp(res, "ok") != 0)
return false;
return true;
return conn_->get_status(req);
}
/////////////////////////////////////////////////////////////////////////////
@ -133,18 +109,15 @@ bool redis_hash::hmget(const char* key, const char* names[],
return hmget(req, result);
}
bool redis_hash::hmget(const string& req, std::vector<string>* result /* = NULL */)
bool redis_hash::hmget(const string& req, std::vector<string>* out /* = NULL */)
{
result_ = conn_->run(req);
if (result_ == NULL)
const redis_result* rr = conn_->run(req);
if (rr == NULL || rr->get_type() != REDIS_RESULT_ARRAY)
return false;
if (result_->get_type() != REDIS_RESULT_ARRAY)
return false;
if (result == NULL)
if (out == NULL)
return true;
size_t size = hmget_size();
const redis_result* rr;
size_t nslice, len;
const char* ptr;
string buf(4096);
@ -153,18 +126,18 @@ bool redis_hash::hmget(const string& req, std::vector<string>* result /* = NULL
{
rr = hmget_result(i);
if (rr == NULL || (nslice = rr->get_size()) == 0)
result->push_back("");
out->push_back("");
else if (nslice == 1)
{
ptr = rr->get(0, &len);
buf.copy(ptr, len);
result->push_back(buf);
out->push_back(buf);
}
else
{
buf.clear();
rr->argv_to_string(buf);
result->push_back(buf);
out->push_back(buf);
}
}
@ -173,45 +146,17 @@ bool redis_hash::hmget(const string& req, std::vector<string>* result /* = NULL
const redis_result* redis_hash::hmget_result(size_t i) const
{
if (result_ == NULL)
return NULL;
if (result_->get_type() != REDIS_RESULT_ARRAY)
return NULL;
return result_->get_child(i);
return conn_->get_child(i);
}
const char* redis_hash::hmget_value(size_t i, size_t* len /* = NULL */) const
{
if (result_ == NULL)
return NULL;
if (result_->get_type() != REDIS_RESULT_ARRAY)
return NULL;
const redis_result* rr = result_->get_child(i);
if (rr == NULL)
return NULL;
size_t size = rr->get_size();
if (size == 0)
return NULL;
if (size == 1)
return rr->get(0, len);
// 大内存有可能被切片成多个不连续的小内存
size = rr->get_length();
size++;
char* buf = (char*) conn_->get_pool()->dbuf_alloc(size);
size = rr->argv_to_string(buf, size);
if (len)
*len = size;
return buf;
return conn_->get_value(i, len);
}
size_t redis_hash::hmget_size() const
{
if (result_ == NULL)
return 0;
if (result_->get_type() != REDIS_RESULT_ARRAY)
return 0;
return result_->get_size();
return conn_->get_size();
}
/////////////////////////////////////////////////////////////////////////////
@ -243,23 +188,7 @@ int redis_hash::hset(const char* key, const char* name, size_t name_len,
lens[3] = value_len;
const string& req = conn_->build_request(4, argv, lens);
return hset(req);
}
int redis_hash::hset(const string& req)
{
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
bool success;
int ret = result_->get_integer(&success);
if (!success)
return -1;
if (ret != 0 && ret != 1)
return -1;
return ret;
return conn_->get_number(req);
}
int redis_hash::hsetnx(const char* key, const char* name, const char* value)
@ -289,21 +218,7 @@ int redis_hash::hsetnx(const char* key, const char* name, size_t name_len,
lens[3] = value_len;
const string& req = conn_->build_request(4, argv, lens);
return hsetnx(req);
}
int redis_hash::hsetnx(const string& req)
{
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
bool success;
int ret = result_->get_integer(&success);
if (!success)
return -1;
return ret;
return conn_->get_number(req);
}
bool redis_hash::hget(const char* key, const char* name, string& result)
@ -325,13 +240,7 @@ bool redis_hash::hget(const char* key, const char* name,
lens[2] = name_len;
const string& req = conn_->build_request(3, argv, lens);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return false;
if (rr->get_type() != REDIS_RESULT_STRING)
return false;
rr->argv_to_string(result);
return true;
return conn_->get_string(req, result) >= 0 ? true : false;
}
bool redis_hash::hgetall(const char* key, std::map<string, string>& result)
@ -339,40 +248,7 @@ bool redis_hash::hgetall(const char* key, std::map<string, string>& result)
const char* keys[1];
keys[0] = key;
const string& req = conn_->build("HGETALL", NULL, keys, 1);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return false;
if (rr->get_type() != REDIS_RESULT_ARRAY)
return false;
if (rr->get_size() == 0)
return true;
size_t size;
const redis_result** children = rr->get_children(&size);
if (children == NULL)
return false;
if (size % 2 != 0)
return false;
string name_buf, value_buf;
for (size_t i = 0; i < size;)
{
rr = children[i];
if (rr->get_type() != REDIS_RESULT_STRING)
{
i += 2;
continue;
}
name_buf.clear();
value_buf.clear();
rr->argv_to_string(name_buf);
i++;
rr->argv_to_string(value_buf);
i++;
result[name_buf] = value_buf;
}
return true;
return conn_->get_strings(req, result) < 0 ? false : true;
}
bool redis_hash::hgetall(const char* key, std::vector<string>& names,
@ -381,43 +257,7 @@ bool redis_hash::hgetall(const char* key, std::vector<string>& names,
const char* keys[1];
keys[0] = key;
const string& req = conn_->build("HGETALL", NULL, keys, 1);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return false;
if (rr->get_type() != REDIS_RESULT_ARRAY)
return false;
if (rr->get_size() == 0)
return true;
size_t size;
const redis_result** children = rr->get_children(&size);
if (children == NULL)
return false;
if (size % 2 != 0)
return false;
string buf;
for (size_t i = 0; i < size;)
{
rr = children[i];
if (rr->get_type() != REDIS_RESULT_STRING)
{
i += 2;
continue;
}
buf.clear();
rr->argv_to_string(buf);
i++;
names.push_back(buf);
buf.clear();
rr->argv_to_string(buf);
i++;
values.push_back(buf);
}
return true;
return conn_->get_strings(req, names, values) < 0 ? false : true;
}
bool redis_hash::hgetall(const char* key, std::vector<const char*>& names,
@ -426,48 +266,7 @@ bool redis_hash::hgetall(const char* key, std::vector<const char*>& names,
const char* keys[1];
keys[0] = key;
const string& req = conn_->build("HGETALL", NULL, keys, 1);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return false;
if (rr->get_type() != REDIS_RESULT_ARRAY)
return false;
if (rr->get_size() == 0)
return true;
size_t size;
const redis_result** children = rr->get_children(&size);
if (children == NULL)
return false;
if (size % 2 != 0)
return false;
char* buf;
size_t len;
dbuf_pool* pool = conn_->get_pool();
std::vector<const redis_result*>::const_iterator cit;
for (size_t i = 0; i < size;)
{
rr = children[i];
if (rr->get_type() != REDIS_RESULT_STRING)
{
i += 2;
continue;
}
len = rr->get_length() + 1;
buf = (char*) pool->dbuf_alloc(len);
rr->argv_to_string(buf, len);
i++;
names.push_back(buf);
len = rr->get_length() + 1;
buf = (char*) pool->dbuf_alloc(len);
rr->argv_to_string(buf, len);
i++;
values.push_back(buf);
}
return true;
return conn_->get_strings(req, names, values) < 0 ? false : true;
}
int redis_hash::hdel(const char* key, const char* first_name, ...)
@ -485,84 +284,73 @@ int redis_hash::hdel(const char* key, const char* first_name, ...)
int redis_hash::hdel(const char* key, const char* names[], size_t argc)
{
const string& req = conn_->build("HDEL", key, names, argc);
return hdel(req);
return conn_->get_number(req);
}
int redis_hash::hdel(const char* key, const char* names[],
const size_t names_len[], size_t argc)
{
const string& req = conn_->build("HDEL", key, names, names_len, argc);
return hdel(req);;
return conn_->get_number(req);;
}
int redis_hash::hdel(const char* key, const std::vector<string>& names)
{
const string& req = conn_->build("HDEL", key, names);
return hdel(req);
return conn_->get_number(req);
}
int redis_hash::hdel(const char* key, const std::vector<char*>& names)
{
const string& req = conn_->build("HDEL", key, names);
return hdel(req);
return conn_->get_number(req);
}
int redis_hash::hdel(const char* key, const std::vector<const char*>& names)
{
const string& req = conn_->build("HDEL", key, names);
return hdel(req);
}
int redis_hash::hdel(const string& req)
{
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
bool redis_hash::hincrby(const char* key, const char* name,
long long int inc, long long int* result /* = NULL */)
{
const char* names[1];
names[0] = name;
char buf[INT64_LEN];
(void) acl_i64toa(inc, buf, sizeof(buf));
const char* values[1];
values[0] = buf;
const string& req = conn_->build("HINCRBY", key, names, values, 1);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return false;
bool success;
if (result != NULL)
*result = result_->get_integer64();
return true;
*result = conn_->get_number64(req, &success);
else
(void) conn_->get_number64(req, &success);
return success;
}
bool redis_hash::hincrbyfloat(const char* key, const char* name,
double inc, double* result /* = NULL */)
{
const char* names[1];
const char* values[1];
names[0] = name;
char buf[FLOAT_LEN];
(void) safe_snprintf(buf, sizeof(buf), "%f", inc);
const char* values[1];
values[0] = buf;
const string& req = conn_->build("HINCRBYFLOAT", key, names, values, 1);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STRING)
const string& req = conn_->build("HINCRBYFLOAT", key, names,
values, 1);
if (conn_->get_string(req, buf, sizeof(buf)) == false)
return false;
if (result != NULL)
{
(void) result_->argv_to_string(buf, sizeof(buf));
*result = atof(buf);
}
return true;
}
@ -572,33 +360,9 @@ bool redis_hash::hkeys(const char* key, std::vector<string>& names)
{
const char* keys[1];
keys[0] = key;
const string& req = conn_->build("HKEYS", NULL, keys, 1);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_ARRAY)
return false;
size_t size;
const redis_result** children = result_->get_children(&size);
if (children == NULL)
return false;
string buf;
const redis_result* rr;
for (size_t i = 0; i < size; i++)
{
rr = children[i];
if (rr->get_type() != REDIS_RESULT_STRING)
continue;
buf.clear();
rr->argv_to_string(buf);
names.push_back(buf);
}
return true;
return conn_->get_strings(req, names) < 0 ? false : true;
}
bool redis_hash::hexists(const char* key, const char* name)
@ -614,29 +378,16 @@ bool redis_hash::hexists(const char* key, const char* name, size_t name_len)
names_len[0] = name_len;
const string& req = conn_->build("HEXISTS", key, names, names_len, 1);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return false;
if (rr->get_type() != REDIS_RESULT_INTEGER)
return false;
int ret = rr->get_integer();
if (ret == 1)
return true;
else
return false;
return conn_->get_number(req) < 0 ? false : true;
}
int redis_hash::hlen(const char* key)
{
const char* keys[1];
keys[0] = key;
const string& req = conn_->build("HLEN", NULL, keys, 1);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return -1;
if (rr->get_type() != REDIS_RESULT_INTEGER)
return -1;
return rr->get_integer();
return conn_->get_number(req);
}
/////////////////////////////////////////////////////////////////////////////

View File

@ -2,7 +2,6 @@
#include "acl_cpp/stdlib/snprintf.hpp"
#include "acl_cpp/stdlib/log.hpp"
#include "acl_cpp/redis/redis_client.hpp"
#include "acl_cpp/redis/redis_result.hpp"
#include "acl_cpp/redis/redis_key.hpp"
namespace acl
@ -11,7 +10,7 @@ namespace acl
#define INT_LEN 11
redis_key::redis_key(redis_client* conn /* = NULL */)
: conn_(conn)
: redis_command(conn)
{
}
@ -21,17 +20,6 @@ redis_key::~redis_key()
}
void redis_key::reset()
{
if (conn_)
conn_->reset();
}
void redis_key::set_client(redis_client* conn)
{
conn_ = conn;
}
/////////////////////////////////////////////////////////////////////////////
int redis_key::del(const char* first_key, ...)
@ -51,49 +39,43 @@ int redis_key::del(const char* first_key, ...)
int redis_key::del(const std::vector<string>& keys)
{
const string& req = conn_->build("DEL", NULL, keys);
return del(req);
return conn_->get_number(req);
}
int redis_key::del(const std::vector<char*>& keys)
{
const string& req = conn_->build("DEL", NULL, keys);
return del(req);
return conn_->get_number(req);
}
int redis_key::del(const std::vector<const char*>& keys)
{
const string& req = conn_->build("DEL", NULL, keys);
return del(req);
return conn_->get_number(req);
}
int redis_key::del(const std::vector<int>& keys)
{
const string& req = conn_->build("DEL", NULL, keys);
return del(req);
return conn_->get_number(req);
}
int redis_key::del(const char* keys[], size_t argc)
{
const string& req = conn_->build("DEL", NULL, keys, argc);
return del(req);
return conn_->get_number(req);
}
int redis_key::del(const int keys[], size_t argc)
{
const string& req = conn_->build("DEL", NULL, keys, argc);
return del(req);
return conn_->get_number(req);
}
int redis_key::del(const char* keys[], const size_t lens[], size_t argc)
{
const string& req = conn_->build("DEL", NULL, keys, lens, argc);
return del(req);
}
int redis_key::del(const string& req)
{
result_ = conn_->run(req);
return result_ == NULL ? -1 : result_->get_integer();
return conn_->get_number(req);
}
/////////////////////////////////////////////////////////////////////////////
@ -107,10 +89,7 @@ int redis_key::expire(const char* key, int n)
argv[1] = buf;
const string& req = conn_->build("EXPIRE", NULL, argv, 2);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return -1;
return rr->get_integer();
return conn_->get_number(req);
}
int redis_key::ttl(const char* key)
@ -119,16 +98,13 @@ int redis_key::ttl(const char* key)
argv[0] = key;
const string& req = conn_->build("TTL", NULL, argv, 1);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return -1;
bool success;
int ret = rr->get_integer(&success);
if (!success)
int ret = conn_->get_number(req, &success);
if (success == false)
return -1;
if (ret < 0)
return 0;
return ret;
else
return ret;
}
bool redis_key::exists(const char* key)
@ -137,10 +113,7 @@ bool redis_key::exists(const char* key)
keys[0] = key;
const string& req = conn_->build("EXISTS", NULL, keys, 1);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
return false;
return rr->get_integer() > 0 ? true : false;
return conn_->get_number(req) > 0 ? true : false;
}
redis_key_t redis_key::type(const char* key)
@ -149,14 +122,7 @@ redis_key_t redis_key::type(const char* key)
keys[0] = key;
const string& req = conn_->build("TYPE", NULL, keys, 1);
const redis_result* rr = conn_->run(req);
if (rr == NULL)
{
logger_error("result null");
return REDIS_KEY_UNKNOWN;
}
const char* ptr = rr->get_status();
const char* ptr = conn_->get_status_string(req);
if (ptr == NULL)
return REDIS_KEY_UNKNOWN;
@ -221,13 +187,7 @@ bool redis_key::migrate(const char* key, const char* addr, unsigned dest_db,
}
const string& req = conn_->build_request(argc, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "OK") != 0)
return false;
return true;
return conn_->get_status(req);
}
int redis_key::move(const char* key, unsigned dest_db)
@ -246,10 +206,7 @@ int redis_key::move(const char* key, unsigned dest_db)
lens[2] = strlen(db_s);
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
/////////////////////////////////////////////////////////////////////////////

View File

@ -13,8 +13,7 @@ namespace acl
#define LONG_LEN 21
redis_list::redis_list(redis_client* conn /* = NULL */)
: conn_(conn)
, result_(NULL)
: redis_command(conn)
{
}
@ -24,17 +23,6 @@ redis_list::~redis_list()
}
void redis_list::reset()
{
if (conn_)
conn_->reset();
}
void redis_list::set_client(redis_client* conn)
{
conn_ = conn;
}
//////////////////////////////////////////////////////////////////////////
int redis_list::llen(const char* key)
@ -48,12 +36,7 @@ int redis_list::llen(const char* key)
lens[1] = strlen(key);
const string& req = conn_->build_request(2, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
bool redis_list::lindex(const char* key, size_t idx, string& buf,
@ -76,14 +59,10 @@ bool redis_list::lindex(const char* key, size_t idx, string& buf,
lens[2] = strlen(tmp);
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STRING)
return false;
if (result_->argv_to_string(buf) > 0 && exist != NULL)
long ret = conn_->get_string(req, buf);
if (exist && ret > 0)
*exist = true;
return true;
return ret >= 0 ? true : false;
}
bool redis_list::lset(const char* key, size_t idx, const char* value)
@ -111,16 +90,7 @@ bool redis_list::lset(const char* key, size_t idx,
lens[3] = len;
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* res = result_->get_status();
if (res == NULL || strcasecmp(res, "OK") != 0)
return false;
else
return true;
return conn_->get_status(req);
}
int redis_list::linsert_before(const char* key, const char* pivot,
@ -165,10 +135,7 @@ int redis_list::linsert(const char* key, const char* pos, const char* pivot,
lens[4] = value_len;
const string& req = conn_->build_request(5, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
int redis_list::lpush(const char* key, const char* first_value, ...)
@ -190,32 +157,32 @@ int redis_list::lpush(const char* key, const char* first_value, ...)
int redis_list::lpush(const char* key, const char* values[], size_t argc)
{
const string& req = conn_->build("LPUSH", key, values, argc);
return push(req);
return conn_->get_number(req);
}
int redis_list::lpush(const char* key, const std::vector<string>& values)
{
const string& req = conn_->build("LPUSH", key, values);
return push(req);
return conn_->get_number(req);
}
int redis_list::lpush(const char* key, const std::vector<char*>& values)
{
const string& req = conn_->build("LPUSH", key, values);
return push(req);
return conn_->get_number(req);
}
int redis_list::lpush(const char* key, const std::vector<const char*>& values)
{
const string& req = conn_->build("LPUSH", key, values);
return push(req);
return conn_->get_number(req);
}
int redis_list::lpush(const char* key, const char* values[],
size_t lens[], size_t argc)
{
const string& req = conn_->build("LPUSH", key, values, lens, argc);
return push(req);
return conn_->get_number(req);
}
int redis_list::rpush(const char* key, const char* first_value, ...)
@ -237,42 +204,32 @@ int redis_list::rpush(const char* key, const char* first_value, ...)
int redis_list::rpush(const char* key, const char* values[], size_t argc)
{
const string& req = conn_->build("RPUSH", key, values, argc);
return push(req);
return conn_->get_number(req);
}
int redis_list::rpush(const char* key, const std::vector<string>& values)
{
const string& req = conn_->build("RPUSH", key, values);
return push(req);
return conn_->get_number(req);
}
int redis_list::rpush(const char* key, const std::vector<char*>& values)
{
const string& req = conn_->build("RPUSH", key, values);
return push(req);
return conn_->get_number(req);
}
int redis_list::rpush(const char* key, const std::vector<const char*>& values)
{
const string& req = conn_->build("RPUSH", key, values);
return push(req);
return conn_->get_number(req);
}
int redis_list::rpush(const char* key, const char* values[],
size_t lens[], size_t argc)
{
const string& req = conn_->build("RPUSH", key, values, lens, argc);
return push(req);
}
int redis_list::push(const string& req)
{
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
int redis_list::lpushx(const char* key, const char* value)
@ -309,12 +266,7 @@ int redis_list::pushx(const char* cmd, const char* key,
lens[2] = len;
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
//////////////////////////////////////////////////////////////////////////
@ -340,13 +292,7 @@ int redis_list::pop(const char* cmd, const char* key, string& buf)
lens[1] = strlen(key);
const string& req = conn_->build_request(2, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_STRING)
return -1;
return result_->argv_to_string(buf);
return (int) conn_->get_string(req, buf);
}
bool redis_list::blpop(std::pair<string, string>& result, size_t timeout,
@ -477,21 +423,21 @@ bool redis_list::bpop(const char* cmd, const std::vector<string>& keys,
return bpop(req, result);
}
bool redis_list::bpop(const string& req, std::pair<string, string>& result)
bool redis_list::bpop(const string& req, std::pair<string, string>& out)
{
result_ = conn_->run(req);
if (result_ == NULL)
const redis_result* result = conn_->run(req);
if (result == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_ARRAY)
if (result->get_type() != REDIS_RESULT_ARRAY)
return false;
size_t size = result_->get_size();
size_t size = result->get_size();
if (size == 0)
return false;
if (size != 2)
return false;
const redis_result* first = result_->get_child(0);
const redis_result* second = result_->get_child(1);
const redis_result* first = result->get_child(0);
const redis_result* second = result->get_child(1);
if (first == NULL || second == NULL
|| first->get_type() != REDIS_RESULT_STRING
|| second->get_type() != REDIS_RESULT_STRING)
@ -501,11 +447,11 @@ bool redis_list::bpop(const string& req, std::pair<string, string>& result)
string buf;
first->argv_to_string(buf);
result.first = buf;
out.first = buf;
buf.clear();
second->argv_to_string(buf);
result.second = buf;
out.second = buf;
return true;
}
@ -523,13 +469,7 @@ bool redis_list::rpoplpush(const char* src, const char* dst,
lens[2] = strlen(dst);
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STRING)
return false;
if (buf == NULL)
return true;
result_->argv_to_string(*buf);
return true;
return conn_->get_string(req, buf) >= 0 ? true : false;
}
bool redis_list::brpoplpush(const char* src, const char* dst,
@ -551,13 +491,7 @@ bool redis_list::brpoplpush(const char* src, const char* dst,
lens[3] = strlen(argv[3]);
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STRING)
return false;
if (buf == NULL)
return true;
result_->argv_to_string(*buf);
return true;
return conn_->get_string(req, buf) >= 0 ? true : false;
}
bool redis_list::lrange(const char* key, size_t start, size_t end,
@ -581,25 +515,7 @@ bool redis_list::lrange(const char* key, size_t start, size_t end,
lens[3] = strlen(end_s);
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_ARRAY)
return false;
size_t size;
const redis_result** children = result_->get_children(&size);
if (children == NULL)
return true;
string buf;
for (size_t i = 0; i < size; i++)
{
const redis_result* rr = children[i];
rr->argv_to_string(buf);
result.push_back(buf);
buf.clear();
}
return true;
return conn_->get_strings(req, result) < 0 ? false : true;
}
int redis_list::lrem(const char* key, int count, const char* value)
@ -626,10 +542,7 @@ int redis_list::lrem(const char* key, int count, const char* value, size_t len)
lens[3] = len;
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
bool redis_list::ltrim(const char* key, size_t start, size_t end)
@ -652,13 +565,7 @@ bool redis_list::ltrim(const char* key, size_t start, size_t end)
lens[3] = strlen(end_s);
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "OK") != 0)
return false;
return true;
return conn_->get_status(req);
}
//////////////////////////////////////////////////////////////////////////

View File

@ -8,7 +8,7 @@ namespace acl
{
redis_pubsub::redis_pubsub(redis_client* conn /* = NULL */)
: conn_(conn)
: redis_command(conn)
{
}
@ -18,17 +18,6 @@ redis_pubsub::~redis_pubsub()
}
void redis_pubsub::reset()
{
if (conn_)
conn_->reset();
}
void redis_pubsub::set_client(redis_client* conn)
{
conn_ = conn;
}
int redis_pubsub::publish(const char* channel, const char* msg, size_t len)
{
const char* argv[3];
@ -42,11 +31,7 @@ int redis_pubsub::publish(const char* channel, const char* msg, size_t len)
lens[2] = len;
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
int redis_pubsub::subscribe(const char* first_channel, ...)
@ -65,7 +50,7 @@ int redis_pubsub::subscribe(const char* first_channel, ...)
int redis_pubsub::subscribe(const std::vector<string>& channels)
{
return subscribe("SUBSCRIBE", channels);
return subop("SUBSCRIBE", channels);
}
int redis_pubsub::unsubscribe(const char* first_channel, ...)
@ -84,11 +69,10 @@ int redis_pubsub::unsubscribe(const char* first_channel, ...)
int redis_pubsub::unsubscribe(const std::vector<string>& channels)
{
return subscribe("UNSUBSCRIBE", channels);
return subop("UNSUBSCRIBE", channels);
}
int redis_pubsub::subscribe(const char* cmd,
const std::vector<string>& channels)
int redis_pubsub::subop(const char* cmd, const std::vector<string>& channels)
{
size_t argc = 1 + channels.size();
dbuf_pool* pool = conn_->get_pool();
@ -107,17 +91,17 @@ int redis_pubsub::subscribe(const char* cmd,
}
const string& req = conn_->build_request(argc, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_ARRAY)
const redis_result* result = conn_->run(req);
if (result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
return -1;
size_t size = result_->get_size();
size_t size = result->get_size();
if (size != channels.size())
return -1;
int nchannels = 0, ret;
for (size_t i = 1; i < size; i++)
{
const redis_result* obj = result_->get_child(i);
const redis_result* obj = result->get_child(i);
if (obj == NULL)
return -1;
if (( ret = check_channel(obj, argv[0], channels[i])) < 0)
@ -153,22 +137,22 @@ int redis_pubsub::check_channel(const redis_result* obj, const char* cmd,
rr = obj->get_child(2);
if (rr == NULL || rr->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return obj->get_integer();
}
bool redis_pubsub::get_message(string& channel, string& msg)
{
result_ = conn_->run("");
if (result_ == NULL)
const redis_result* result = conn_->run("");
if (result == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_ARRAY)
if (result->get_type() != REDIS_RESULT_ARRAY)
return false;
size_t size = result_->get_size();
size_t size = result->get_size();
if (size != 3)
return false;
const redis_result* obj = result_->get_child(0);
const redis_result* obj = result->get_child(0);
if (obj == NULL || obj->get_type() != REDIS_RESULT_STRING)
return false;
@ -177,12 +161,12 @@ bool redis_pubsub::get_message(string& channel, string& msg)
if (strcasecmp(tmp.c_str(), "message") != 0)
return false;
obj = result_->get_child(1);
obj = result->get_child(1);
if (obj == NULL || obj->get_type() != REDIS_RESULT_STRING)
return false;
obj->argv_to_string(channel);
obj = result_->get_child(2);
obj = result->get_child(2);
if (obj == NULL || obj->get_type() != REDIS_RESULT_STRING)
return false;
obj->argv_to_string(msg);

View File

@ -0,0 +1,212 @@
#include "acl_stdafx.hpp"
#include "acl_cpp/redis/redis_client.hpp"
#include "acl_cpp/redis/redis_set.hpp"
namespace acl
{
redis_set::redis_set(redis_client* conn /* = NULL */)
: redis_command(conn)
{
}
redis_set::~redis_set()
{
}
int redis_set::sadd(const char* key, const char* first_member, ...)
{
std::vector<const char*> members;
va_list ap;
va_start(ap, first_member);
const char* member;
while ((member = va_arg(ap, const char*)) != NULL)
members.push_back(member);
va_end(ap);
return sadd(key, members);
}
int redis_set::sadd(const char* key, const std::vector<const char*>& memsbers)
{
const string& req = conn_->build("SADD", key, memsbers);
return conn_->get_number(req);
}
int redis_set::sadd(const char* key, const std::vector<string>& members)
{
const string& req = conn_->build("SADD", key, members);
return conn_->get_number(req);
}
int redis_set::sadd(const char* key, const char* argv[], size_t argc)
{
const string& req = conn_->build("SADD", key, argv, argc);
return conn_->get_number(req);
}
int redis_set::sadd(const char* key, const char* argv[],
const size_t lens[], size_t argc)
{
const string& req = conn_->build("SADD", key, argv, lens, argc);
return conn_->get_number(req);
}
bool redis_set::spop(const char* key, string& buf)
{
const char* argv[2];
size_t lens[2];
argv[0] = "SPOP";
lens[0] = sizeof("SPOP") - 1;
argv[1] = key;
lens[1] = strlen(key);
const string& req = conn_->build_request(2, argv, lens);
return conn_->get_string(req, buf) < 0 ? false : true;
}
int redis_set::scard(const char* key)
{
const char* argv[2];
size_t lens[2];
argv[0] = "SCARD";
lens[0] = sizeof("SCARD") - 1;
argv[1] = key;
lens[1] = strlen(key);
const string& req = conn_->build_request(2, argv, lens);
return conn_->get_number(req);
}
int redis_set::smembers(const char* key, std::vector<string>& members)
{
const char* argv[2];
size_t lens[2];
argv[0] = "SMEMBERS";
lens[0] = sizeof("SMEMBERS") - 1;
argv[1] = key;
lens[1] = strlen(key);
const string& req = conn_->build_request(2, argv, lens);
return conn_->get_strings(req, members);
}
int redis_set::smove(const char* src, const char* dst, const char* member)
{
return smove(src, dst, member, strlen(member));
}
int redis_set::smove(const char* src, const char* dst, const string& member)
{
return smove(src, dst, member.c_str(), member.length());
}
int redis_set::smove(const char* src, const char* dst, const char* member,
size_t len)
{
const char* argv[4];
size_t lens[4];
argv[0] = "SMOVE";
lens[0] = sizeof("SMOVE") - 1;
argv[1] = src;
lens[1] = strlen(src);
argv[2] = dst;
lens[2] = strlen(dst);
argv[3] = member;
lens[3] = len;
const string& req = conn_->build_request(4, argv, lens);
return conn_->get_number(req);
}
int redis_set::sinter(std::vector<string>& members, const char* first_key, ...)
{
std::vector<const char*> keys;
keys.push_back(first_key);
va_list ap;
va_start(ap, first_key);
const char* key;
while ((key = va_arg(ap, const char*)) != NULL)
keys.push_back(key);
va_end(ap);
return sinter(keys, members);
}
int redis_set::sinter(const std::vector<const char*>& keys,
std::vector<string>& members)
{
const string& req = conn_->build("SINTER", NULL, keys);
return conn_->get_strings(req, members);
}
int redis_set::sinter(const std::vector<string>& keys,
std::vector<string>& members)
{
const string& req = conn_->build("SINTER", NULL, keys);
return conn_->get_strings(req, members);
}
int redis_set::sdiff(std::vector<string>& members, const char* first_key, ...)
{
std::vector<const char*> keys;
keys.push_back(first_key);
va_list ap;
va_start(ap, first_key);
const char* key;
while ((key = va_arg(ap, const char*)) != NULL)
keys.push_back(key);
va_end(ap);
return sdiff(keys, members);
}
int redis_set::sdiff(const std::vector<const char*>& keys,
std::vector<string>& members)
{
const string& req = conn_->build("SDIFF", NULL, keys);
return conn_->get_strings(req, members);
}
int redis_set::sdiff(const std::vector<string>& keys,
std::vector<string>& members)
{
const string& req = conn_->build("SDIFF", NULL, keys);
return conn_->get_strings(req, members);
}
int redis_set::sdiffstore(const char* dst, const char* first_key, ...)
{
std::vector<const char*> keys;
va_list ap;
va_start(ap, first_key);
const char* key;
while ((key = va_arg(ap, const char*)) != NULL)
keys.push_back(key);
va_end(ap);
return sdiffstore(dst, keys);
}
int redis_set::sdiffstore(const char* dst, const std::vector<const char*>& keys)
{
const string& req = conn_->build("SDIFFSTORE", dst, keys);
return conn_->get_number(req);
}
int redis_set::sdiffstore(const char* dst, const std::vector<string>& keys)
{
const string& req = conn_->build("SDIFFSTORE", dst, keys);
return conn_->get_number(req);
}
} // namespace acl

View File

@ -14,8 +14,7 @@ namespace acl
#define FLOAT_LEN 32
redis_string::redis_string(redis_client* conn /* = NULL */)
: conn_(conn)
, result_(NULL)
: redis_command(conn)
{
}
@ -25,12 +24,6 @@ redis_string::~redis_string()
}
void redis_string::reset()
{
if (conn_)
conn_->reset();
}
bool redis_string::set(const char* key, const char* value)
{
return set(key, strlen(key), value, strlen(value));
@ -50,16 +43,7 @@ bool redis_string::set(const char* key, size_t key_len,
lens[2] = value_len;
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "OK") != 0)
return false;
else
return true;
return conn_->get_status(req);
}
bool redis_string::setex(const char* key, const char* value, int timeout)
@ -87,16 +71,7 @@ bool redis_string::setex(const char* key, size_t key_len, const char* value,
lens[3] = value_len;
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "OK") != 0)
return false;
else
return true;
return conn_->get_status(req);
}
int redis_string::setnx(const char* key, const char* value)
@ -118,12 +93,7 @@ int redis_string::setnx(const char* key, size_t key_len,
lens[2] = value_len;
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
int redis_string::append(const char* key, const char* value)
@ -144,12 +114,7 @@ int redis_string::append(const char* key, const char* value, size_t size)
lens[2] = size;
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
bool redis_string::get(const char* key, string& buf)
@ -159,11 +124,16 @@ bool redis_string::get(const char* key, string& buf)
bool redis_string::get(const char* key, size_t len, string& buf)
{
result_ = get(key, len);
if (result_ == NULL)
return false;
(void) result_->argv_to_string(buf);
return true;
const char* argv[2];
size_t lens[2];
argv[0] = "GET";
lens[0] = sizeof("GET") - 1;
argv[1] = key;
lens[1] = len;
const string& req = conn_->build_request(2, argv, lens);
return conn_->get_string(req, buf) >= 0 ? true : false;
}
const redis_result* redis_string::get(const char* key)
@ -182,12 +152,12 @@ const redis_result* redis_string::get(const char* key, size_t len)
lens[1] = len;
const string& req = conn_->build_request(2, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
const redis_result* result = conn_->run(req);
if (result == NULL)
return NULL;
if (result_->get_type() != REDIS_RESULT_STRING)
if (result->get_type() != REDIS_RESULT_STRING)
return NULL;
return result_;
return result;
}
bool redis_string::getset(const char* key, const char* value, string& buf)
@ -209,13 +179,7 @@ bool redis_string::getset(const char* key, size_t key_len,
lens[2] = value_len;
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STRING)
return false;
(void) result_->argv_to_string(buf);
return true;
return conn_->get_string(req, buf) >= 0 ? true : false;
}
/////////////////////////////////////////////////////////////////////////////
@ -236,12 +200,7 @@ int redis_string::str_len(const char* key, size_t len)
lens[1] = len;
const string& req = conn_->build_request(2, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
int redis_string::setrange(const char* key, unsigned offset, const char* value)
@ -269,13 +228,7 @@ int redis_string::setrange(const char* key, size_t key_len, unsigned offset,
lens[3] = value_len;
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
bool redis_string::getrange(const char* key, int start, int end, string& buf)
@ -304,13 +257,7 @@ bool redis_string::getrange(const char* key, size_t key_len,
lens[3] = strlen(end_buf);
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STRING)
return false;
(void) result_->argv_to_string(buf);
return true;
return conn_->get_string(req, buf) >= 0 ? true : false;
}
/////////////////////////////////////////////////////////////////////////////
@ -339,12 +286,7 @@ bool redis_string::setbit(const char* key, size_t len, unsigned offset, int bit)
lens[3] = 1;
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return false;
return true;
return conn_->get_number(req) >= 0 ? true : false;
}
bool redis_string::getbit(const char* key, unsigned offset, int& bit)
@ -352,7 +294,8 @@ bool redis_string::getbit(const char* key, unsigned offset, int& bit)
return getbit(key, strlen(key), offset, bit);
}
bool redis_string::getbit(const char* key, size_t len, unsigned offset, int& bit)
bool redis_string::getbit(const char* key, size_t len,
unsigned offset, int& bit)
{
const char* argv[3];
size_t lens[3];
@ -368,13 +311,10 @@ bool redis_string::getbit(const char* key, size_t len, unsigned offset, int& bit
lens[2] = strlen(buf4off);
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
int ret = conn_->get_number(req);
if (ret < 0)
return false;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return false;
bit = result_->get_integer() == 0 ? 0 : 1;
bit = ret == 0 ? 0 : 1;
return true;
}
@ -394,12 +334,7 @@ int redis_string::bitcount(const char* key, size_t len)
lens[1] = len;
const string& req = conn_->build_request(2, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
int redis_string::bitcount(const char* key, int start, int end)
@ -428,12 +363,7 @@ int redis_string::bitcount(const char* key, size_t len, int start, int end)
lens[3] = strlen(buf4end);
const string& req = conn_->build_request(4, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
int redis_string::bitop_and(const char* destkey, const std::vector<string>& keys)
@ -542,7 +472,7 @@ int redis_string::bitop(const char* op, const char* destkey,
}
const string& req = conn_->build_request(argc, argv, lens);
return bitop(req);
return conn_->get_number(req);
}
int redis_string::bitop(const char* op, const char* destkey,
@ -570,7 +500,7 @@ int redis_string::bitop(const char* op, const char* destkey,
}
const string& req = conn_->build_request(argc, argv, lens);
return bitop(req);
return conn_->get_number(req);
}
int redis_string::bitop(const char* op, const char* destkey,
@ -597,17 +527,7 @@ int redis_string::bitop(const char* op, const char* destkey,
}
const string& req = conn_->build_request(argc, argv, lens);
return bitop(req);
}
int redis_string::bitop(const string& req)
{
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
return result_->get_integer();
return conn_->get_number(req);
}
/////////////////////////////////////////////////////////////////////////////
@ -615,33 +535,33 @@ int redis_string::bitop(const string& req)
bool redis_string::mset(const std::map<string, string>& objs)
{
const string& req = conn_->build("MSET", NULL, objs);
return mset(req);
return conn_->get_status(req);
}
bool redis_string::mset(const std::map<int, string>& objs)
{
const string& req = conn_->build("MSET", NULL, objs);
return mset(req);
return conn_->get_status(req);
}
bool redis_string::mset(const std::vector<string>& keys,
const std::vector<string>& values)
{
const string& req = conn_->build("MSET", NULL, keys, values);
return mset(req);
return conn_->get_status(req);
}
bool redis_string::mset(const std::vector<int>& keys,
const std::vector<string>& values)
{
const string& req = conn_->build("MSET", NULL, keys, values);
return mset(req);
return conn_->get_status(req);
}
bool redis_string::mset(const char* keys[], const char* values[], size_t argc)
{
const string& req = conn_->build("MSET", NULL, keys, values, argc);
return mset(req);
return conn_->get_status(req);
}
bool redis_string::mset(const char* keys[], const size_t keys_len[],
@ -649,20 +569,7 @@ bool redis_string::mset(const char* keys[], const size_t keys_len[],
{
const string& req = conn_->build("MSET", NULL, keys, keys_len,
values, values_len, argc);
return mset(req);
}
bool redis_string::mset(const string& req)
{
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* ptr = result_->get(0);
if (ptr == NULL || strcasecmp(ptr, "OK") != 0)
return false;
return true;
return conn_->get_status(req);
}
/////////////////////////////////////////////////////////////////////////////
@ -670,34 +577,34 @@ bool redis_string::mset(const string& req)
int redis_string::msetnx(const std::map<string, string>& objs)
{
const string& req = conn_->build("MSETNX", NULL, objs);
return msetnx(req);
return conn_->get_number(req);
}
int redis_string::msetnx(const std::map<int, string>& objs)
{
const string& req = conn_->build("MSETNX", NULL, objs);
return msetnx(req);
return conn_->get_number(req);
}
int redis_string::msetnx(const std::vector<string>& keys,
const std::vector<string>& values)
{
const string& req = conn_->build("MSETNX", NULL, keys, values);
return msetnx(req);
return conn_->get_number(req);
}
int redis_string::msetnx(const std::vector<int>& keys,
const std::vector<string>& values)
{
const string& req = conn_->build("MSETNX", NULL, keys, values);
return msetnx(req);
return conn_->get_number(req);
}
int redis_string::msetnx(const char* keys[], const char* values[], size_t argc)
{
const string& req = conn_->build("MSETNX", NULL, keys, values, argc);
return msetnx(req);
return conn_->get_number(req);
}
int redis_string::msetnx(const char* keys[], const size_t keys_len[],
@ -705,56 +612,40 @@ int redis_string::msetnx(const char* keys[], const size_t keys_len[],
{
const string& req = conn_->build("MSETNX", NULL, keys, keys_len,
values, values_len, argc);
return msetnx(req);
}
int redis_string::msetnx(const string& req)
{
result_ = conn_->run(req);
if (result_ == NULL)
return -1;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return -1;
int ret = result_->get_integer();
if (ret == 0)
return 0;
else if (ret == 1)
return 1;
else
return -1;
return conn_->get_number(req);
}
/////////////////////////////////////////////////////////////////////////////
bool redis_string::mget(const std::vector<string>& keys,
std::vector<string>* result /* = NULL */)
std::vector<string>* out /* = NULL */)
{
const string& req = conn_->build("MGET", NULL, keys);
return mget(req, result);
return mget(req, out);
}
bool redis_string::mget(const std::vector<const char*>& keys,
std::vector<string>* result /* = NULL */)
std::vector<string>* out /* = NULL */)
{
const string& req = conn_->build("MGET", NULL, keys);
return mget(req, result);
return mget(req, out);
}
bool redis_string::mget(const std::vector<char*>& keys,
std::vector<string>* result /* = NULL */)
std::vector<string>* out /* = NULL */)
{
const string& req = conn_->build("MGET", NULL, keys);
return mget(req, result);
return mget(req, out);
}
bool redis_string::mget(const std::vector<int>& keys,
std::vector<string>* result /* = NULL */)
std::vector<string>* out /* = NULL */)
{
const string& req = conn_->build("MGET", NULL, keys);
return mget(req, result);
return mget(req, out);
}
bool redis_string::mget(std::vector<string>* result,
bool redis_string::mget(std::vector<string>* out,
const char* first_key, ...)
{
std::vector<const char*> keys;
@ -766,63 +657,63 @@ bool redis_string::mget(std::vector<string>* result,
keys.push_back(key);
va_end(ap);
return mget(keys, result);
return mget(keys, out);
}
bool redis_string::mget(const char* keys[], size_t argc,
std::vector<string>* result /* = NULL */)
std::vector<string>* out /* = NULL */)
{
const string& req = conn_->build("MGET", NULL, keys, argc);
return mget(req, result);
return mget(req, out);
}
bool redis_string::mget(const int keys[], size_t argc,
std::vector<string>* result /* = NULL */)
std::vector<string>* out /* = NULL */)
{
const string& req = conn_->build("MGET", NULL, keys, argc);
return mget(req, result);
return mget(req, out);
}
bool redis_string::mget(const char* keys[], const size_t keys_len[],
size_t argc, std::vector<string>* result /* = NULL */)
size_t argc, std::vector<string>* out /* = NULL */)
{
const string& req = conn_->build("MGET", NULL, keys, keys_len, argc);
return mget(req, result);
return mget(req, out);
}
bool redis_string::mget(const string& req,
std::vector<string>* result /* = NULL */)
bool redis_string::mget(const string& req, std::vector<string>* out /* = NULL */)
{
result_ = conn_->run(req);
if (result_ == NULL)
const redis_result* result = conn_->run(req);
if (result == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_ARRAY)
if (result->get_type() != REDIS_RESULT_ARRAY)
return false;
if (result == NULL)
return true;
size_t size = mget_size();
string buf(4096);
const redis_result* rr;
size_t nslice, len;
const char* ptr;
size_t size = mget_size();
for (size_t i = 0; i < size; i++)
{
rr = mget_result(i);
if (rr == NULL || (nslice = rr->get_size()) == 0)
result->push_back("");
out->push_back("");
else if (nslice == 1)
{
ptr = rr->get(0, &len);
buf.copy(ptr, len);
result->push_back(buf);
out->push_back(buf);
}
else
{
buf.clear();
rr->argv_to_string(buf);
result->push_back(buf);
out->push_back(buf);
}
}
@ -831,41 +722,17 @@ bool redis_string::mget(const string& req,
const char* redis_string::mget_value(size_t i, size_t* len /* = NULL */) const
{
const redis_result* rr = mget_result(i);
if (rr == NULL)
return NULL;
size_t size = rr->get_size();
if (size == 0)
return NULL;
if (size == 1)
return rr->get(0, len);
// 大内存有可能被切片成多个不连续的小内存
size = rr->get_length();
size++;
char* buf = (char*) conn_->get_pool()->dbuf_alloc(size);
size = rr->argv_to_string(buf, size);
if (len)
*len = size;
return buf;
return conn_->get_value(i, len);
}
const redis_result* redis_string::mget_result(size_t i) const
{
if (result_ == NULL)
return NULL;
if (result_->get_type() != REDIS_RESULT_ARRAY)
return NULL;
return result_->get_child(i);
return conn_->get_child(i);
}
size_t redis_string::mget_size() const
{
if (result_ == NULL)
return 0;
if (result_->get_type() != REDIS_RESULT_ARRAY)
return 0;
return result_->get_size();
return conn_->get_size();
}
/////////////////////////////////////////////////////////////////////////////
@ -898,16 +765,11 @@ bool redis_string::incrbyfloat(const char* key, double inc,
lens[2] = strlen(buf);
const string& req = conn_->build_request(3, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_STRING)
if (conn_->get_string(req, buf, sizeof(buf)) == false)
return false;
if (result != NULL)
{
(void) result_->argv_to_string(buf, sizeof(buf));
*result = atof(buf);
}
return true;
}
@ -944,14 +806,13 @@ bool redis_string::incoper(const char* cmd, const char* key, long long int n,
}
const string& req = conn_->build_request(argc, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL)
return false;
if (result_->get_type() != REDIS_RESULT_INTEGER)
return false;
bool success;
if (result != NULL)
*result = result_->get_integer64();
return true;
*result = conn_->get_number64(req, &success);
else
(void) conn_->get_number64(req, &success);
return success;
}
/////////////////////////////////////////////////////////////////////////////

View File

@ -8,8 +8,7 @@ namespace acl
{
redis_transaction::redis_transaction(redis_client* conn /* = NULL */)
: conn_(conn)
, result_(NULL)
: redis_command(conn)
{
}
@ -19,42 +18,16 @@ redis_transaction::~redis_transaction()
}
void redis_transaction::reset()
{
if (conn_)
conn_->reset();
cmds_.clear();
}
void redis_transaction::set_client(redis_client* conn)
{
conn_ = conn;
}
bool redis_transaction::watch(const std::vector<string>& keys)
{
const string& req = conn_->build("WATCH", NULL, keys);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "OK") != 0)
return false;
else
return true;
return conn_->get_status(req);
}
bool redis_transaction::unwatch(const std::vector<string>& keys)
{
const string& req = conn_->build("UNWATCH", NULL, keys);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "OK") != 0)
return false;
else
return true;
return conn_->get_status(req);
}
bool redis_transaction::multi()
@ -66,13 +39,7 @@ bool redis_transaction::multi()
lens[0] = sizeof("MULTI") - 1;
const string& req = conn_->build_request(1, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "OK") != 0)
return false;
return true;
return conn_->get_status(req);
}
bool redis_transaction::exec()
@ -84,11 +51,11 @@ bool redis_transaction::exec()
lens[0] = sizeof("EXEC") - 1;
const string& req = conn_->build_request(1, argv, lens);
result_ = conn_->run(req);
if(result_ == NULL || result_->get_type() != REDIS_RESULT_ARRAY)
const redis_result* result = conn_->run(req);
if(result == NULL || result->get_type() != REDIS_RESULT_ARRAY)
return false;
size_t size = result_->get_size();
size_t size = result->get_size();
if (size != cmds_.size())
return false;
return true;
@ -103,47 +70,32 @@ bool redis_transaction::discard()
lens[0] = sizeof("DISCARD") - 1;
const string& req = conn_->build_request(1, argv, lens);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "OK") != 0)
return false;
return true;
return conn_->get_status(req);
}
bool redis_transaction::queue_cmd(const char* cmd, const char* argv[],
const size_t lens[], size_t argc)
{
const string& req = conn_->build(cmd, NULL, argv, lens, argc);
result_ = conn_->run(req);
if (result_ == NULL || result_->get_type() != REDIS_RESULT_STATUS)
return false;
const char* status = result_->get_status();
if (status == NULL || strcasecmp(status, "QUEUED") != 0)
if (conn_->get_status(req, "QUEUED") == false)
return false;
cmds_.push_back(cmd);
return true;
}
size_t redis_transaction::get_size()
size_t redis_transaction::get_size() const
{
if (result_ == NULL)
return 0;
return result_->get_size();
return conn_->get_size();
}
const redis_result* redis_transaction::get_child(size_t i, string* cmd)
const redis_result* redis_transaction::get_child(size_t i, string* cmd) const
{
if (cmd != NULL)
{
if (i < cmds_.size())
*cmd = cmds_[i];
}
if (result_ == NULL || result_->get_type() != REDIS_RESULT_ARRAY)
return NULL;
return result_->get_child(i);
return conn_->get_child(i);
}
} // namespace acl

View File

@ -0,0 +1,19 @@
#include "acl_stdafx.hpp"
#include "acl_cpp/redis/redis_client.hpp"
#include "acl_cpp/redis/redis_zset.hpp"
namespace acl
{
redis_zset::redis_zset(redis_client* conn /* = NULL */)
: redis_command(conn)
{
}
redis_zset::~redis_zset()
{
}
} // namespace acl