@@ -1035,6 +1035,16 @@ bool CAttemperEngineSink::OnTCPNetworkMainServiceInfo(WORD wSubCmdID, VOID * pDa | |||||
m_pITCPNetworkEngine->SendDataBatch(MDM_CS_SERVICE_INFO, SUB_CS_S_SERVER_ONLINE, &ServerOnLine, sizeof(ServerOnLine), 0L); | m_pITCPNetworkEngine->SendDataBatch(MDM_CS_SERVICE_INFO, SUB_CS_S_SERVER_ONLINE, &ServerOnLine, sizeof(ServerOnLine), 0L); | ||||
//存储信息到redis | |||||
std::vector<std::pair<std::string, std::string>> field_val; | |||||
field_val.push_back({ "wServerID", string_format("%d",ServerOnLine.wServerID) }); | |||||
field_val.push_back({ "dwOnLineCount", string_format("%d",ServerOnLine.dwOnLineCount) }); | |||||
field_val.push_back({ "wTableOnlineCount", string_format("%d",ServerOnLine.wTableOnlineCount) }); | |||||
field_val.push_back({ "wTableFullCount", string_format("%d",ServerOnLine.wTableFullCount) }); | |||||
m_RedisClient->GetRedisClient()->hmset(string_format("Server_Info:%d", ServerOnLine.wServerID), field_val, [](const cpp_redis::reply& reply) { | |||||
std::cout << "hmset: " << reply << std::endl; | |||||
}); | |||||
m_RedisClient->GetRedisClient()->sync_commit(); | |||||
} | } | ||||
return true; | return true; | ||||
@@ -111,11 +111,13 @@ bool CServiceUnits::InitializeService() | |||||
WORD wServicePort = m_InitParameter.m_wServicePort; | WORD wServicePort = m_InitParameter.m_wServicePort; | ||||
if (m_TCPNetworkEngine->SetServiceParameter(wServicePort, wMaxConnect, szCompilation) == false) return false; | if (m_TCPNetworkEngine->SetServiceParameter(wServicePort, wMaxConnect, szCompilation) == false) return false; | ||||
//Á¬½Óredis | |||||
m_RedisClient = std::make_shared<RedisBase>(); | |||||
//Á¬½Óredis | |||||
m_RedisClient = std::make_shared<RedisBase>(); | |||||
m_RedisClient->Connect(m_InitParameter.m_RedisHost, m_InitParameter.m_RedisPort, m_InitParameter.m_RedisDb, m_InitParameter.m_RedisTimeOut, m_InitParameter.m_RedisMaxReconnect, m_InitParameter.m_RedisReconnectInterval); | m_RedisClient->Connect(m_InitParameter.m_RedisHost, m_InitParameter.m_RedisPort, m_InitParameter.m_RedisDb, m_InitParameter.m_RedisTimeOut, m_InitParameter.m_RedisMaxReconnect, m_InitParameter.m_RedisReconnectInterval); | ||||
m_AttemperEngineSink.m_RedisClient = m_RedisClient; | |||||
return true; | |||||
m_AttemperEngineSink.m_RedisClient = m_RedisClient; | |||||
m_RedisClient->GetRedisClient()->set("hello", "42"); | |||||
m_RedisClient->GetRedisClient()->sync_commit(); | |||||
return true; | |||||
} | } | ||||
//Æô¶¯ÄÚºË | //Æô¶¯ÄÚºË | ||||
@@ -21,7 +21,6 @@ bool RedisBase::Connect(const std::string& host /*= "127.0.0.1"*/, std::size_t p | |||||
} | } | ||||
}, timeout_ms, max_reconnects, reconnect_interval_ms); | }, timeout_ms, max_reconnects, reconnect_interval_ms); | ||||
m_redisclient->select(db); | m_redisclient->select(db); | ||||
m_redisclient->disconnect(true); | |||||
return true; | return true; | ||||
} | } | ||||
@@ -7,6 +7,19 @@ | |||||
#include "winsock_initializer.h" | #include "winsock_initializer.h" | ||||
#include <cpp_redis/misc/macro.hpp> | #include <cpp_redis/misc/macro.hpp> | ||||
#include <memory> | #include <memory> | ||||
#include <iostream> | |||||
#include <string> | |||||
#include <cstdio> | |||||
template<typename ... Args> | |||||
std:: string string_format(const std::string& format, Args ... args) | |||||
{ | |||||
size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1; // Extra space for '\0' | |||||
std::unique_ptr<char[]> buf(new char[size]); | |||||
snprintf(buf.get(), size, format.c_str(), args ...); | |||||
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside | |||||
} | |||||
//Redis²Ù×÷»ù´¡Àà | //Redis²Ù×÷»ù´¡Àà | ||||
#ifndef _DEBUG | #ifndef _DEBUG | ||||