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;
}