Skip to content

Commit 06f0d4d

Browse files
committed
added:data insert memory-cache and switch supported
1 parent 7381e5c commit 06f0d4d

File tree

14 files changed

+195
-37
lines changed

14 files changed

+195
-37
lines changed

XEngine_Release/XEngine_Config/XEngine_Config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"tszPassLogout":""
3636
},
3737
"XMemory":{
38-
"bEnable":true,
38+
"bDataQueryEnable":true,
39+
"bDataInsertEnable":true,
3940
"nTimeLast":3600,
4041
"nTimeStart":0
4142
},

XEngine_Source/MQCore_ConfigModule/Config_Define.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ typedef struct
5757
}st_XPass;
5858
struct
5959
{
60-
bool bEnable;
60+
bool bDataQueryEnable;
61+
bool bDataInsertEnable;
6162
int nTimeLast;
6263
int nTimeStart;
6364
}st_XMemory;

XEngine_Source/MQCore_ConfigModule/Config_Json/Config_Json.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,15 @@ bool CConfig_Json::Config_Json_File(LPCXSTR lpszConfigFile,XENGINE_SERVERCONFIG
123123
_tcsxcpy(pSt_ServerConfig->st_XPass.tszPassRegister, st_JsonXPass["tszPassRegister"].asCString());
124124
_tcsxcpy(pSt_ServerConfig->st_XPass.tszPassUNReg, st_JsonXPass["tszPassUNReg"].asCString());
125125

126-
if (st_JsonRoot["XMemory"].empty() || (3 != st_JsonRoot["XMemory"].size()))
126+
if (st_JsonRoot["XMemory"].empty() || (4 != st_JsonRoot["XMemory"].size()))
127127
{
128128
Config_IsErrorOccur = true;
129129
Config_dwErrorCode = ERROR_MQ_MODULE_CONFIG_JSON_XMEMORY;
130130
return false;
131131
}
132132
Json::Value st_JsonXMemory = st_JsonRoot["XMemory"];
133-
pSt_ServerConfig->st_XMemory.bEnable = st_JsonXMemory["bEnable"].asBool();
133+
pSt_ServerConfig->st_XMemory.bDataQueryEnable = st_JsonXMemory["bDataQueryEnable"].asBool();
134+
pSt_ServerConfig->st_XMemory.bDataInsertEnable = st_JsonXMemory["bDataInsertEnable"].asBool();
134135
pSt_ServerConfig->st_XMemory.nTimeLast = st_JsonXMemory["nTimeLast"].asInt();
135136
pSt_ServerConfig->st_XMemory.nTimeStart = st_JsonXMemory["nTimeStart"].asInt();
136137

XEngine_Source/MQCore_DBModule/DBModule_Define.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,22 @@ extern "C" XLONG DBModule_GetLastError(int *pInt_SysError = NULL);
7171
类型:数据结构指针
7272
可空:N
7373
意思:数据MYSQL数据库连接信息
74-
参数.二:bMemoryCache
74+
参数.二:bMemoryQuery
7575
In/Out:In
7676
类型:逻辑型
7777
可空:Y
78-
意思:是否启用缓存支持
78+
意思:是否启用查询缓存支持
79+
参数.三:bMemoryInsert
80+
In/Out:In
81+
类型:逻辑型
82+
可空:Y
83+
意思:是否启用插入缓存支持
7984
返回值
8085
类型:逻辑型
8186
意思:是否成功
8287
备注:
8388
*********************************************************************/
84-
extern "C" bool DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO* pSt_DBConnector, bool bMemoryCache = true);
89+
extern "C" bool DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO* pSt_DBConnector, bool bMemoryQuery = true, bool bMemoryInsert = true);
8590
/********************************************************************
8691
函数名称:DBModule_MQData_Destory
8792
函数功能:销毁数据库管理器

XEngine_Source/MQCore_DBModule/DBModule_MQData/DBModule_MQData.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@ CDBModule_MQData::~CDBModule_MQData()
3030
类型:数据结构指针
3131
可空:N
3232
意思:数据MYSQL数据库连接信息
33-
参数.二:bMemoryCache
33+
参数.二:bMemoryQuery
3434
In/Out:In
3535
类型:逻辑型
3636
可空:Y
37-
意思:是否启用缓存支持
37+
意思:是否启用查询缓存支持
38+
参数.三:bMemoryInsert
39+
In/Out:In
40+
类型:逻辑型
41+
可空:Y
42+
意思:是否启用插入缓存支持
3843
返回值
3944
类型:逻辑型
4045
意思:是否成功
4146
备注:
4247
*********************************************************************/
43-
bool CDBModule_MQData::DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO* pSt_DBConnector, bool bMemoryCache)
48+
bool CDBModule_MQData::DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO* pSt_DBConnector, bool bMemoryQuery /* = true */, bool bMemoryInsert /* = true */)
4449
{
4550
DBModule_IsErrorOccur = false;
4651

@@ -50,7 +55,8 @@ bool CDBModule_MQData::DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO* pSt_DBCo
5055
DBModule_dwErrorCode = ERROR_XENGINE_MQCORE_DATABASE_PARAMENT;
5156
return false;
5257
}
53-
m_bMemoryCache = bMemoryCache;
58+
m_bMemoryQuery = bMemoryQuery;
59+
m_bMemoryInsert = bMemoryInsert;
5460
//连接数据库
5561
_tcsxcpy(pSt_DBConnector->tszDBName, _X("XEngine_MQData"));
5662
if (!DataBase_MySQL_Connect(&xhDBSQL, pSt_DBConnector))
@@ -59,6 +65,10 @@ bool CDBModule_MQData::DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO* pSt_DBCo
5965
DBModule_dwErrorCode = DataBase_GetLastError();
6066
return false;
6167
}
68+
if (m_bMemoryInsert)
69+
{
70+
MemoryCache_DBData_SetHandle(xhDBSQL);
71+
}
6272
return true;
6373
}
6474
/********************************************************************
@@ -115,13 +125,20 @@ bool CDBModule_MQData::DBModule_MQData_Insert(XENGINE_DBMESSAGEQUEUE* pSt_DBInfo
115125
memcpy(tszSQLStatement + nRet, tszSQLCoder, nLen);
116126
nRet += nLen;
117127

118-
if (!DataBase_MySQL_Execute(xhDBSQL, tszSQLStatement, &nRet))
119-
{
120-
DBModule_IsErrorOccur = true;
121-
DBModule_dwErrorCode = DataBase_GetLastError();
122-
return false;
123-
}
124-
if (m_bMemoryCache)
128+
if (m_bMemoryInsert)
129+
{
130+
MemoryCache_DBData_QueueInsert(tszSQLStatement);
131+
}
132+
else
133+
{
134+
if (!DataBase_MySQL_Execute(xhDBSQL, tszSQLStatement, &nRet))
135+
{
136+
DBModule_IsErrorOccur = true;
137+
DBModule_dwErrorCode = DataBase_GetLastError();
138+
return false;
139+
}
140+
}
141+
if (m_bMemoryQuery)
125142
{
126143
MemoryCache_DBData_DataInsert(pSt_DBInfo);
127144
}
@@ -151,7 +168,7 @@ bool CDBModule_MQData::DBModule_MQData_Query(XENGINE_DBMESSAGEQUEUE* pSt_DBInfo)
151168
return false;
152169
}
153170

154-
if (m_bMemoryCache)
171+
if (m_bMemoryQuery)
155172
{
156173
if (MemoryCache_DBData_DataQuery(pSt_DBInfo))
157174
{
@@ -235,7 +252,7 @@ bool CDBModule_MQData::DBModule_MQData_Query(XENGINE_DBMESSAGEQUEUE* pSt_DBInfo)
235252
}
236253
DataBase_MySQL_FreeResult(xhDBSQL, xhTable);
237254

238-
if (m_bMemoryCache)
255+
if (m_bMemoryQuery)
239256
{
240257
MemoryCache_DBData_DataInsert(pSt_DBInfo);
241258
}
@@ -293,7 +310,7 @@ bool CDBModule_MQData::DBModule_MQData_Modify(XENGINE_DBMESSAGEQUEUE* pSt_DBInfo
293310
DBModule_dwErrorCode = ERROR_XENGINE_MQCORE_DATABASE_NOTFOUND;
294311
return false;
295312
}
296-
if (m_bMemoryCache)
313+
if (m_bMemoryQuery)
297314
{
298315
MemoryCache_DBData_DataInsert(pSt_DBInfo);
299316
}
@@ -603,7 +620,7 @@ bool CDBModule_MQData::DBModule_MQData_DeleteTable(LPCXSTR lpszQueueName)
603620
return false;
604621
}
605622

606-
if (m_bMemoryCache)
623+
if (m_bMemoryQuery)
607624
{
608625
XENGINE_DBMESSAGEQUEUE st_MessageQueue = {};
609626

XEngine_Source/MQCore_DBModule/DBModule_MQData/DBModule_MQData.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CDBModule_MQData
1717
CDBModule_MQData();
1818
~CDBModule_MQData();
1919
public:
20-
bool DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO* pSt_DBConnector, bool bMemoryCache = true);
20+
bool DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO* pSt_DBConnector, bool bMemoryQuery = true, bool bMemoryInsert = true);
2121
bool DBModule_MQData_Destory();
2222
bool DBModule_MQData_Insert(XENGINE_DBMESSAGEQUEUE* pSt_DBInfo);
2323
bool DBModule_MQData_Query(XENGINE_DBMESSAGEQUEUE* pSt_DBInfo);
@@ -29,6 +29,7 @@ class CDBModule_MQData
2929
bool DBModule_MQData_ShowTable(XCHAR*** pppszTableName, int* pInt_ListCount);
3030
bool DBModule_MQData_GetLeftCount(LPCXSTR lpszTableName, int nSerial, int* pInt_Count);
3131
private:
32-
bool m_bMemoryCache = false;
32+
bool m_bMemoryQuery = false;
33+
bool m_bMemoryInsert = false;
3334
XNETHANDLE xhDBSQL = 0;
3435
};

XEngine_Source/MQCore_DBModule/pch.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ extern "C" XLONG DBModule_GetLastError(int* pInt_SysError)
3131
/*************************************************************************
3232
消息队列导出函数
3333
**************************************************************************/
34-
extern "C" bool DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO * pSt_DBConnector, bool bMemoryCache)
34+
extern "C" bool DBModule_MQData_Init(DATABASE_MYSQL_CONNECTINFO * pSt_DBConnector, bool bMemoryQuery, bool bMemoryInsert)
3535
{
36-
return m_DBData.DBModule_MQData_Init(pSt_DBConnector, bMemoryCache);
36+
return m_DBData.DBModule_MQData_Init(pSt_DBConnector, bMemoryQuery, bMemoryInsert);
3737
}
3838
extern "C" bool DBModule_MQData_Destory()
3939
{

XEngine_Source/MQCore_MemoryCache/MQCore_MemoryCache.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ EXPORTS
44
MemoryCache_GetLastError
55

66
MemoryCache_DBData_Init
7+
MemoryCache_DBData_SetHandle
78
MemoryCache_DBData_Destory
89
MemoryCache_DBData_DataInsert
910
MemoryCache_DBData_DataQuery
10-
MemoryCache_DBData_DataDelete
11+
MemoryCache_DBData_DataDelete
12+
MemoryCache_DBData_QueueInsert

XEngine_Source/MQCore_MemoryCache/MemoryCache_DBData/MemoryCache_DBData.cpp

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,25 @@ CMemoryCache_DBData::~CMemoryCache_DBData()
3939
意思:是否成功
4040
备注:
4141
*********************************************************************/
42-
bool CMemoryCache_DBData::MemoryCache_DBData_Init(int nTimeLast, int nTimeStart)
42+
bool CMemoryCache_DBData::MemoryCache_DBData_Init(int nTimeLast /* = 3600 */, int nTimeStart /* = 0 */)
4343
{
4444
MemoryCache_IsErrorOccur = false;
4545

4646
bIsRun = true;
4747

48+
4849
m_nTimeLast = nTimeLast;
4950
m_nTimeStart = nTimeStart;
5051

51-
pSTDThread = std::make_unique<std::thread>(DBModule_MQUser_TimeThread, this);
52-
if (NULL == pSTDThread)
52+
pSTDThread_Query = std::make_unique<std::thread>(DBModule_MQUser_TimeThread, this);
53+
if (NULL == pSTDThread_Query)
54+
{
55+
MemoryCache_IsErrorOccur = true;
56+
MemoryCache_dwErrorCode = ERROR_XENGINE_MQCORE_MEMORYCACHE_DBDATA_THREAD;
57+
return false;
58+
}
59+
pSTDThread_Insert = std::make_unique<std::thread>(DBModule_MQUser_InsertThread, this);
60+
if (NULL == pSTDThread_Insert)
5361
{
5462
MemoryCache_IsErrorOccur = true;
5563
MemoryCache_dwErrorCode = ERROR_XENGINE_MQCORE_MEMORYCACHE_DBDATA_THREAD;
@@ -58,6 +66,27 @@ bool CMemoryCache_DBData::MemoryCache_DBData_Init(int nTimeLast, int nTimeStart)
5866
return true;
5967
}
6068
/********************************************************************
69+
函数名称:MemoryCache_DBData_SetHandle
70+
函数功能:设置句柄
71+
参数.一:xhDBSQL
72+
In/Out:In
73+
类型:句柄
74+
可空:N
75+
意思:数据库句柄
76+
返回值
77+
类型:逻辑型
78+
意思:是否成功
79+
备注:
80+
*********************************************************************/
81+
bool CMemoryCache_DBData::MemoryCache_DBData_SetHandle(XNETHANDLE xhDBSQL)
82+
{
83+
MemoryCache_IsErrorOccur = false;
84+
85+
m_xhDBSQL = xhDBSQL;
86+
87+
return true;
88+
}
89+
/********************************************************************
6190
函数名称:MemoryCache_DBData_Destory
6291
函数功能:销毁高速缓存管理器
6392
返回值
@@ -70,9 +99,13 @@ bool CMemoryCache_DBData::MemoryCache_DBData_Destory()
7099
MemoryCache_IsErrorOccur = false;
71100

72101
bIsRun = false;
73-
if (NULL != pSTDThread)
102+
if (NULL != pSTDThread_Query)
103+
{
104+
pSTDThread_Query->join();
105+
}
106+
if (NULL != pSTDThread_Insert)
74107
{
75-
pSTDThread->join();
108+
pSTDThread_Insert->join();
76109
}
77110
return true;
78111
}
@@ -211,6 +244,35 @@ bool CMemoryCache_DBData::MemoryCache_DBData_DataDelete(XENGINE_DBMESSAGEQUEUE*
211244
st_LockerQuery.unlock();
212245
return true;
213246
}
247+
/********************************************************************
248+
函数名称:MemoryCache_DBData_QueueInsert
249+
函数功能:队列插入工具
250+
参数.一:lpszSQLStr
251+
In/Out:In
252+
类型:常量字符指针
253+
可空:N
254+
意思:输入要插入的语句
255+
返回值
256+
类型:逻辑型
257+
意思:是否成功
258+
备注:
259+
*********************************************************************/
260+
bool CMemoryCache_DBData::MemoryCache_DBData_QueueInsert(LPCXSTR lpszSQLStr)
261+
{
262+
MemoryCache_IsErrorOccur = false;
263+
264+
if (NULL == lpszSQLStr)
265+
{
266+
MemoryCache_IsErrorOccur = true;
267+
MemoryCache_dwErrorCode = ERROR_XENGINE_MQCORE_MEMORYCACHE_DBDATA_PARAMENT;
268+
return false;
269+
}
270+
271+
st_LockerList.lock();
272+
stl_ListInsert.push_back(lpszSQLStr);
273+
st_LockerList.unlock();
274+
return true;
275+
}
214276
//////////////////////////////////////////////////////////////////////////
215277
// 线程函数
216278
//////////////////////////////////////////////////////////////////////////
@@ -244,4 +306,26 @@ XHTHREAD CALLBACK CMemoryCache_DBData::DBModule_MQUser_TimeThread(XPVOID lParam)
244306
std::this_thread::sleep_for(std::chrono::seconds(1));
245307
}
246308
return 0;
309+
}
310+
XHTHREAD CALLBACK CMemoryCache_DBData::DBModule_MQUser_InsertThread(XPVOID lParam)
311+
{
312+
CMemoryCache_DBData* pClass_This = (CMemoryCache_DBData*)lParam;
313+
314+
while (pClass_This->bIsRun)
315+
{
316+
if (!pClass_This->stl_ListInsert.empty())
317+
{
318+
pClass_This->st_LockerList.lock();
319+
std::string m_StrSQLInsert = pClass_This->stl_ListInsert.front();
320+
pClass_This->stl_ListInsert.pop_front();
321+
pClass_This->st_LockerList.unlock();
322+
323+
if (!DataBase_MySQL_Execute(pClass_This->m_xhDBSQL, m_StrSQLInsert.c_str()))
324+
{
325+
326+
}
327+
}
328+
std::this_thread::sleep_for(std::chrono::seconds(1));
329+
}
330+
return 0;
247331
}

XEngine_Source/MQCore_MemoryCache/MemoryCache_DBData/MemoryCache_DBData.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,27 @@ class CMemoryCache_DBData
3838
~CMemoryCache_DBData();
3939
public:
4040
bool MemoryCache_DBData_Init(int nTimeLast = 3600, int nTimeStart = 0);
41+
bool MemoryCache_DBData_SetHandle(XNETHANDLE xhDBSQL);
4142
bool MemoryCache_DBData_Destory();
4243
public:
4344
bool MemoryCache_DBData_DataInsert(XENGINE_DBMESSAGEQUEUE* pSt_DBMessageInfo);
4445
bool MemoryCache_DBData_DataQuery(XENGINE_DBMESSAGEQUEUE* pSt_DBMessageInfo);
4546
bool MemoryCache_DBData_DataDelete(XENGINE_DBMESSAGEQUEUE* pSt_DBMessageInfo);
47+
public:
48+
bool MemoryCache_DBData_QueueInsert(LPCXSTR lpszSQLStr);
4649
protected:
4750
static XHTHREAD CALLBACK DBModule_MQUser_TimeThread(XPVOID lParam);
51+
static XHTHREAD CALLBACK DBModule_MQUser_InsertThread(XPVOID lParam);
4852
private:
4953
bool bIsRun = false;
5054
int m_nTimeLast = 0;
5155
int m_nTimeStart = 0;
56+
XNETHANDLE m_xhDBSQL = 0;
5257
private:
5358
std::shared_mutex st_LockerList;
5459
std::shared_mutex st_LockerQuery;
55-
std::unique_ptr<std::thread> pSTDThread;
60+
std::unique_ptr<std::thread> pSTDThread_Query;
61+
std::unique_ptr<std::thread> pSTDThread_Insert;
5662
private:
5763
std::list<std::string> stl_ListInsert;
5864
std::unordered_map<std::tuple<__int64x, std::string>, MEMORYCACHE_DBINFO, TupleHash> stl_MapQuery;

0 commit comments

Comments
 (0)