From 6d346a3819ba7935e3d8c0789eafba2b5187b547 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Tue, 16 Jun 2020 17:35:14 +0800 Subject: [PATCH] 1 --- server/masterserver/cachemgr.cc | 43 ++++++++++++++++++++++++++++++++- server/masterserver/cachemgr.h | 9 +++++++ server/masterserver/types.h | 1 + 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/server/masterserver/cachemgr.cc b/server/masterserver/cachemgr.cc index 3001916..a3c41fc 100644 --- a/server/masterserver/cachemgr.cc +++ b/server/masterserver/cachemgr.cc @@ -1,6 +1,7 @@ #include "precompile.h" #include +#include #include "cachemgr.h" #include "app.h" @@ -12,11 +13,22 @@ void CacheMgr::Init() { INIT_LIST_HEAD(&friend_list_); + curr_entry_ = &friend_list_; + a8::Timer::Instance()->AddRepeatTimerAndAttach + ( + 1000 * 10, + a8::XParams(), + [] (const a8::XParams& param) + { + CacheMgr::Instance()->RemoveTimeoutFriend(); + }, + &timer_attacher_.timer_list_ + ); } void CacheMgr::UnInit() { - + timer_attacher_.ClearTimerList(); } void CacheMgr::_SS_IM_UserOnline(f8::MsgHdr& hdr, const ss::SS_IM_UserOnline& msg) @@ -47,6 +59,7 @@ void CacheMgr::_SS_IM_UserOnline(f8::MsgHdr& hdr, const ss::SS_IM_UserOnline& ms friend_data->svr_node = svr_node; } } + friend_data->last_active_tick = a8::XGetTickCount(); TypeConvert::Convert(user_info.base_data(), friend_data->base_data); TypeConvert::Convert(user_info.temp_custom_data(), friend_data->temp_custom_data); } @@ -66,6 +79,7 @@ void CacheMgr::_SS_IM_UserOffline(f8::MsgHdr& hdr, const ss::SS_IM_UserOffline& list_del_init(&friend_data->human_entry); } friend_data->svr_node = nullptr; + friend_data->last_active_tick = a8::XGetTickCount(); } } } @@ -90,3 +104,30 @@ Friend* CacheMgr::GetFriendData(const std::string& account_id) auto itr = friend_hash_.find(account_id); return itr != friend_hash_.end() ? itr->second : nullptr; } + +void CacheMgr::RemoveTimeoutFriend() +{ + if (friend_hash_.size() < 10000 * 50) { + return; + } + int num = 0; + long long tick = a8::XGetTickCount(); + while (!list_is_last(curr_entry_, &friend_list_) && num++ < 1000) { + list_head* p = curr_entry_; + curr_entry_ = curr_entry_->next; + + Friend* friend_data = list_entry(p, Friend, cache_entry); + long long inactive_tick = tick - friend_data->last_active_tick; + if ((friend_data->base_data.online && inactive_tick > 1000 * 3600 * 24 * 2) || + (!friend_data->base_data.online && inactive_tick > 1000 * 3600 * 24)) { + if (!list_empty(&friend_data->human_entry)) { + list_del_init(&friend_data->human_entry); + } + list_del_init(&friend_data->cache_entry); + friend_hash_.erase(friend_data->base_data.account_id); + } + } + if (list_is_last(curr_entry_, &friend_list_)) { + curr_entry_ = &friend_list_; + } +} diff --git a/server/masterserver/cachemgr.h b/server/masterserver/cachemgr.h index 73143f0..70b9144 100644 --- a/server/masterserver/cachemgr.h +++ b/server/masterserver/cachemgr.h @@ -1,5 +1,9 @@ #pragma once +#include + +#include + #include "ss_proto.pb.h" class CacheMgr : public a8::Singleton @@ -23,7 +27,12 @@ class CacheMgr : public a8::Singleton Friend* GetFriendData(const std::string& account_id); private: + void RemoveTimeoutFriend(); + + private: + a8::TimerAttacher timer_attacher_; std::map friend_hash_; list_head friend_list_; + list_head* curr_entry_ = nullptr; }; diff --git a/server/masterserver/types.h b/server/masterserver/types.h index 6725535..3fa483e 100755 --- a/server/masterserver/types.h +++ b/server/masterserver/types.h @@ -37,6 +37,7 @@ struct Friend BaseUserData base_data; UserTempCustomData temp_custom_data; + long long last_active_tick = 0; list_head cache_entry; list_head human_entry; struct SvrNode* svr_node = nullptr;