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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值