天天看點

TeamTalk db_proxy_server SyncCenter詳解

1. 簡介

CSyncCenter用于mysql記錄的群組最後聊天時間同步,同時更新redis記錄的同步時間。

2. 類與接口

void CSyncCenter::init()

初始化函數,從cache裡面加載上次同步的時間資訊等

若redis unread total_user_updated和last_update_group為空則設定其為目前時間

void CSyncCenter::updateTotalUpdate(uint32_t nUpdated)

更新上次同步内網資訊時間,設定redis unread total_user_update為nUpdated

void CSyncCenter::updateLastUpdateGroup(uint32_t nUpdated)

更新上次同步群組資訊時間,将redis unread last_update_group上次同步時間為nUpdated

void* CSyncCenter::doSyncGroupChat(void* arg)

同步線程執行函數,同步群組聊天資訊

将IMGroup各群組最後聊天時間和相應群組裡各成員IMRecentSession時間進行同步,同時更新redis unread last_update_group為最新時間

void CSyncCenter::startSync()

建立線程開啟内網資料同步以及群組聊天記錄同步

3. mysql redis相關庫表

127.0.0.1:6379[4]> select 5
OK
127.0.0.1:6379[5]> hgetall group_member_1
1) "1"
2) "1646811069"
3) "18"
4) "1646811069"
127.0.0.1:6379[5]> select 1
OK
127.0.0.1:6379[1]> get last_update_group
"1650962722"
127.0.0.1:6379[1]> get total_user_update
"1650962584"
           
mysql> select * from IMGroup;
+----+----------+--------+---------+------+---------+--------+---------+------------+------------+------------+
| id | name     | avatar | creator | type | userCnt | status | version | lastChated | updated    | created    |
+----+----------+--------+---------+------+---------+--------+---------+------------+------------+------------+
|  1 | family   |        |       1 |    2 |       6 |      0 |       3 | 1648468589 | 1646811068 | 1646811068 |
|  2 | family_2 |        |       1 |    2 |       3 |      0 |       1 |          0 | 1647313976 | 1647313976 |
+----+----------+--------+---------+------+---------+--------+---------+------------+------------+------------+
2 rows in set (0.04 sec)

mysql> select * from IMRecentSession;
+----+--------+--------+------+--------+------------+------------+
| id | userId | peerId | type | status | created    | updated    |
+----+--------+--------+------+--------+------------+------------+
|  1 |      2 |      1 |    1 |      0 | 1640850654 | 1640851183 |
|  2 |      1 |      2 |    1 |      0 | 1640850654 | 1640851183 |
|  3 |     17 |      1 |    1 |      0 | 1645607067 | 1648468503 |
|  4 |      1 |     17 |    1 |      0 | 1645607067 | 1648468503 |
|  5 |      1 |      1 |    2 |      0 | 1646811088 | 1648468589 |
|  6 |     17 |      1 |    2 |      1 | 1646811089 | 1648468575 |
|  7 |     18 |      1 |    2 |      0 | 1646811089 | 1648468589 |
+----+--------+--------+------+--------+------------+------------+
7 rows in set (0.06 sec)
           

4. 測試

//
//  test_synccenter.cpp
//  test_synccenter
//
//  Created by blueBling on 22-04-28.
//  Copyright (c) 2022年blueBling. All rights reserved.
//

#include "SyncCenter.h"
#include "util.h"

#include <iostream>


using std::cout;
using std::endl;

int test_synccenter() {

	log("startSync");
	CSyncCenter::getInstance()->startSync();
	sleep(10);
	CSyncCenter::getInstance()->stopSync();
	log("stopSync");

	return 0;
}


int main(){

	test_synccenter();

	//這裡mysql和redis連接配接池未釋放存在記憶體洩漏問題,解決方法參考test_dbpool
	
	return 0;
}
           

5. 源碼

Github

繼續閱讀