hiredis異步接口封裝并導出到Lua
(金慶的專欄 2017.1)
hiredis 不支持 Windows, Windows 下使用 wasppdotorg / hiredis-for-windows 。
Linux 下仍是 redis/hiredis。
hiredis-for-windows 是以 hiredis 0.13.3 為基礎移植的。
hiredis-for-windows 需要稍加修正:
* 去除 inline 宏
* TCP_NODELAY 改在連接之前設置。
詳見其Issue.
Cluster 支持采用 shinberg/cpp-hiredis-cluster。這是個CPP庫,支持異步,
要求 hiredis >= 0.12.0。
jinq0123/cpp-hiredis-cluster 在 develop 分支上更改了接口,讓它更好用。
因為網絡庫是boost asio, 所以需要asio適配器,采用 jinq0123/hiredis-boostasio-adapter。
cpp-hiredis-cluster 提供的是統一的Command接口,接收字符串命令,返回 redisReply.
對于常用命令,需要更簡單的接口。
在Lua手游服務器代碼中新建CRedis類,封裝 cpp-hiredis-cluster,
為常用redis命令封裝更好用的接口。
CRedis類封裝了asio, 接口是根據應用需要定義的,所以是專用接口,
不在 cpp-hiredis-cluster 中實現。
bool CRedis::Init(io_service& rIos, const std::string& sHost, uint16_t uPort)
創建 RedisCluster 對象。
io_service 用于創建一個 redis 事件適配器,
RedisCluster創建需要一個適配器。
sHost, uPort 用于初始化連接redis cluster, 獲取集群信息。
using Cmd = RedisCluster::AsyncHiredisCommand;
bool CRedis::Init(io_service& rIos, const std::string& sHost, uint16_t uPort)
{
m_pAdapter.reset(new Adapter(rIos));
try
{
m_pCluster.reset(Cmd::createCluster("127.0.0.1", 7000, *m_pAdapter));
}
catch (const RedisCluster::ClusterException &e)
{
LOG_ERROR("Cluster exception: " << e.what());
return false;
}
return true;
}
static Cmd::Action handleException(const RedisCluster::ClusterException &exception,
RedisCluster::HiredisProcess::processState state)
{
// Check the exception type.
// Retry in case of non-critical exceptions.
if (!dynamic_cast<const RedisCluster::CriticalException*>(&exception))
{
LOG_WARN("Exception in processing async redis callback: "
<< exception.what() << " Retry...");
// retry to send a command to redis node
return Cmd::RETRY;
}
LOG_ERROR("Critical exception in processing async redis callback: "
<< exception.what());
return Cmd::FINISH;
}
static void handleSetReply(const redisReply &reply, const CRedis::SetCb& setCb)
{
if (!setCb) return;
if (reply.type == REDIS_REPLY_STATUS)
{
const std::string OK("OK");
if (OK == reply.str)
{
setCb(true);
return;
}
}
LOG_WARN("Set reply: " << reply.str);
setCb(false);
}
void CRedis::Set(const string& sKey, const string& sValue, const SetCb& setCb)
{
assert(m_pCluster);
Cmd::commandf2(*m_pCluster, sKey,
[setCb](const redisReply& reply) {
handleSetReply(reply, setCb);
},
handleException,
"set %s %s", sKey.c_str(), sValue.c_str());
}
static void handleGetReply(const redisReply& reply,
const CRedis::ReplyStringCb& hdlStrReply)
{
if (!hdlStrReply) return;
using Rt = CRedis::ReplyType;
if (reply.type == REDIS_REPLY_NIL)
{
hdlStrReply(Rt::NIL, "");
return;
}
std::string sReply(reply.str, reply.len);
if (reply.type == REDIS_REPLY_STRING)
hdlStrReply(Rt::OK, sReply);
else
hdlStrReply(Rt::ERR, sReply);
}
void CRedis::Get(const std::string& sKey, const ReplyStringCb& hdlStrRepl)
{
assert(m_pCluster);
Cmd::commandf2(*m_pCluster, sKey,
[hdlStrRepl](const redisReply& reply) {
handleGetReply(reply, hdlStrRepl);
},
handleException,
"get %s", sKey.c_str());
}
handleException 是Cmd::command() 接口中需要的異常處理,這里是盡量重試。
Cmd::command() 中的第3個參數是 redis 應答的回調,讀取 redisReply, 然后觸發命令的回調。
CRedis::Get() 執行 redis GET 命令,固定1個參數,返回是字符串,nil, 或錯誤。
enum class ReplyType
{
OK = 0, // redis replys string/integer/array
NIL = 1, // redis replys nil
ERR = 2, // redis replys error status
};
// 簡單的常用命令會自動解析reply, 使用更易用的回調。
using ReplyStringCb = function<void (ReplyType, const string& sReply)>;
CRedis::Get() 的回調就是 ReplyStringCb。
void CRedis::Set() 的回調只需知道成功或失敗,SetCb 定義為:
using SetCb = function<void (bool ok)>;
失敗時會有錯誤信息,已統一打印日志,不再暴露出來。
SET 命令完整的參數列表還有其他參數:
set key value [EX seconds] [PX milliseconds] [NX|XX]
因為常用的就 "set key value", 其他擴展的命令需要使用通用的 command() 接口,
并需要讀取 redisReply.
使用 LuaIntf 導出 CRedis 到 Lua.
因為只有一個 CRedis, 所以導出為模塊 c_redis:
#include <LuaIntf/LuaIntf.h>
namespace {
void Set(const string& sKey, const string& sValue, const LuaRef& luaSetCb)
{
// Default is empty callback.
auto setCb = ToFunction<CRedis::SetCb>(luaSetCb);
GetRedis().Set(sKey, sValue, setCb);
}
void Get(const string& sKey, const LuaRef& luaReplyStringCb)
{
auto replyStringCb = ToFunction<CRedis::ReplyStringCb>(
luaReplyStringCb);
GetRedis().Get(sKey, replyStringCb);
}
} // namespace
void Bind(lua_State* L)
{
LuaBinding(L).beginModule("c_redis")
.addFunction("set", &Set)
.addFunction("get", &Get)
.endModule();
}
需要將 lua 的回調函數轉成 cpp 的回調:
template <class Function>
Function ToFunction(const LuaIntf::LuaRef& luaFunction)
{
// Default is empty.
if (!luaFunction)
return Function(); // skip nil
if (luaFunction.isFunction())
return luaFunction.toValue<Function>(); // Todo: catch
LOG_WARN_TO("ToFunction", "Lua function expected, but got "
<< luaFunction.typeName());
return Function();
}
Lua 這樣調用:
c_redis.set("FOO", "1234")
c_redis.set("FOO", "1234", function(ok) print(ok) end)
c_redis.get("FOO", function(reply_type, reply)
assert("string" == type(reply))
if 0 == reply_type or 1 == reply_type then
print("FOO="..reply)
else
print("Error: "..reply)
end
end)
(金慶的專欄 2017.1)
hiredis 不支持 Windows, Windows 下使用 wasppdotorg / hiredis-for-windows 。
Linux 下仍是 redis/hiredis。
hiredis-for-windows 是以 hiredis 0.13.3 為基礎移植的。
hiredis-for-windows 需要稍加修正:
* 去除 inline 宏
* TCP_NODELAY 改在連接之前設置。
詳見其Issue.
Cluster 支持采用 shinberg/cpp-hiredis-cluster。這是個CPP庫,支持異步,
要求 hiredis >= 0.12.0。
jinq0123/cpp-hiredis-cluster 在 develop 分支上更改了接口,讓它更好用。
因為網絡庫是boost asio, 所以需要asio適配器,采用 jinq0123/hiredis-boostasio-adapter。
cpp-hiredis-cluster 提供的是統一的Command接口,接收字符串命令,返回 redisReply.
對于常用命令,需要更簡單的接口。
在Lua手游服務器代碼中新建CRedis類,封裝 cpp-hiredis-cluster,
為常用redis命令封裝更好用的接口。
CRedis類封裝了asio, 接口是根據應用需要定義的,所以是專用接口,
不在 cpp-hiredis-cluster 中實現。
bool CRedis::Init(io_service& rIos, const std::string& sHost, uint16_t uPort)
創建 RedisCluster 對象。
io_service 用于創建一個 redis 事件適配器,
RedisCluster創建需要一個適配器。
sHost, uPort 用于初始化連接redis cluster, 獲取集群信息。
using Cmd = RedisCluster::AsyncHiredisCommand;
bool CRedis::Init(io_service& rIos, const std::string& sHost, uint16_t uPort)
{
m_pAdapter.reset(new Adapter(rIos));
try
{
m_pCluster.reset(Cmd::createCluster("127.0.0.1", 7000, *m_pAdapter));
}
catch (const RedisCluster::ClusterException &e)
{
LOG_ERROR("Cluster exception: " << e.what());
return false;
}
return true;
}
static Cmd::Action handleException(const RedisCluster::ClusterException &exception,
RedisCluster::HiredisProcess::processState state)
{
// Check the exception type.
// Retry in case of non-critical exceptions.
if (!dynamic_cast<const RedisCluster::CriticalException*>(&exception))
{
LOG_WARN("Exception in processing async redis callback: "
<< exception.what() << " Retry...");
// retry to send a command to redis node
return Cmd::RETRY;
}
LOG_ERROR("Critical exception in processing async redis callback: "
<< exception.what());
return Cmd::FINISH;
}
static void handleSetReply(const redisReply &reply, const CRedis::SetCb& setCb)
{
if (!setCb) return;
if (reply.type == REDIS_REPLY_STATUS)
{
const std::string OK("OK");
if (OK == reply.str)
{
setCb(true);
return;
}
}
LOG_WARN("Set reply: " << reply.str);
setCb(false);
}
void CRedis::Set(const string& sKey, const string& sValue, const SetCb& setCb)
{
assert(m_pCluster);
Cmd::commandf2(*m_pCluster, sKey,
[setCb](const redisReply& reply) {
handleSetReply(reply, setCb);
},
handleException,
"set %s %s", sKey.c_str(), sValue.c_str());
}
static void handleGetReply(const redisReply& reply,
const CRedis::ReplyStringCb& hdlStrReply)
{
if (!hdlStrReply) return;
using Rt = CRedis::ReplyType;
if (reply.type == REDIS_REPLY_NIL)
{
hdlStrReply(Rt::NIL, "");
return;
}
std::string sReply(reply.str, reply.len);
if (reply.type == REDIS_REPLY_STRING)
hdlStrReply(Rt::OK, sReply);
else
hdlStrReply(Rt::ERR, sReply);
}
void CRedis::Get(const std::string& sKey, const ReplyStringCb& hdlStrRepl)
{
assert(m_pCluster);
Cmd::commandf2(*m_pCluster, sKey,
[hdlStrRepl](const redisReply& reply) {
handleGetReply(reply, hdlStrRepl);
},
handleException,
"get %s", sKey.c_str());
}
handleException 是Cmd::command() 接口中需要的異常處理,這里是盡量重試。
Cmd::command() 中的第3個參數是 redis 應答的回調,讀取 redisReply, 然后觸發命令的回調。
CRedis::Get() 執行 redis GET 命令,固定1個參數,返回是字符串,nil, 或錯誤。
enum class ReplyType
{
OK = 0, // redis replys string/integer/array
NIL = 1, // redis replys nil
ERR = 2, // redis replys error status
};
// 簡單的常用命令會自動解析reply, 使用更易用的回調。
using ReplyStringCb = function<void (ReplyType, const string& sReply)>;
CRedis::Get() 的回調就是 ReplyStringCb。
void CRedis::Set() 的回調只需知道成功或失敗,SetCb 定義為:
using SetCb = function<void (bool ok)>;
失敗時會有錯誤信息,已統一打印日志,不再暴露出來。
SET 命令完整的參數列表還有其他參數:
set key value [EX seconds] [PX milliseconds] [NX|XX]
因為常用的就 "set key value", 其他擴展的命令需要使用通用的 command() 接口,
并需要讀取 redisReply.
使用 LuaIntf 導出 CRedis 到 Lua.
因為只有一個 CRedis, 所以導出為模塊 c_redis:
#include <LuaIntf/LuaIntf.h>
namespace {
void Set(const string& sKey, const string& sValue, const LuaRef& luaSetCb)
{
// Default is empty callback.
auto setCb = ToFunction<CRedis::SetCb>(luaSetCb);
GetRedis().Set(sKey, sValue, setCb);
}
void Get(const string& sKey, const LuaRef& luaReplyStringCb)
{
auto replyStringCb = ToFunction<CRedis::ReplyStringCb>(
luaReplyStringCb);
GetRedis().Get(sKey, replyStringCb);
}
} // namespace
void Bind(lua_State* L)
{
LuaBinding(L).beginModule("c_redis")
.addFunction("set", &Set)
.addFunction("get", &Get)
.endModule();
}
需要將 lua 的回調函數轉成 cpp 的回調:
template <class Function>
Function ToFunction(const LuaIntf::LuaRef& luaFunction)
{
// Default is empty.
if (!luaFunction)
return Function(); // skip nil
if (luaFunction.isFunction())
return luaFunction.toValue<Function>(); // Todo: catch
LOG_WARN_TO("ToFunction", "Lua function expected, but got "
<< luaFunction.typeName());
return Function();
}
Lua 這樣調用:
c_redis.set("FOO", "1234")
c_redis.set("FOO", "1234", function(ok) print(ok) end)
c_redis.get("FOO", function(reply_type, reply)
assert("string" == type(reply))
if 0 == reply_type or 1 == reply_type then
print("FOO="..reply)
else
print("Error: "..reply)
end
end)